add my whatnow replacement whatnow2
[mmh] / uip / whatnow2.sh
1 #!/bin/sh
2
3 printhelp()
4 {
5         echo "Usage: $0 command"
6         echo "  commands are:"
7         echo "  edit [editor]"
8         echo "  list"
9         echo "  send [sendargs]"
10         echo "  delete"
11         echo "  display"
12         echo "  attach files"
13         echo "  alist"
14         echo "  detach anum"
15         echo "  refile +folder"
16         echo "  -help"
17         echo "  -Version"
18 }
19
20 version()
21 {
22         if [ $# -eq 0 ]
23         then
24                 echo "$0 has no own version number, thus this instead:"
25                 folder -Version
26                 exit 0
27         fi
28         echo "$0 has no own version number, thus this instead:" 1>&2
29         folder -Version 1>&2
30         exit 1
31 }
32
33 usage()
34 {
35         if [ $1 -eq 0 ]
36         then
37                 printhelp
38                 exit 0
39         fi
40         printhelp 1>&2
41         exit $1
42 }
43
44 get_editor()
45 {
46         if [ -f "$mhmetafile" ]
47         then
48                 lasteditor=`anno -list -component 'last-editor' $mhmetafile`
49                 if [ -n "$lasteditor" ]
50                 then
51                         editor=`echo $lasteditor | cut -d ' ' -f 1`
52                         mheditor=`mhparam "$editor-next"`
53                         [ -n "$mheditor" ] && return
54                         mheditor=$lasteditor
55                         return
56                 fi
57         fi
58         if [ -n "$MMHEDITOR" ]
59         then
60                 mheditor=$MMHEDITOR
61                 return
62         fi
63         mheditor=`mhparam 'Editor'`
64         if [ -n "$mheditor" ]
65         then
66                 return
67         fi
68         if [ -n "$VISUAL" ]
69         then
70                 mheditor=$VISUAL
71                 return
72         fi
73         if [ -n "$EDITOR" ]
74         then
75                 mheditor=$EDITOR
76                 return
77         fi
78         mheditor=vi
79 }
80
81 get_showproc()
82 {
83         mhshowproc=`mhparam 'listproc'`
84         return
85 }
86
87 get_realpath()
88 {
89         reldir=`dirname "$1"`
90         filename=`basename "$1"`
91         cd $reldir
92         echo "$PWD/$filename"
93         cd -
94 }
95
96 get_attachmentheader()
97 {
98         header=`mhparam 'Attachment-Header'`
99 }
100
101 set_lasteditor()
102 {
103         anno -delete -number all -component 'last-editor' $mhmetafile
104         anno -nodate -component 'last-editor' -text "$1" $mhmetafile
105 }
106
107 create()
108 {
109         if [ -z "$mhdraft" ]
110         then
111                 usage 1
112         fi
113         mhmetafile=$mhdraft.meta
114         touch $mhmetafile
115         if [ -z $mheditor ]
116         then
117                 get_editor
118         fi
119         if [ -n "$mhaltmsg" ]
120         then
121                 anno -nodate -component 'mhaltmsg' -text "$mhaltmsg" $mhmetafile
122         fi
123         if [ -n "$mhdist" ]
124         then
125                 anno -nodate -component 'mhdist' -text "$mhdist" $mhmetafile
126         fi
127         if [ -n "$mhdist" ]
128         then
129                 anno -nodate -component 'mhuse' -text "$mhuse" $mhmetafile
130         fi
131         if [ -n "$mhfolder" ]
132         then
133                 anno -nodate -component 'mhfolder' -text "$mhfolder" $mhmetafile
134         fi
135         if [ -n "$mhmessages" ]
136         then
137                 anno -nodate -component 'mhmessages' -text "$mhmessages" $mhmetafile
138         fi
139         if [ -n "$mhannotate" ]
140         then
141                 anno -nodate -component 'mhannotate' -text "$mhannotate" $mhmetafile
142         fi
143         set_lasteditor "$mheditor"
144         exec $mheditor $mhdraft
145 }
146
147 edit()
148 {
149         if [ $# -eq 0 ]
150         then
151                 get_editor
152         else
153                 mheditor="$@"
154         fi
155
156         set_lasteditor "$mheditor"
157         exec $mheditor $mhdraft
158 }
159
160 list()
161 {
162         get_showproc
163         exec $mhshowproc -file $mhdraft
164 }
165
166 send()
167 {
168         export mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
169         export mhdist=`anno -list -component 'mhdist' $mhmetafile`
170         export mhuse=`anno -list -component 'mhuse' $mhmetafile`
171         export mhfolder=`anno -list -component 'mhfolder' $mhmetafile`
172         export mhmessages=`anno -list -component 'mhmessages' $mhmetafile`
173         export mhannotate=`anno -list -component 'mhannotate' $mhmetafile`
174         rm -f $mhmetafile
175         exec send "$@" $mhdraft
176 }
177
178 delete()
179 {
180         rmm +draft c
181         rm $mhmetafile
182 }
183
184 attach()
185 {
186         get_attachmentheader
187         while [ -n "$1" ]
188         do
189                 if [ ! -f "$1" ]
190                 then
191                         echo "file not found: $1" 1>&2
192                         shift
193                         echo -n "folloing files are not attached: " 1>&2
194                         echo -n "$1" 1>&2
195                         echo "$@" 1>&2
196                         exit 1
197                 fi
198                 file=`get_realpath "$1"`
199                 anno -nodate -append -component $header -text "$file" $mhdraft
200                 shift
201         done
202 }
203
204 alist()
205 {
206         get_attachmentheader
207         anno -list -number -component $header $mhdraft
208 }
209
210 detach()
211 {
212         get_attachmentheader
213         while [ -n "$1" ]
214         do
215                 anno -delete -component $header -number "$1" $mhdraft
216                 if [ $? -ne 0 ]
217                 then
218                         echo "can't delet attachment $1" 1>&2
219                         exit 1
220                 fi
221                 shift
222         done
223 }
224
225 display()
226 {
227         mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
228         get_showproc
229         if [ -n "$mhaltmsg" ]
230         then
231                 echo "no altmsg" 1>&2
232                 exit 1
233         fi
234         exec $mhshowproc -file $mhaltmsg
235 }
236
237 if [ $# -eq 0 ]
238 then
239         create
240         exit
241 fi
242
243 command=$1
244 shift
245
246 draftfolder=`mhparam Draft-Folder`
247 if [ -z "$dratffolder" ]
248 then
249     draftfolder="+drafts"
250 fi
251 mhdraft=`mhpath $draftfolder c 2>/dev/null`
252 if [ -z "$mhdraft" ]
253 then
254         case $command in
255         -h*)
256                 usage $#
257                 ;;
258         -V*)
259                 version $#
260                 ;;
261         *)
262                 echo "no current message in $draftsfolder" 1>&2
263                 usage 1
264                 ;;
265         esac
266 fi
267 mhmetafile=$mhdraft.meta
268 touch $mhmetafile
269
270
271 case $command in
272 e*)
273         edit "$@"
274         ;;
275 l*)
276         [ $# -eq 0 ] || usage 1
277         list
278         ;;
279 s*)
280         send "$@"
281         ;;
282 del*)
283         [ $# -eq 0 ] || usage 1
284         delete
285         ;;
286 di*)
287         [ $# -eq 0 ] || usage 1
288         display
289         ;;
290 at*)
291         attach "$@"
292         ;;
293 al*)
294         [ $# -eq 0 ] || usage 1
295         alist
296         ;;
297 det*)
298         detach "$@"
299         ;;
300 r*)
301         refile -nolink -file $mhdraft "$@"
302         ;;
303 w*)
304         whom "$@" $mhdraft
305         ;;
306 -h*)
307         usage $#
308         ;;
309 -V*)
310         version $#
311         ;;
312 esac