Fixed to work with Solaris Bourne shell.
[mmh] / docs / contrib / build_nmh
1 #! /bin/sh
2 #
3 # Configures and builds nmh.
4 # * This script must be invoked from an nmh source directory.
5 # * This script retrieves configuration from the first existing nmh
6 #   installation on your $PATH, if any, as well as any $EDITOR/$VISUAL
7 #   and $PAGER environment variable settings.
8 # * Unless the -y option is provided, this script then interactively
9 #   walks you through confirmation of common configuration settings.
10 #
11 # Typical usage:
12 # The first time you invoke this script, use the -i option to install
13 # nmh in the specified location.  The script will walk you through the
14 # common nmh configuration settings.  The -v option will cause display
15 # of brief progress indicators.  Be sure to add the bin directory of
16 # the install location to your $PATH, if not already there.
17 # Subsequently, invoke this script with the -y option, to use the
18 # relevant configuration settings from the installed nmh without
19 # confirmation.
20 #
21 # Option summary:
22 #   First time use:
23 #     -i to install nmh
24 #     -v to display progress
25 #   Subsequent uses, assuming installed nmh bin directory is on $PATH:
26 #     -y to accept all configuration options without confirmation
27 #   Output control:
28 #     -l <logfile name>, default 'build_nmh.log'
29 #   Advanced/developer use:
30 #     -c to run 'make distcheck' instead of 'make check'
31 #     -d to build nmh with debug enabled
32 #     -s to use 'make superclean': requires recent autoconf and automake,
33 #        see docs/README.developers
34 #     -r to build rpm
35 #
36 # On Fedora, at least these rpms must be installed:
37 #   gdbm-devel
38 #   ncurses-devel
39 #   cyrus-sasl-devel, if using sasl
40 #   openssl-devel, if using TLS
41 #   autoconf and automake, with -s (see docs/README.developers for versions)
42 #   rpm-build, with -r
43
44
45 ####
46 #### OS-specific setup.
47 ####
48 which=which
49 ldd=ldd
50 locking=dot
51
52 os=${OSTYPE:-`uname -s`}
53 #### It'd be nice to have configure decide what locking style to use.
54 #### In the meantime, select it manually.  To determine what style
55 #### locking your system uses by default, try something like this,
56 #### assuming that strace is installed:
57 ####   $ echo test | strace -o /tmp/mail.strace mail -s test $LOGNAME
58 #### Then look in /tmp/mail.strace for fcntl, flock, and lockf system
59 #### calls and opens of dot files in the mail spool.
60 #### These might help but are old:
61 ####   https://bugzilla.mozilla.org/show_bug.cgi?id=239013#c9
62 ####   http://www.jwz.org/doc/movemail.html
63 case "$os" in
64   aix) locking=fcntl ;;
65   freebsd*) locking=flock ;;
66   linux* ) locking=fcntl ;;
67 esac
68
69
70 ####
71 #### Interpret command arguments.
72 ####
73 check=check
74 debug=0
75 install=0
76 logfile=build_nmh.log
77 build_rpm=0
78 superclean=0
79 verbose=0
80 yes=0
81 usage="usage: $0
82   [-c to run 'make distcheck' instead of 'make check']
83   [-d to build nmh with debug enabled]
84   [-i to install nmh]
85   [-l <logfile name>, default '$logfile']
86   [-r to build rpm]
87   [-s to use 'make superclean': requires recent autoconf and automake]
88   [-v to display progress]
89   [-y to accept all configuration options without confirmation]"
90
91 while getopts 'cdil:rsvy?' arg; do
92   case $arg in
93     c  ) check=distcheck ;;
94     d  ) debug=1 ;;
95     i  ) install=1 ;;
96     l  ) logfile=$OPTARG ;;
97     r  ) build_rpm=1 ;;
98     s  ) superclean=1 ;;
99     v  ) verbose=1 ;;
100     y  ) yes=1 ;;
101     '?') echo "$usage"; exit 0 ;;
102   esac
103 done
104 shift `expr $OPTIND - 1`
105
106 #### No command line arguments are supported.
107 if [ $# -gt 0 ]; then
108   echo "usage: $0"
109   exit 1
110 fi
111
112 #### Check to see that we're in a nmh source directory.
113 if grep 'the authors of nmh' COPYRIGHT >/dev/null; then
114   :
115 else
116   echo "$0: not in nmh source directory"
117   exit 1
118 fi
119
120 ####
121 #### Set up configure options.  Handle options that can have embedded
122 #### spaces (currently just smtpservers) specially.
123 ####
124
125 #### dotlocking, the usual default, requires chgrp and chmod of inc.
126 installpriv=
127 if [ $install -ge 1  -a  `id -u` -ne 0  -a  "$OSTYPE" != cygwin ]; then
128   if [ "x$locking" = x  -o  "$locking" = dot ]; then
129     echo "$0: "'install requires chgrp and chmod 2755'
130     echo 'so will sudo to install.  Terminate with Ctrl-C if unacceptable.'
131     installpriv=sudo
132   fi
133 fi
134
135
136 #### Here are the config options that we will try to detect, then
137 #### confirm, and finally set.
138 config_prefix=/usr/local/nmh
139 config_locking="$locking"
140 config_mts=smtp
141 config_smtpservers=localhost
142 config_sasl=n
143 config_tls=n
144 config_editor=vi
145 for i in more less most cat; do
146   if which $i >/dev/null 2>&1; then
147     config_pager=$i
148     break
149   fi
150 done
151 config_debug=n
152
153 if install-mh -check >/dev/null 2>&1; then
154   # Determine config options from installed nmh.
155   mhparam=`which mhparam`
156   mhbin=`dirname "$mhparam"`
157
158   config_prefix=`cd $mhbin/.. && pwd`
159
160   mtsconf=`dirname "$mhbin"`/etc/mts.conf
161   if [ -f "$mtsconf" ]; then
162     mts_entry=`grep '^mts:' $mtsconf`
163     if [ "mts_entry" ]; then
164       mts=`echo $mts_entry | sed -e 's/^mts: *//'`
165       if [ "$mts"  -a  "$mts" != smtp ]; then
166         config_mts="$mts"
167       fi
168     fi
169
170     mtsconfservers=`grep '^servers:' $mtsconf`
171     if [ "$mtsconfservers" ]; then
172       servers=`echo $mtsconfservers | sed -e 's/^servers: *//' -e 's/ /\\\ /g'`
173       [ "$servers" ]  &&  config_smtpservers="$servers"
174     fi
175   fi
176
177   if $ldd $mhbin/inc | grep sasl >/dev/null; then
178     config_sasl=y
179   fi
180
181   if $ldd $mhbin/inc | grep ssl >/dev/null; then
182     config_tls=y
183   fi
184 fi
185
186 if [ "$EDITOR" ]; then
187   config_editor="$EDITOR"
188 elif [ "$VISUAL" ]; then
189   config_editor="$VISUAL"
190 fi
191
192 [ "$PAGER" ]  &&  config_pager="$PAGER"
193
194 [ $debug -ge 1 ]  &&  config_debug=y
195
196 if [ $yes -eq 0 ]; then
197   #### Confirm each config setting with user.
198   printf 'Install prefix [%s]: ' $config_prefix
199   read prefix
200   [ "$prefix" ]  &&  config_prefix="$prefix"
201
202   printf 'Locking type (dot|fcntl|flock|lockf) [%s]: ' $config_locking
203   read locking
204   [ "$locking" ]  &&  config_locking="$locking"
205
206   printf 'MTS (smtp|sendmail) [%s]: ' $config_mts
207   read mts
208   [ "$mts" ]  &&  config_mts="$mts"
209
210   if [ "$mts"  -o  "$mts" = smtp ]; then
211     :
212   else
213     printf 'SMTP server(s), space separated [%s]: ' $config_smtpservers
214     read response
215     servers=`echo $response | sed -e 's/ /\\\ /g'`
216     [ "$servers" ]  &&  config_smtpservers="$servers"
217   fi
218
219   printf 'Cyrus SASL support [%s]: ' $config_sasl
220   read response
221   if [ "$response" = y  -o  "$response" = Y ]; then
222     config_sasl=y
223   elif [ "$response" = n  -o  "$response" = N ]; then
224     config_sasl=n
225   fi
226
227   printf 'TLS support [%s]: ' $config_tls
228   read response
229   if [ "$response" = y  -o  "$response" = Y ]; then
230     config_tls=y
231   elif [ "$response" = n  -o  "$response" = N ]; then
232     config_tls=n
233   fi
234
235   printf 'Default editor [%s]: ' $config_editor
236   read editor
237   [ "$editor" ]  &&  config_editor=$editor
238
239   printf 'Pager [%s]: ' $config_pager
240   read pager
241   [ "$pager" ]  &&  config_pager=$pager
242
243   #### Don't confirm debug here:  obey the -d option to this script.
244 fi
245
246 smtpservers=
247 config_opts="--prefix=$config_prefix"
248
249 [ "$config_locking" ]  &&  \
250   config_opts="$config_opts --with-locking=$config_locking"
251 [ "$config_mts"  -a  "$config_mts" != smtp ]  &&  \
252   config_opts="$config_opts --with-mts=$config_mts"
253 [ "$config_smtpservers"  -a  "$config_smtpservers" != localhost ]  &&  \
254   smtpservers="--with-smtpservers=$config_smtpservers"
255 [ "$config_sasl" = y ]  &&  \
256   config_opts="$config_opts --with-cyrus-sasl"
257 [ "$config_tls" = y ]  &&  \
258   config_opts="$config_opts --with-tls"
259 [ "$config_editor" ]  &&  \
260   config_opts="$config_opts --with-editor=$config_editor"
261 [ "$config_pager" ]  &&  \
262   config_opts="$config_opts --with-pager=$config_pager"
263 [ $config_debug = y ]  &&  \
264   config_opts="$config_opts --enable-debug"
265
266
267 ####
268 #### Clean up, and set up with autoconfig if necessary.
269 ####
270 if [ -f Makefile ]; then
271   [ $verbose -ge 1 ]  &&  echo cleaning . . .
272   if [ $superclean -ge 1 ]; then
273     make superclean >/dev/null
274   else
275     make distclean >/dev/null
276   fi
277 fi
278
279 /bin/rm -f $logfile
280 if [ -f configure  -a  -f Makefile.in ]; then
281   :
282 else
283   [ $verbose -ge 1 ]  &&  echo autoconfiguring . . .
284   ./autogen.sh >>$logfile 2>&1
285 fi
286
287
288 ####
289 #### Build.
290 ####
291 [ $verbose -ge 1 ]  &&  echo configuring . . .
292 echo ./configure $config_opts ${smtpservers:+"$smtpservers"} >>$logfile 2>&1
293 ./configure $config_opts ${smtpservers:+"$smtpservers"} >>$logfile 2>&1
294 status=$?
295
296 if [ $status -eq 0 ]; then
297   [ $verbose -ge 1 ]  &&  echo building . . .
298   make >>$logfile 2>&1
299   status=$?
300
301   if [ $status -eq 0 ]; then
302     [ $verbose -ge 1 ]  &&  echo testing . . .
303     checkoutput=`make $check SETGID_MAIL= 2>>$logfile`
304     status=$?
305
306     tests_summary=`echo "$checkoutput" | grep tests`
307     if [ "$tests_summary" ]; then
308       echo '===================' >>$logfile
309       echo $tests_summary >>$logfile
310       echo '===================' >>$logfile
311       [ "$check" = distcheck ]  &&  \
312         echo "$checkoutput" | tail -n 4 >>$logfile
313     fi
314
315     if [ $status -eq 0 ]; then
316       if [ $install -ge 1 ]; then
317         [ $verbose -ge 1 ]  &&  echo installing . . .
318         ($installpriv make install) >/dev/null 2>>$logfile
319         status=$?
320       fi
321
322       if [ $status -eq 0  -a  $build_rpm -ge 1 ]; then
323         [ $verbose -ge 1 ]  &&  echo building rpm . . .
324         make rpm >/dev/null 2>>$logfile
325         status=$?
326       fi
327     fi
328   fi
329 fi
330
331 grep 'Error' $logfile
332 grep 'warn' $logfile
333 [ $status -ne 0 ]  &&  echo build failed!
334 [ $status -eq 0  -a  $verbose -ge 1 ]  &&  echo build completed successfully
335
336 exit $status