3 # Sends multiple files and/or directories in a MIME message.
4 # Requires tar and any specified compression program.
6 # This code is Copyright (c) 2012, by the authors of nmh. See the
7 # COPYRIGHT file in the root directory of the nmh distribution for
8 # complete copyright information.
10 usage='Usage: sendfiles [switches] -to recipient -subject subject '"\
13 sendfiles [switches] recipient subject file1 [file2 ...]
15 -compress [bzip2 | compress | gzip | lzma | none]
17 -[delay] <delay> (expressed in seconds)
20 Can use PERSON environment variable instead of -from switch.'
23 #### Find location of a program. Bourne shell just puts the name in
24 #### $0 if it's found from the PATH, so search that if necessary.
30 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
36 printf '%s\n' "$usage"
37 #### Print the nmh intro text.
38 ${nmhbindir}/mhparam -help | sed -n -e '/^$/,$p'
43 printf '%s\n' "$usage"
48 nmhbindir=`cd "$bindir" && pwd`
49 nmhlibexecdir=`$nmhbindir/mhparam libexecdir`
52 #### Process switches.
53 compress= ## compress method
54 compressarg=0 ## whether currently handling -compress
56 delayarg=0 ## whether currently handling -delay
57 from= ## From: contents
58 fromarg=0 ## whether currently handling -from
59 subject= ## Subject: contents
60 subjectarg=0 ## whether currently handling -subject
62 toarg=0 ## whether currently handling -to
65 -c|-co|-com|-comp|-compr|-compre|-compres|-compress) compressarg=1 ;;
66 -d|-de|-del|-dela|-delay) delayarg=1 ;;
67 -[0-9]|-[0-9][0-9]|-[0-9][0-9][0-9]|-[0-9][0-9][0-9][0-9])
68 delay=`printf '%s\n' "$arg" | sed -e 's%-%%'` ;;
69 -f|-fr|-fro|-from) fromarg=1 ;;
70 #### Support -gzip for backward compatibility.
71 -gzip) compress=gzip ;;
72 -h|-he|-hel|-help) help ;;
73 #### Support -none for backward compatibility.
74 -none) compress=none ;;
75 -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
77 -v|-ve|-ver|-vers|-versi|-versio|-version)
78 "$nmhlibexecdir/viamail" -version | sed 's/viamail/sendfiles/'; exit ;;
80 *) if [ $compressarg -eq 1 ]; then
83 elif [ $delayarg -eq 1 ]; then
86 elif [ $fromarg -eq 1 ]; then
89 elif [ $subjectarg -eq 1 ]; then
92 elif [ $toarg -eq 1 ]; then
96 #### Argument doesn't apply to a switch, so we're done with switches.
103 #### Check for switch after non-switch argument.
110 #### Check for required arguments (to, subject, file(s)).
111 if [ x"$to" = x ]; then
112 if [ x"$subject" = x ]; then
113 if [ $# -ge 3 ]; then
123 [ x"$subject" = x -o $# -lt 1 ] && die
126 #### Check for missing mandatory arguments.
128 if [ $compressarg -eq 1 ]; then
129 printf 'sendfiles: missing argument to -compress\n' >&2; exit 1
130 elif [ $delayarg -eq 1 ]; then
131 printf 'sendfiles: missing argument to -delay\n' >&2; exit 1
132 elif [ $fromarg -eq 1 ]; then
133 printf 'sendfiles: missing argument to -from\n' >&2; exit 1
134 elif [ $subjectarg -eq 1 ]; then
135 printf 'sendfiles: missing argument to -subject\n' >&2; exit 1
136 elif [ $toarg -eq 1 ]; then
137 printf 'sendfiles: missing argument to -to\n' >&2; exit 1
145 if [ x"$from" = x ]; then
146 if [ x"$PERSON" = x ]; then
147 from=`"$nmhlibexecdir/ap" -format '%(localmbox)' 0`
154 #### Determine compression method and descriptive info.
155 if [ x"$compress" = x ]; then
156 for compressor in gzip bzip2 lzma compress none; do
157 if [ x"`finddir $compressor`" = x ]; then :; else
158 compress="$compressor"
165 bzip2) uncompress=bzcat; conversion='; x-conversions=bzip2' ;;
166 compress) compress='compress -c'; uncompress='uncompress -c';
167 conversion='; x-conversions=compress' ;;
168 gzip) compress='gzip -c'; uncompress='gzip -cd'
169 conversion='; x-conversions=gzip' ;;
170 lzma) compress='lzma -c'; uncompress='lzma -cd'
171 conversion='; x-conversions=lzma' ;;
172 none) compress=cat uncompress=cat; conversion= ;;
173 *) printf 'sendfiles: unknown compression method "%s"\n' \
179 #### Send using viamail.
180 tar cvf - "$@" | $compress | \
181 "$nmhlibexecdir/viamail" -to "$to" -subject "$subject" \
182 -from "$from" -parameters "type=tar$conversion" \
183 -comment "extract with $uncompress | tar xvpf -" \
184 -delay "$delay" -verbose