1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
#!/bin/zsh
#
# Autorg :: autonomy is organization
#
# a tool to privately handle your agenda
# based on Emacs and org-mode
#
# Copyleft (C) 2010-2011 Denis Roio <[email protected]>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
VERSION=0.3
DATE=Oct/2011
AUTORGEXEC=$0
typeset -a OLDARGS
for arg in ${argv}; do OLDARGS+=($arg); done
#declare global variables
QUIET=0
DEBUG=0
PARAM=()
typeset -A global_opts
typeset -A opts
autoload colors; colors
# standard output message routines
# it's always useful to wrap them, in case we change behaviour later
notice() { if [[ $QUIET == 0 ]]; then print "$fg_bold[green][*]$fg_no_bold[white] $1" >&2; fi }
error() { if [[ $QUIET == 0 ]]; then print "$fg[red][!]$fg[white] $1" >&2; fi }
func() { if [[ $DEBUG == 1 ]]; then print "$fg[blue][D]$fg[white] $1" >&2; fi }
act() {
if [[ $QUIET == 0 ]]; then
if [ "$1" = "-n" ]; then
print -n "$fg_bold[white] . $fg_no_bold[white] $2" >&2;
else
print "$fg_bold[white] . $fg_no_bold[white] $1" >&2;
fi
fi
}
# parse configuration
if [ -r /etc/autorg/config ]; then source /etc/autorg/config; fi
if [ -r $HOME/.autorg/config ]; then source $HOME/.autorg/config
else WORKDIR=$HOME/.autorg; fi # default
# environment variable overrides all
if [ $AUTORG ]; then
if [ -r $AUTORG/config ]; then
source $AUTORG/config
else
error "configuration not found in $AUTORG"
error "correct your AUTORG environment."
return 1
fi
fi
# temp dir
mkdir -p /tmp/autorg
notice "Autorg $VERSION starting...."
act "using directory in $WORKDIR"
# ZSH commandline parser by Boyska ( Tomb.dyne.org )
option_is_set() {
#First argument, the option (something like "-s")
#Second (optional) argument: if it's "out", command will print it out 'set'/'unset'
# This is useful for if conditions
#Return 0 if is set, 1 otherwise
[[ -n ${(k)opts[$1]} ]];
r=$?
if [[ $2 == out ]]; then
if [[ $r == 0 ]]; then
echo 'set'
else
echo 'unset'
fi
fi
return $r;
}
option_value() {
#First argument, the option (something like "-s")
<<< ${opts[$1]}
}
emacs-batch() {
# used internally: renders the elisp file with config paths
# then launches the desired command.
local org_batch_templ
org_batch_templ="${DATADIR}/elisp/org-batch.el.in"
if ! [ -r "${org_batch_templ}" ]; then
error "org_batch template not found in `dirname ${org_batch_templ}`"
return 1
fi
act "using org-batch template ${org_batch_templ}"
act "emacs-batch executing ${1}"
rm -f /tmp/autorg/org-batch.el
sed -e 's@\%orgmode_path\%@'"$ORGMODE_DIR"'@g' \
-e 's@\%autorg_path\%@'"$WORKDIR"'@g' \
-e 's@\%data_path\%@'"$DATADIR"'@g' \
${DATADIR}/elisp/org-batch.el.in > \
/tmp/autorg/org-batch.el
emacs ${EMACS_OPTS} -batch -l /tmp/autorg/org-batch.el --eval="${1}"
}
init() {
if [ $1 ]; then
WORKDIR=${1}
fi
# initializes a blank directory
if [ -r $WORKDIR/.git/index ]; then
error "AutOrg already initialized in $WORKDIR"
error "operation aborted."
return 1
fi
mkdir -p $WORKDIR
mkdir -p $WORKDIR/manual
mkdir -p $WORKDIR/mobile
cp /etc/autorg/config $WORKDIR
cp $DATADIR/org-templates/agenda-setup.org $WORKDIR
cat <<EOF > $WORKDIR/.gitignore
manual
*.ps
EOF
cat <<EOF > $WORKDIR/agenda.org
#+TITLE: AutOrg agenda
#+SETUPFILE: $WORKDIR/agenda-setup.org
* `date +%Y`
EOF
cat <<EOF > $WORKDIR/mobile.org
#+TITLE: AutOrg mobile notes
EOF
cd $WORKDIR
git init
git add .
git commit -m "AutOrg automatic initialization"
cd -
}
mobile-sync() {
mkdir -p $WORKDIR/mobile
# refreshes files
# emacs-batch "(org-mobile-apply)"
# emacs-batch "(org-mobile-pull)"
# emacs-batch "(org-mobile-create-sumo-agenda)"
emacs-batch "(org-mobile-push)"
which obexftp &> /dev/null
if [[ $? != 0 ]]; then
error "obexftp not found, needed for bluetooth: operation aborted."
return 1
fi
# push all via bluetooth
mkdir -p $WORKDIR/sync
cd $WORKDIR/sync
files=`ls $WORKDIR/mobile | grep .org`
notice "Syncing files with device: $MOBILE dir: $MOBILEDIR"
for f in ${(f)files}; do gets+=("-G"); gets+=("$f"); done
for f in ${(f)files}; do puts+=("-p"); puts+=("$WORKDIR/mobile/$f"); done
obexftp -b ${MOBILE} -C ${MOBILEDIR} \
-k checksums.dat ${(@)gets}
emacs-batch "(org-mobile-pull)"
obexftp -b ${MOBILE} -C ${MOBILEDIR} \
-p $WORKDIR/mobile/checksums.dat ${(@)puts}
# obexftp -b ${MOBILE} -C ${MOBILEDIR} \
# -k checksums.dat -k mobileorg.org -k index.org -k agendas.org -k mobile.org \
# -p $WORKDIR/mobile/checksums.dat \
# -p $WORKDIR/mobile/mobileorg.org -p $WORKDIR/mobile/index.org \
# -p $WORKDIR/mobile/agendas.org -p $WORKDIR/mobile/mobile.org
# TODO error code
# return code of obexftp seems to be 255
return 0
}
publish() {
# takes two args: name of project
# and optionally "force" if should be all recompiled
local force="nil"
if [ "$2" = "force" ]; then
force="t"
act "forcing recompilation of all contents"
fi
emacs-batch "(org-publish-batch-project \"$1\" ${force})"
}
browse() {
sensible-browser "http://$WEBHOST:$PORT/index.html" &
gawk -f $DATADIR/awk/server.awk -v docroot=$1
}
main()
{
local -A subcommands_opts
### Options configuration
#Hi, dear developer! Are you trying to add a new subcommand, or to add some options?
#Well, keep in mind that:
# 1. An option CAN'T have differente meanings/behaviour in different subcommands.
# For example, "-s" means "size" and accept an argument. If you are tempted to add
# an option "-s" (that means, for example "silent", and doesn't accept an argument)
# DON'T DO IT!
# There are two reasons for that:
# I. usability; user expect that "-s" is "size
# II. Option parsing WILL EXPLODE if you do this kind of bad things
# (it will say "option defined more than once, and he's right)
main_opts=(q -quiet=q D -debug=D h -help=h v -version=v n -dry-run=n)
subcommands_opts[__default]=""
subcommands_opts[init]=""
subcommands_opts[browse]=""
subcommands_opts[help]=""
subcommands_opts[edit]=""
subcommands_opts[sync]=""
subcommands_opts[source]=""
# subcommands_opts[mount]=${subcommands_opts[open]}
# subcommands_opts[create]="s: -size=s -ignore-swap k: -key=k"
### Detect subcommand
local -aU every_opts #every_opts behave like a set; that is, an array with unique elements
for optspec in $subcommands_opts$main_opts; do
for opt in ${=optspec}; do
every_opts+=${opt}
done
done
local -a oldstar
oldstar=($argv)
zparseopts -M -E -D -Adiscardme ${every_opts}
unset discardme
subcommand=$1
if [[ -z $subcommand ]]; then
subcommand="__default"
fi
if [[ -z ${(k)subcommands_opts[$subcommand]} ]]; then #there's no such subcommand
error "Subcommand '$subcommand' doesn't exist"
exit 127
fi
argv=(${oldstar})
unset oldstar
### Parsing global + command-specific options
# zsh magic: ${=string} will split to multiple arguments when spaces occur
set -A cmd_opts ${main_opts} ${=subcommands_opts[$subcommand]}
if [[ -n $cmd_opts ]]; then #if there is no option, we don't need parsing
zparseopts -M -E -D -Aopts ${cmd_opts}
if [[ $? != 0 ]]; then
error "Some error occurred during option processing."
exit 127
fi
fi
#build PARAM (array of arguments) and check if there are unrecognized options
local ok=0
# PARAM=()
for arg in $*; do
if [[ $arg == '--' || $arg == '-' ]]; then
ok=1
continue #it shouldnt be appended to PARAM
elif [[ $arg[1] == '-' ]]; then
if [[ $ok == 0 ]]; then
error "unrecognized option $arg"
exit 127
fi
fi
PARAM+=$arg
done
#first parameter actually is the subcommand: delete it and shift
if [[ $subcommand != '__default' ]]; then
PARAM[1]=()
shift
fi
### End parsing command-specific options
if option_is_set -v; then act "AutOrg - $VERSION"; fi
if option_is_set -h; then error "TODO usage here"; fi
if option_is_set -q; then QUIET=1; fi
if option_is_set -D; then func "Debug messages ON"; DEBUG=1; fi
case "$subcommand" in
init) init ;;
browse) browse ${PARAM} ;;
sync) mobile-sync ;;
edit) act "EDIT TODO" ;;
help) act "HELP TODO" ;;
__default) ;;
*) error "command \"$subcommand\" not recognized"
act "try -h for help"
return 1
;;
esac
return 0
}
main $@
|