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