Cleaned up a couple of shell variables.
[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
51 ####
52 #### Interpret command arguments.
53 ####
54 check=check
55 debug=0
56 install=0
57 logfile=build_nmh.log
58 build_rpm=0
59 superclean=0
60 verbose=0
61 yes=0
62 usage="usage: $0
63   [-c to run 'make distcheck' instead of 'make check']
64   [-d to build nmh with debug enabled]
65   [-i to install nmh]
66   [-l <logfile name>, default '$logfile']
67   [-r to build rpm]
68   [-s to use 'make superclean': requires recent autoconf and automake]
69   [-v to display progress]
70   [-y to accept all configuration options without confirmation]"
71
72 while getopts 'cdil:rsvy?' arg; do
73   case $arg in
74     c  ) check=distcheck ;;
75     d  ) debug=1 ;;
76     i  ) install=1 ;;
77     l  ) logfile=$OPTARG ;;
78     r  ) build_rpm=1 ;;
79     s  ) superclean=1 ;;
80     v  ) verbose=1 ;;
81     y  ) yes=1 ;;
82     '?') echo "$usage"; exit 0 ;;
83   esac
84 done
85 shift `expr $OPTIND - 1`
86
87 #### No command line arguments are supported.
88 if [ $# -gt 0 ]; then
89   echo "usage: $0"
90   exit 1
91 fi
92
93 #### Check to see that we're in a nmh source directory.
94 if grep 'the authors of nmh' COPYRIGHT >/dev/null; then
95   :
96 else
97   echo "$0: not in nmh source directory"
98   exit 1
99 fi
100
101 ####
102 #### Set up configure options.  Handle options that can have embedded
103 #### spaces (currently just smtpservers) specially.
104 ####
105
106 #### Here are the config options that we will try to detect, then
107 #### confirm, and finally set.
108 config_prefix=/usr/local/nmh
109 config_locking=
110 config_mts=smtp
111 config_smtpservers=localhost
112 config_sasl=n
113 config_tls=n
114 config_editor=vi
115 for i in more less most cat; do
116   if which $i >/dev/null 2>&1; then
117     config_pager=$i
118     break
119   fi
120 done
121 config_debug=n
122
123 if install-mh -check >/dev/null 2>&1; then
124   # Determine config options from installed nmh.
125   mhparam=`which mhparam`
126   mhbin=`dirname "$mhparam"`
127
128   config_prefix=`cd $mhbin/.. && pwd`
129
130   mtsconf=`dirname "$mhbin"`/etc/mts.conf
131   if [ -f "$mtsconf" ]; then
132     mts_entry=`grep '^mts:' "$mtsconf"`
133     if [ "$mts_entry" ]; then
134       mts=`echo "$mts_entry" | sed -e 's/^mts: *//'`
135       if [ "$mts"  -a  "$mts" != smtp ]; then
136         config_mts="$mts"
137       fi
138     fi
139
140     mtsconfservers=`grep '^servers:' "$mtsconf"`
141     if [ "$mtsconfservers" ]; then
142       servers=`echo "$mtsconfservers" | \
143                sed -e 's/^servers: *//' -e 's/ /\\\ /g'`
144       [ "$servers" ]  &&  config_smtpservers="$servers"
145     fi
146   fi
147
148   if $ldd "$mhbin/inc" | grep sasl >/dev/null; then
149     config_sasl=y
150   fi
151
152   if $ldd "$mhbin/inc" | grep ssl >/dev/null; then
153     config_tls=y
154   fi
155 fi
156
157 if [ "$EDITOR" ]; then
158   config_editor="$EDITOR"
159 elif [ "$VISUAL" ]; then
160   config_editor="$VISUAL"
161 fi
162
163 [ "$PAGER" ]  &&  config_pager="$PAGER"
164
165 [ $debug -ge 1 ]  &&  config_debug=y
166
167 if [ $yes -eq 0 ]; then
168   #### Confirm each config setting with user.
169   printf 'Install prefix [%s]: ' $config_prefix
170   read prefix
171   [ "$prefix" ]  &&  config_prefix="$prefix"
172
173   printf 'Locking type (dot|fcntl|flock|lockf) [determined by configure]: '
174   read locking
175   [ "$locking" ]  &&  config_locking="$locking"
176
177   printf 'MTS (smtp|sendmail/smtp|sendmail/pipe) [%s]: ' $config_mts
178   read mts
179   [ "$mts" ]  &&  config_mts="$mts"
180
181   if [ "$config_mts" = smtp ]; then
182     printf 'SMTP server(s), space separated [%s]: ' $config_smtpservers
183     read response
184     servers=`echo $response | sed -e 's/ /\\\ /g'`
185     [ "$servers" ]  &&  config_smtpservers="$servers"
186   fi
187
188   printf 'Cyrus SASL support [%s]: ' $config_sasl
189   read response
190   if [ "$response" = y  -o  "$response" = Y ]; then
191     config_sasl=y
192   elif [ "$response" = n  -o  "$response" = N ]; then
193     config_sasl=n
194   fi
195
196   printf 'TLS support [%s]: ' $config_tls
197   read response
198   if [ "$response" = y  -o  "$response" = Y ]; then
199     config_tls=y
200   elif [ "$response" = n  -o  "$response" = N ]; then
201     config_tls=n
202   fi
203
204   printf 'Default editor [%s]: ' $config_editor
205   read editor
206   [ "$editor" ]  &&  config_editor="$editor"
207
208   printf 'Pager [%s]: ' $config_pager
209   read pager
210   [ "$pager" ]  &&  config_pager="$pager"
211
212   #### Don't confirm debug here:  obey the -d option to this script.
213 fi
214
215 smtpservers=
216 config_opts="--prefix=$config_prefix"
217
218 [ "$config_locking" ]  &&  \
219   config_opts="$config_opts --with-locking=$config_locking"
220 [ "$config_mts"  -a  "$config_mts" != smtp ]  &&  \
221   config_opts="$config_opts --with-mts=$config_mts"
222 [ "$config_smtpservers"  -a  "$config_smtpservers" != localhost ]  &&  \
223   smtpservers="--with-smtpservers=$config_smtpservers"
224 [ "$config_sasl" = y ]  &&  \
225   config_opts="$config_opts --with-cyrus-sasl"
226 [ "$config_tls" = y ]  &&  \
227   config_opts="$config_opts --with-tls"
228 [ "$config_editor" ]  &&  \
229   config_opts="$config_opts --with-editor=$config_editor"
230 [ "$config_pager" ]  &&  \
231   config_opts="$config_opts --with-pager=$config_pager"
232 [ $config_debug = y ]  &&  \
233   config_opts="$config_opts --enable-debug"
234
235
236 #### dotlocking, the usual default, requires chgrp and chmod of inc.
237 installpriv=
238 if [ $install -ge 1  -a  `id -u` -ne 0 ]; then
239   if [ "$config_locking" = dot ]; then
240     echo "$0: "'install requires chgrp and chmod 2755'
241     echo 'so will sudo to install.  Terminate with Ctrl-C if unacceptable.'
242     installpriv=sudo
243   fi
244 fi
245
246
247 ####
248 #### Clean up, and set up with autoconfig if necessary.
249 ####
250 if [ -f Makefile ]; then
251   [ $verbose -ge 1 ]  &&  echo cleaning . . .
252   if [ $superclean -ge 1 ]; then
253     make superclean >/dev/null
254   else
255     make distclean >/dev/null
256   fi
257 fi
258
259 /bin/rm -f "$logfile"
260 if [ -f configure  -a  -f Makefile.in ]; then
261   :
262 else
263   [ $verbose -ge 1 ]  &&  echo autoconfiguring . . .
264   ./autogen.sh >>"$logfile" 2>&1
265 fi
266
267
268 ####
269 #### Build.
270 ####
271 [ $verbose -ge 1 ]  &&  echo configuring . . .
272 echo ./configure $config_opts ${smtpservers:+"$smtpservers"} >>"$logfile" 2>&1
273 ./configure $config_opts ${smtpservers:+"$smtpservers"} >>"$logfile" 2>&1
274 status=$?
275
276 if [ $status -eq 0 ]; then
277   [ $verbose -ge 1 ]  &&  echo building . . .
278   make >>"$logfile" 2>&1
279   status=$?
280
281   if [ $status -eq 0 ]; then
282     if [ "$TESTS_SHELL"x = x ]; then
283       #### Bonus:  use heirloom shell to test, if available, and if
284       #### TESTS_SHELL hadn't already been set.
285       heirloom_shell=/usr/lib/heirloom/5bin/sh
286       if [ -x "$heirloom_shell" ]; then
287         TESTS_SHELL="$heirloom_shell"; export TESTS_SHELL
288       fi
289    fi
290
291     [ $verbose -ge 1 ]  &&  echo testing . . .
292     checkoutput=`make $check 2>>"$logfile"`
293     status=$?
294
295     tests_summary=`echo "$checkoutput" | grep tests`
296     #### If multiple tests not run, that line will be caught by the
297     #### "grep tests" above.
298     test_not_run=`echo "$checkoutput" | grep 'test was not run'`
299     if [ "$tests_summary" ]; then
300       echo '===================' >>"$logfile"
301       echo "$tests_summary" >>"$logfile"
302       [ "$test_not_run" ]  &&  echo "$test_not_run" >>"$logfile"
303       echo '===================' >>"$logfile"
304       [ "$check" = distcheck ]  &&  \
305         echo "$checkoutput" | tail -n 4 >>"$logfile"
306     fi
307
308     if [ $status -eq 0 ]; then
309       if [ $install -ge 1 ]; then
310         [ $verbose -ge 1 ]  &&  echo installing . . .
311         ($installpriv make install) >/dev/null 2>>"$logfile"
312         status=$?
313       fi
314
315       if [ $status -eq 0  -a  $build_rpm -ge 1 ]; then
316         [ $verbose -ge 1 ]  &&  echo building rpm . . .
317         make rpm >/dev/null 2>>"$logfile"
318         status=$?
319       fi
320     fi
321   fi
322 fi
323
324 grep 'Error' "$logfile"
325 grep 'warn' "$logfile"
326 if [ $status -ne 0 ]; then
327   echo build failed!
328   echo build log is in "$logfile"
329 fi
330 [ $status -eq 0  -a  $verbose -ge 1 ]  &&  echo build completed successfully
331
332 exit $status