]> diplodocus.org Git - nmh/blob - etc/sendfiles
Replace GNUism with standard touch flag in test-scan-file.
[nmh] / etc / sendfiles
1 #! /bin/sh
2 #
3 # Sends multiple files and/or directories in a MIME message.
4 # Requires tar and any specified compression program.
5 #
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.
9
10 usage='Usage: sendfiles [switches] -to recipient -subject subject '"\
11 "'file1 [file2 ...]
12 or
13 sendfiles [switches] recipient subject file1 [file2 ...]
14 switches are:
15 -compress [bzip2 | compress | gzip | lzma | none]
16 -from <sender>
17 -version
18 -help
19 Can use PERSON environment variable instead of -from switch.'
20
21
22 #### Find location of a program. Bourne shell just puts the name in
23 #### $0 if it's found from the PATH, so search that if necessary.
24 finddir() {
25 case $1 in
26 */*) dirname "$1" ;;
27 * ) IFS=:
28 for d in $PATH; do
29 [ -f "${d:=.}/$1" -a -x "$d/$1" ] && printf %s "$d" && break
30 done ;;
31 esac
32 }
33
34 help() {
35 printf '%s\n' "$usage"
36 #### Print the nmh intro text.
37 ${nmhbindir}/mhparam -help | sed -n -e '/^$/,$p'
38 exit
39 }
40
41 die() {
42 printf '%s\n' "$usage"
43 exit 1
44 }
45
46 bindir=`finddir $0`
47 nmhbindir=`cd "$bindir" && pwd`
48 nmhlibexecdir=`$nmhbindir/mhparam libexecdir`
49
50
51 #### Process switches.
52 compress= ## compress method
53 compressarg=0 ## whether currently handling -compress
54 from= ## From: contents
55 fromarg=0 ## whether currently handling -from
56 subject= ## Subject: contents
57 subjectarg=0 ## whether currently handling -subject
58 to= ## To: address
59 toarg=0 ## whether currently handling -to
60 for arg in "$@"; do
61 case $arg in
62 -c|-co|-com|-comp|-compr|-compre|-compres|-compress) compressarg=1 ;;
63 -f|-fr|-fro|-from) fromarg=1 ;;
64 #### Support -gzip for backward compatibility.
65 -gzip) compress=gzip ;;
66 -h|-he|-hel|-help) help ;;
67 #### Support -none for backward compatibility.
68 -none) compress=none ;;
69 -s|-su|-sub|-subj|-subje|-subjec|-subject) subjectarg=1 ;;
70 -t|-to) toarg=1 ;;
71 -v|-ve|-ver|-vers|-versi|-versio|-version)
72 "$nmhlibexecdir/viamail" -version | sed 's/viamail/sendfiles/'; exit ;;
73 -*) die ;;
74 *) if [ $compressarg -eq 1 ]; then
75 compress="$arg"
76 compressarg=0
77 elif [ $fromarg -eq 1 ]; then
78 from="$arg"
79 fromarg=0
80 elif [ $subjectarg -eq 1 ]; then
81 subject="$arg"
82 subjectarg=0
83 elif [ $toarg -eq 1 ]; then
84 to="$arg"
85 toarg=0
86 else
87 #### Argument doesn't apply to a switch, so we're done with switches.
88 break
89 fi ;;
90 esac
91 shift
92 done
93
94 #### Check for switch after non-switch argument.
95 for arg in "$@"; do
96 case $arg in
97 -*) die ;;
98 esac
99 done
100
101 #### Check for required arguments (to, subject, file(s)).
102 if [ x"$to" = x ]; then
103 if [ x"$subject" = x ]; then
104 if [ $# -ge 3 ]; then
105 to="$1"; shift
106 subject="$1"; shift
107 else
108 die
109 fi
110 else
111 die
112 fi
113 else
114 [ x"$subject" = x -o $# -lt 1 ] && die
115 fi
116
117 #### Check for missing mandatory arguments.
118 checkforargs() {
119 if [ $compressarg -eq 1 ]; then
120 printf 'sendfiles: missing argument to -compress\n' >&2; exit 1
121 elif [ $fromarg -eq 1 ]; then
122 printf 'sendfiles: missing argument to -from\n' >&2; exit 1
123 elif [ $subjectarg -eq 1 ]; then
124 printf 'sendfiles: missing argument to -subject\n' >&2; exit 1
125 elif [ $toarg -eq 1 ]; then
126 printf 'sendfiles: missing argument to -to\n' >&2; exit 1
127 fi
128 }
129
130 checkforargs
131 [ $# -eq 0 ] && die
132
133
134 if [ x"$from" = x ]; then
135 if [ x"$PERSON" = x ]; then
136 from=`"$nmhlibexecdir/ap" -format '%(localmbox)' 0`
137 else
138 from="$PERSON"
139 fi
140 fi
141
142
143 #### Determine compression method and descriptive info.
144 if [ x"$compress" = x ]; then
145 for compressor in gzip bzip2 lzma compress none; do
146 if [ x"`finddir $compressor`" = x ]; then :; else
147 compress="$compressor"
148 break
149 fi
150 done
151 fi
152
153 case $compress in
154 bzip2) uncompress=bzcat; conversion='; x-conversions=bzip2' ;;
155 compress) compress='compress -c'; uncompress='uncompress -c';
156 conversion='; x-conversions=compress' ;;
157 gzip) compress='gzip -c'; uncompress='gzip -cd'
158 conversion='; x-conversions=gzip' ;;
159 lzma) compress='lzma -c'; uncompress='lzma -cd'
160 conversion='; x-conversions=lzma' ;;
161 none) compress=cat uncompress=cat; conversion= ;;
162 *) printf 'sendfiles: unknown compression method "%s"\n' \
163 "$compress" >&2
164 die ;;
165 esac
166
167
168 #### Send using viamail.
169 tar cvf - "$@" | $compress | \
170 "$nmhlibexecdir/viamail" -to "$to" -subject "$subject" \
171 -from "$from" -parameters "type=tar$conversion" \
172 -comment "extract with $uncompress | tar xvpf -" \
173 -verbose