2f70c6e35fa99b740c5eca9575f5937d2e6acb34
[exim.git] / src / scripts / exim_install
1 #! /bin/sh
2 # $Cambridge: exim/src/scripts/exim_install,v 1.2 2005/09/06 13:18:13 ph10 Exp $
3
4 # Script to install Exim binaries in BIN_DIRECTORY, which is defined in
5 # the local Makefile. It expects to be run in a build directory. It needs
6 # to be run as root in order to make exim setuid to root. If exim runs setuid
7 # to (e.g.) exim, this script should be run as that user or root.
8
9 # This script also installs a default configuration file in CONFIGURE_FILE
10 # if there is no configuration file there, but only if CONFIGURE_FILE specifies
11 # single file. If it specifies a list, no action is taken.
12
13 # If a default configuration file is installed, the existence of the system
14 # aliases file is tested. A default, containing only comments, is installed if
15 # necessary.
16
17 # If INFO_DIRECTORY is defined in any of the local Makefiles, and the Exim doc
18 # directory contains the Texinfo documentation, this script also installs a
19 # the info files in INFO_DIRECTORY.
20
21 # If DESTDIR is defined, all file paths are prefixed with ${DESTDIR}, with the
22 # sole exception of the reference to the system aliases file in the default
23 # configuration, because it is assumed that Exim is not actually going to be
24 # run from this position. For backward compatibility, if DESTDIR is not
25 # defined, ROOT is used instead.
26
27 # The script can be made to output what it would do, without actually doing
28 # anything, by giving it the option "-n" (cf make). Arguments are the names
29 # of things to install. No arguments installs everything.
30
31 do_chown=yes
32 do_symlink=yes
33
34 while [ $# -gt 0 ] ; do
35   case "$1" in
36     -n)
37       real="true || "
38       ver="verification "
39       com=": "
40       echo $com ""
41       echo $com "*** Verification mode only: no commands will actually be obeyed"
42       echo $com "*** You can cut and paste the bits you want to a shell, etc"
43       echo $com ""
44       echo cd `pwd`
45       ;;
46
47     -no_chown)
48       do_chown=no
49       ;;
50
51     -no_symlink)
52       do_symlink=no
53       ;;
54
55     *)
56       break
57       ;;
58   esac
59   shift
60 done
61
62 # Get the values of BIN_DIRECTORY, CONFIGURE_FILE, INFO_DIRECTORY, NO_SYMLINK,
63 # SYSTEM_ALIASES_FILE, and EXE from the global Makefile (in the build
64 # directory). EXE is empty except in the Cygwin environment. In each case, keep
65 # the latest definition, thus respecting the Makefiles precedence. The sed
66 # sequences here are messy, but have to be very "basic" in order to work on
67 # Solaris, where the regular expressions in sed are primitive indeed. Modify at
68 # your peril.
69
70 BIN_DIRECTORY=`sed -n   -e '/^ *BIN_DIRECTORY *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
71 CONFIGURE_FILE=`sed -n -e '/^ *CONFIGURE_FILE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
72 INFO_DIRECTORY=`sed -n -e '/^ *INFO_DIRECTORY *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
73 NO_SYMLINK=`sed -n         -e '/^ *NO_SYMLINK *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
74
75 CHOWN=`sed -n           -e '/^ *CHOWN_COMMAND *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
76 MV=`sed -n                 -e '/^ *MV_COMMAND *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
77
78 SYSTEM_ALIASES_FILE=`sed -n -e '/^ *SYSTEM_ALIASES_FILE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
79 EXE=`sed -n                                 -e '/^ *EXE *=/{s/^[^=]*= *//; s/ \{1,\}#.*//;s/ *$//;h;}' -e '${g;p;}' Makefile`
80
81 # Set a default for SYSTEM_ALIASES_FILE
82
83 if [ "${SYSTEM_ALIASES_FILE}" = "" ] ; then
84   SYSTEM_ALIASES_FILE=/etc/aliases
85 fi
86
87 # Allow INST_xx to over-ride xx
88 case "$INST_BIN_DIRECTORY"       in ?*) BIN_DIRECTORY="$INST_BIN_DIRECTORY";; esac
89 case "$INST_CONFIGURE_FILE"      in ?*) CONFIGURE_FILE="$INST_CONFIGURE_FILE";; esac
90 case "$INST_INFO_DIRECTORY"      in ?*) INFO_DIRECTORY="$INST_INFO_DIRECTORY";; esac
91 case "$INST_SYSTEM_ALIASES_FILE" in ?*) SYSTEM_ALIASES_FILE="$INST_SYSTEM_ALIASES_FILE";; esac
92
93 case "$INST_CHOWN"               in ?*) CHOWN="$INST_CHOWN";; esac
94 case "$INST_MV"                  in ?*) MV="$INST_MV";; esac
95
96 case "$INST_UID"     in '') INST_UID=root;;    *) INST_UID="$INST_UID";; esac
97 case "$INST_CP"      in '') CP=cp;;            *) CP="$INST_CP";; esac
98 case "$INST_LN"      in '') LN=ln;;            *) LN="$INST_LN";; esac
99 case "$INST_CHMOD"   in '') CHMOD=chmod;;      *) CHMOD="$INST_CHMOD";; esac
100 case "$INST_DIRNAME" in '') DIRNAME=dirname;;  *) DIRNAME="$INST_DIRNAME";; esac
101 case "$INST_MKDIR"   in '') MKDIR=mkdir;;      *) MKDIR="$INST_MKDIR";; esac
102
103 # Allow the user to over-ride xx
104 case "$inst_dest"    in ?*) BIN_DIRECTORY="$inst_dest";; esac
105 case "$inst_conf"    in ?*) CONFIGURE_FILE="$inst_conf";; esac
106 case "$inst_info"    in ?*) INFO_DIRECTORY="$inst_info";; esac
107 case "$inst_aliases" in ?*) SYSTEM_ALIASES_FILE="$inst_aliases";; esac
108
109 # Insert ${DESTDIR} at the start of all paths so that the whole thing can be
110 # installed under a different file root. For backwards compatibility, use
111 # ${ROOT} if ${DESTDIR} is not set. However, we need to save the value of
112 # the real system aliases file, and use that in the default configuration.
113
114 ACTUAL_SYSTEM_ALIASES_FILE=${SYSTEM_ALIASES_FILE}
115 DESTDIR=${DESTDIR:-${ROOT}}
116
117 BIN_DIRECTORY=${DESTDIR}${BIN_DIRECTORY}
118 CONFIGURE_FILE=${DESTDIR}${CONFIGURE_FILE}
119 SYSTEM_ALIASES_FILE=${DESTDIR}${SYSTEM_ALIASES_FILE}
120
121 if [ "${INFO_DIRECTORY}" != "" ] ; then
122   INFO_DIRECTORY=${DESTDIR}${INFO_DIRECTORY}
123 fi
124
125 # Overrides of other things
126 case "$inst_uid"     in ?*) INST_UID="$inst_uid";; esac
127 case "$inst_cp"      in ?*) CP="$inst_cp";; esac
128 case "$inst_mv"      in ?*) MV="$inst_mv";; esac
129 case "$inst_ln"      in ?*) LN="$inst_ln";; esac
130 case "$inst_chown"   in ?*) CHOWN="$inst_chown";; esac
131 case "$inst_chmod"   in ?*) CHMOD="$inst_chmod";; esac
132 case "$inst_dirname" in ?*) DIRNAME="$inst_dirname";; esac
133 case "$inst_mkdir"   in ?*) MKDIR="$inst_mkdir";; esac
134
135 # chown is a special case; in at least one OS it is in /usr/etc instead
136 # of in /usr/bin, and therefore not likely to be on the path. Another OS
137 # has it in /usr/sbin. This fudge tries to cope with these variations.
138
139 # Otherwise, and for other commands, we assume that the normal PATH will
140 # give access to where they are on your operating system (normally /usr/bin
141 # or /bin).
142
143 if [ "${CHOWN}" = "chown" -a -x /usr/sbin/chown ] ; then
144   CHOWN=/usr/sbin/chown
145 fi
146
147 if [ "${CHOWN}" = "chown" -a ! -f /usr/bin/chown -a -f /usr/etc/chown ] ; then
148   CHOWN=/usr/etc/chown
149 fi
150
151 # The values of CHOWN and MV taken from the Makefile are sometimes set to
152 # "look_for_it", which causes a search of the usual suspects. This code is
153 # similar to that in exicyclog, but has to be fudged for upper/lower case
154 # distinctions.
155
156 for cmd in CHOWN MV ; do
157   eval "oldcmd=\$$cmd"
158   if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
159   if [ "$cmd" = "CHOWN" ] ; then cmdlc="chown" ; fi
160   if [ "$cmd" = "MV" ] ; then cmdlc="mv" ; fi
161   newcmd=$cmdlc
162   for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
163     if [ -f $dir/$cmdlc ] ; then
164       newcmd=$dir/$cmdlc
165       break
166     fi
167   done
168   eval $cmd=$newcmd
169 done
170
171 # See if the exim monitor has been built
172
173 if [ -f eximon -a -f eximon.bin ]; then
174   exim_monitor="eximon eximon.bin"
175 fi
176
177 # If bin directory doesn't exist, try to create it
178
179 if [ ! -d "${BIN_DIRECTORY}" ]; then
180   echo mkdir -p ${BIN_DIRECTORY}
181   ${real} mkdir -p ${BIN_DIRECTORY}
182   if [ $? -ne 0 ]; then
183     echo $com ""
184     echo $com "*** Exim installation ${ver}failed ***"
185     exit 1
186   else
187     ${real} echo $com ${BIN_DIRECTORY} created
188   fi
189 fi
190
191 # If no arguments, install everything
192
193 if [ $# -gt 0 ]; then
194   set $@
195 else
196   set exim${EXE} ${exim_monitor} exim_dumpdb${EXE} exim_fixdb${EXE} \
197       exim_tidydb${EXE} exinext exiwhat exim_dbmbuild${EXE} exicyclog \
198       exigrep eximstats exipick exiqgrep exiqsumm exim_lock${EXE} \
199       exim_checkaccess
200 fi
201
202 echo $com ""
203 echo $com Installation directory is ${BIN_DIRECTORY}
204 echo $com ""
205
206 while [ $# -gt 0 ]; do
207   name=$1
208   shift
209
210   if [ ! -s ${name} ]; then
211     echo $com ""
212     echo $com "*** `pwd`/${name} does not exist or is empty"
213     echo $com "*** Have you built Exim successfully?"
214     echo $com "*** Exim installation ${ver}failed ***"
215     exit 1
216   fi
217
218   # The exim binary is handled specially
219
220   if [ $name = exim${EXE} ]; then
221     version=exim-`./exim -bV -C /dev/null | \
222       awk '/Exim version/ { OFS=""; print $3,"-",substr($4,2,length($4)-1) }'`${EXE}
223
224     if [ "${version}" = "exim-${EXE}" ]; then
225       echo $com ""
226       echo $com "*** Could not run ./exim to find version number ***"
227       echo $com "*** Exim installation ${ver}failed ***"
228       exit 1
229     fi
230
231     # Do something only if newer than existing file, or no existing file
232
233     if ../scripts/newer ${name} ${BIN_DIRECTORY}/${version}; then
234       echo ${CP} ${name} ${BIN_DIRECTORY}/${version}
235       ${real} ${CP} ${name} ${BIN_DIRECTORY}/${version}
236       if [ $? -ne 0 ]; then
237         echo $com ""
238         echo $com "*** Exim installation ${ver}failed ***"
239         exit 1
240       fi
241
242       # After copy, set ownership and permissions, unless disabled
243
244       if [ "$do_chown" != "no" ]; then
245         echo ${CHOWN} ${INST_UID} ${BIN_DIRECTORY}/${version}
246         ${real} ${CHOWN} ${INST_UID} ${BIN_DIRECTORY}/${version}
247         if [ $? -ne 0 ]; then
248           echo $com ""
249           echo $com "*** You must be ${INST_UID} to install exim ***"
250           exit 1
251         fi
252         echo ${CHMOD} a+x ${BIN_DIRECTORY}/${version}
253         ${real} ${CHMOD} a+x ${BIN_DIRECTORY}/${version}
254         if [ $? -ne 0 ]; then
255           echo $com ""
256           echo $com "*** Exim installation ${ver}failed ***"
257           exit 1
258         fi
259         echo ${CHMOD} u+s ${BIN_DIRECTORY}/${version}
260         ${real} ${CHMOD} u+s ${BIN_DIRECTORY}/${version}
261         if [ $? -ne 0 ]; then
262           echo $com ""
263           echo $com "*** Exim installation ${ver}failed ***"
264           exit 1
265         fi
266       else
267         echo $com "$CHOWN $INST_UID omitted: -no_chown was specified"
268         echo $com "$CHMOD u+s omitted: -no_chown was specified"
269       fi
270
271       # Now sort out the "exim" alias, unless NO_SYMLINK is set.
272
273       if [ "X$NO_SYMLINK" = "X" ] && [ "$do_symlink" != "no" ] ; then
274
275         #  First check whether "exim" exists in the directory.
276         if [ -f ${BIN_DIRECTORY}/exim ]; then
277
278           # If it's not a symbolic link, make a copy with the old version number
279           if [ `ls -l ${BIN_DIRECTORY}/exim | cut -c1-1` != 'l' ]; then
280             oldversion=exim-`${BIN_DIRECTORY}/exim -bV -C /dev/null | \
281               awk '/Exim version/ { OFS=""; print $3,"-",substr($4,2,length($4)-1) }'`${EXE}
282             if [ "${version}" = "${oldversion}" ] ; then
283               echo $com ""
284               echo $com "*** Existing file called exim has the same version and compile number ***"
285               echo $com "*** Exim installation ${ver}failed ***"
286               exit 1
287             fi
288             echo ${CP} ${BIN_DIRECTORY}/exim ${BIN_DIRECTORY}/${oldversion}
289             ${real} ${CP} ${BIN_DIRECTORY}/exim ${BIN_DIRECTORY}/${oldversion}
290             if [ $? -ne 0 ]; then
291               echo $com ""
292               echo $com "*** Exim installation ${ver}failed ***"
293               exit 1
294             fi
295           fi
296
297           # Now we can move the name "exim" to be a symbolic link to the new
298           # version, atomically.
299
300           echo \(cd ${BIN_DIRECTORY}\; ${LN} -s ${version} temporary_exim\)
301           (${real} cd ${BIN_DIRECTORY}; ${real} ${LN} -s ${version} temporary_exim)
302           if [ $? -ne 0 ]; then
303             echo $com ""
304             echo $com "*** Exim installation ${ver}failed ***"
305             exit 1
306           fi
307
308           echo ${MV} -f ${BIN_DIRECTORY}/temporary_exim ${BIN_DIRECTORY}/exim
309           ${real} ${MV} -f ${BIN_DIRECTORY}/temporary_exim ${BIN_DIRECTORY}/exim
310           if [ $? -ne 0 ]; then
311             echo $com ""
312             echo $com "*** Exim installation ${ver}failed ***"
313             exit 1
314           fi
315
316         # If "exim" does not already exist just create a symbolic link.
317
318         else
319           echo \(cd ${BIN_DIRECTORY}\; ${LN} -s ${version} exim\)
320           (${real} cd ${BIN_DIRECTORY}; ${real} ${LN} -s ${version} exim)
321           if [ $? -ne 0 ]; then
322             echo $com ""
323             echo $com "*** Exim installation ${ver}failed ***"
324             exit 1
325           fi
326         fi
327
328       else
329         echo $com "creation of symlink omitted"
330         if [ "X$NO_SYMLINK" != "X" ] ; then
331           echo $com "(NO_SYMLINK is specified in Local/Makefile)"
332         else
333           echo $com "(-no_symlink was specified)"
334         fi
335       fi
336
337     # New binary is not newer than the installed file
338
339     else
340       echo $com ${name} is not newer than ${BIN_DIRECTORY}/${version}
341     fi
342
343   # Handle everything other than the exim binary itself
344
345   else
346     if ../scripts/newer ${name} ${BIN_DIRECTORY}/${name}; then
347       if [ -f ${BIN_DIRECTORY}/${name} ]; then
348         echo ${CP} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
349         ${real} ${CP} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
350         if [ $? -ne 0 ]; then
351           echo $com ""
352           echo $com "*** Exim installation ${ver}failed ***"
353           exit 1
354         fi
355       fi
356       echo ${CP} ${name} ${BIN_DIRECTORY}
357       ${real} ${CP} ${name} ${BIN_DIRECTORY}
358       if [ $? -ne 0 ]; then
359         echo $com ""
360         echo $com "*** Exim installation ${ver}failed ***"
361         exit 1
362       fi
363     else
364       echo $com ${name} is not newer than ${BIN_DIRECTORY}/${name}
365     fi
366   fi
367
368 done
369
370
371
372 # If there is no configuration file, install the default, modifying it to refer
373 # to the configured system aliases file. If there is no setting for
374 # SYSTEM_ALIASES_FILE, use the traditional /etc/aliases. If the file does not
375 # exist, install a default (dummy) for that too.
376
377 # However, if CONFIGURE_FILE specifies a list of files, skip this code.
378
379 echo $com ""
380
381 if [ `expr "${CONFIGURE_FILE}" : ".*:"` -ne 0 ] ; then
382   echo $com Runtime configuration is specified as the following list:
383   echo $com ' ' ${CONFIGURE_FILE}
384   echo $com Therefore, skipping automatic installation.
385
386 elif [ ! -f ${CONFIGURE_FILE} ]; then
387   echo $com Installing default configuration in ${CONFIGURE_FILE}
388   echo $com because there is no existing configuration file.
389   if [ "${SYSTEM_ALIASES_FILE}" = "" ] ; then
390     SYSTEM_ALIASES_FILE=/etc/aliases
391     echo $com This configuration has system aliases in ${SYSTEM_ALIASES_FILE}.
392   fi
393
394   echo ${MKDIR} -p `${DIRNAME} ${CONFIGURE_FILE}`
395   ${real} ${MKDIR} -p `${DIRNAME} ${CONFIGURE_FILE}`
396
397   echo sed -e '\\'
398   echo "  \"/SYSTEM_ALIASES_FILE/ s'SYSTEM_ALIASES_FILE'${ACTUAL_SYSTEM_ALIASES_FILE}'\"" '\\'
399   echo "  ../src/configure.default > \${CONFIGURE_FILE}"
400
401   # I can't find a way of writing this using the ${real} feature because
402   # it seems that the output redirection always happens, even when -n was
403   # specified. So control it the hard way.
404
405   if [ "$real" = "" ] ; then
406     sed -e \
407       "/SYSTEM_ALIASES_FILE/ s'SYSTEM_ALIASES_FILE'${ACTUAL_SYSTEM_ALIASES_FILE}'" \
408       ../src/configure.default > ${CONFIGURE_FILE}
409   else
410     true
411   fi
412
413   if [ $? -ne 0 ]; then
414     echo $com ""
415     echo $com "*** Exim installation ${ver}failed ***"
416     exit 1
417   fi
418   if [ ! -f ${SYSTEM_ALIASES_FILE} ]; then
419     echo $com '****'
420     echo $com Installing a dummy ${SYSTEM_ALIASES_FILE} file because you do not have
421     echo $com one, and the default configuration requires it. You should
422     echo $com edit ${SYSTEM_ALIASES_FILE} and at least create an alias for postmaster.
423     echo $com '***'
424     echo ${CP} ../src/aliases.default ${SYSTEM_ALIASES_FILE}
425     ${real} ${CP} ../src/aliases.default ${SYSTEM_ALIASES_FILE}
426   fi
427
428 else
429   echo $com Configuration file ${CONFIGURE_FILE} already exists
430 fi
431
432 # Install info files if the directory is defined and the Texinfo
433 # source documentation is present.
434
435 if [ "${INFO_DIRECTORY}" != "" -a -f ../doc/spec.texinfo ] ; then
436   echo $com ""
437   if [ ! -d "${INFO_DIRECTORY}" ] ; then
438     echo mkdir -p ${INFO_DIRECTORY}
439     ${real} mkdir -p ${INFO_DIRECTORY}
440     if [ $? -ne 0 ]; then
441       echo $com ""
442       echo $com "*** Exim installation ${ver}failed ***"
443       exit 1
444     else
445       echo $com ${INFO_DIRECTORY} created
446     fi
447   fi
448
449   echo $com Info installation directory is ${INFO_DIRECTORY}
450   echo $com ""
451
452   ${real} makeinfo --no-split --output exim.info ../doc/spec.texinfo
453   echo ${CP} exim.info ${INFO_DIRECTORY}
454   ${real} ${CP} exim.info ${INFO_DIRECTORY}
455   ${real} install-info --section="Exim" \
456       --entry "* User guide: (exim).           Exim manual" \
457       ${INFO_DIRECTORY}/exim.info ${INFO_DIRECTORY}/dir
458   ${real} makeinfo --no-split --output exim_filter.info ../doc/filter.texinfo
459   echo ${CP} exim_filter.info ${INFO_DIRECTORY}
460   ${real} ${CP} exim_filter.info ${INFO_DIRECTORY}
461   ${real} install-info --section="Exim" \
462       --entry "* Filtering: (exim_filter).     Filtering mail with Exim" \
463       ${INFO_DIRECTORY}/exim_filter.info ${INFO_DIRECTORY}/dir
464 fi
465
466 # Everything OK
467
468 echo $com ""
469 echo $com Exim installation ${ver}complete
470
471 # End of exim_install