f8778b1b6461f4131332e77a2e14d94138b97fd1
[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 [ $1 -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 }
65
66 get_showproc()
67 {
68         mhshowproc=`mhparam 'listproc'`
69         return
70 }
71
72 get_realpath()
73 {
74         reldir=`dirname "$1"`
75         filename=`basename "$1"`
76         cd $reldir
77         echo "$PWD/$filename"
78         cd -
79 }
80
81 get_attachmentheader()
82 {
83         header=`mhparam 'Attachment-Header'`
84 }
85
86 set_lasteditor()
87 {
88         anno -delete -number all -component 'last-editor' $mhmetafile
89         anno -nodate -component 'last-editor' -text "$1" $mhmetafile
90 }
91
92 create()
93 {
94         if [ -z "$mhdraft" ]
95         then
96                 usage 1
97         fi
98         mhmetafile=$mhdraft.meta
99         touch $mhmetafile
100         if [ -z $mheditor ]
101         then
102                 get_editor
103         fi
104         if [ -n "$mhaltmsg" ]
105         then
106                 anno -nodate -component 'mhaltmsg' -text "$mhaltmsg" $mhmetafile
107         fi
108         if [ -n "$mhdist" ]
109         then
110                 anno -nodate -component 'mhdist' -text "$mhdist" $mhmetafile
111         fi
112         if [ -n "$mhdist" ]
113         then
114                 anno -nodate -component 'mhuse' -text "$mhuse" $mhmetafile
115         fi
116         if [ -n "$mhfolder" ]
117         then
118                 anno -nodate -component 'mhfolder' -text "$mhfolder" $mhmetafile
119         fi
120         if [ -n "$mhmessages" ]
121         then
122                 anno -nodate -component 'mhmessages' -text "$mhmessages" $mhmetafile
123         fi
124         if [ -n "$mhannotate" ]
125         then
126                 anno -nodate -component 'mhannotate' -text "$mhannotate" $mhmetafile
127         fi
128         set_lasteditor "$mheditor"
129         exec $mheditor $mhdraft
130 }
131
132 edit()
133 {
134         if [ $# -eq 0 ]
135         then
136                 get_editor
137         else
138                 mheditor="$@"
139         fi
140
141         set_lasteditor "$mheditor"
142         exec $mheditor $mhdraft
143 }
144
145 list()
146 {
147         get_showproc
148         exec $mhshowproc -file $mhdraft
149 }
150
151 send()
152 {
153         export mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
154         export mhdist=`anno -list -component 'mhdist' $mhmetafile`
155         export mhuse=`anno -list -component 'mhuse' $mhmetafile`
156         export mhfolder=`anno -list -component 'mhfolder' $mhmetafile`
157         export mhmessages=`anno -list -component 'mhmessages' $mhmetafile`
158         export mhannotate=`anno -list -component 'mhannotate' $mhmetafile`
159         rm -f $mhmetafile
160         exec send "$@" $mhdraft
161 }
162
163 delete()
164 {
165         folder -push $draftfolder >/dev/null 2>&1
166         rmm $draftfolder c
167         folder -pop >/dev/null 2>&1
168         rm $mhmetafile
169 }
170
171 attach()
172 {
173         get_attachmentheader
174         while [ -n "$1" ]
175         do
176                 if [ ! -f "$1" ]
177                 then
178                         echo "file not found: $1" 1>&2
179                         shift
180                         echo -n "folloing files are not attached: " 1>&2
181                         echo -n "$1" 1>&2
182                         echo "$@" 1>&2
183                         exit 1
184                 fi
185                 file=`get_realpath "$1"`
186                 anno -nodate -append -component $header -text "$file" $mhdraft
187                 shift
188         done
189 }
190
191 alist()
192 {
193         get_attachmentheader
194         anno -list -number -component $header $mhdraft
195 }
196
197 detach()
198 {
199         get_attachmentheader
200         while [ -n "$1" ]
201         do
202                 anno -delete -component $header -number "$1" $mhdraft
203                 if [ $? -ne 0 ]
204                 then
205                         echo "can't delet attachment $1" 1>&2
206                         exit 1
207                 fi
208                 shift
209         done
210 }
211
212 display()
213 {
214         mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
215         get_showproc
216         if [ -n "$mhaltmsg" ]
217         then
218                 echo "no altmsg" 1>&2
219                 exit 1
220         fi
221         exec $mhshowproc -file $mhaltmsg
222 }
223
224 if [ $# -eq 0 ]
225 then
226         create
227         exit
228 fi
229
230 command=$1
231 shift
232
233 draftfolder="mhparam draftfolder"
234
235 mhdraft=`mhpath $draftfolder c 2>/dev/null`
236 if [ -z "$mhdraft" ]
237 then
238         case $command in
239         -h*)
240                 usage $#
241                 ;;
242         -V*)
243                 version $#
244                 ;;
245         *)
246                 echo "no current message in $draftsfolder" 1>&2
247                 usage 1
248                 ;;
249         esac
250 fi
251 mhmetafile=$mhdraft.meta
252 touch $mhmetafile
253
254
255 case $command in
256 e|ed|edi|edit)
257         edit "$@"
258         ;;
259 l|li|lis|list)
260         [ $# -eq 0 ] || usage 1
261         list
262         ;;
263 s|se|sen|send)
264         send "$@"
265         ;;
266 del|dele|delet)
267         [ $# -eq 0 ] || usage 1
268         delete
269         ;;
270 di|dis|disp|displ|displa|display)
271         [ $# -eq 0 ] || usage 1
272         display
273         ;;
274 at|att|atta|attac|attach)
275         attach "$@"
276         ;;
277 al|ali|alis|alist)
278         [ $# -eq 0 ] || usage 1
279         alist
280         ;;
281 det|deta|detac|detach)
282         detach "$@"
283         ;;
284 r|re|ref|refi|refil|refile)
285         refile -file $mhdraft "$@"
286         ;;
287 w|wh|who|whom)
288         whom "$@" $mhdraft
289         ;;
290 -h|-he|-hel|-help)
291         usage $#
292         ;;
293 -V|-Ve|-Ver|-Vers|-Versi|-Versio|-Version)
294         version $#
295         ;;
296 esac