#! /bin/sh

# Script to install Exim binaries in BIN_DIRECTORY, which is defined in
# the local Makefile. It expects to be run in a build directory. It needs
# to be run as root in order to make exim setuid to root. If exim runs setuid
# to (e.g.) exim, this script should be run as that user or root.

# This script also installs a default configuration file in CONFIGURE_FILE
# if there is no configuration file there.

# The script can be made to output what it would do, without actually doing
# anything, by giving it the option "-n" (cf make). Arguments are the names
# of things to install. No arguments installs everything.

if [ "$1" = "-n" ]; then
  shift
  real="true || "
  ver="verification "
  com=": "
  echo $com ""
  echo $com "*** Verification mode only: no commands will actually be obeyed"
  echo $com ""
  echo $com "You can cut and paste the bits you want to a shell, etc"
  echo $com ""
  echo cd `pwd`
fi

# First off, get the OS type, and check that there is a make file for it.

os=`../scripts/os-type` || exit 1

if	test ! -r ../OS/Makefile-$os
then    echo ""
	echo "*** Sorry - operating system $os is not supported"
        echo "*** See OS/Makefile-* for supported systems" 1>&2
        echo ""
	exit 1;
fi

# We also need the architecture type, in order to test for any architecture-
# specific configuration files.

arch=`../scripts/arch-type` || exit 1

# Get the values of BIN_DIRECTORY and CONFIGURE_FILE from the relevant
# makefiles in the Local directory. Such make files might contain
# settings that have spaces in them for other parameters, and these
# confuse the shell if a straight sourcing operation is done. Pity that
# make and sh are incompatible in this way. Instead we have to seek out
# the values we want explicitly.

Local_Makefile=${LOCAL_MAKEFILE-../Local/Makefile}
files=${Local_Makefile}

if [ -f ${Local_Makefile}-$os ] ; then
  files="$files ${Local_Makefile}-$os"
fi

if [ -f ${Local_Makefile}-$arch ] ; then
  files="$files ${Local_Makefile}-$arch"
fi

if [ -f ${Local_Makefile}-$os-$arch ] ; then
  files="$files ${Local_Makefile}-$os-$arch"
fi

BIN_DIRECTORY=`grep "^ *BIN_DIRECTORY" $files | tail -1 | cut -f2-99 -d: | cut -c15-99`
CONFIGURE_FILE=`grep "^ *CONFIGURE_FILE" $files | tail -1 | cut -f2-99 -d: | cut -c16-99`

# Allow INST_xx to over-ride xx
case "$INST_BIN_DIRECTORY" in ?*) BIN_DIRECTORY="$INST_BIN_DIRECTORY";; esac
case "$INST_CONFIGURE_FILE" in ?*) CONFIGURE_FILE="$INST_CONFIGURE_FILE";; esac
case "$INST_CP" in '') CP=cp;; *) CP="$INST_CP";; esac
case "$INST_MV" in '') MV=mv;; *) MV="$INST_MV";; esac
case "$INST_UID" in '') INST_UID=root;; *) INST_UID="$INST_UID";; esac

# Allow the user to over-ride xx
case "$inst_dest" in ?*) BIN_DIRECTORY="$inst_dest";; esac
case "$inst_conf" in ?*) CONFIGURE_FILE="$inst_conf";; esac
case "$inst_cp"   in ?*) CP="$inst_cp";; esac
case "$inst_mv"   in ?*) MV="$inst_mv";; esac
case "$inst_uid"  in ?*) INST_UID="$inst_uid";; esac

# See if the exim monitor has been built

if [ -f eximon -a -f eximon.bin ]; then
  exim_monitor="eximon eximon.bin"
fi

# If bin directory doesn't exist, try to create it

if [ ! -d ${BIN_DIRECTORY} ]; then
  echo mkdir -p ${BIN_DIRECTORY}
  ${real} mkdir -p ${BIN_DIRECTORY}
  if [ $? -ne 0 ]; then
    echo $com ""
    echo $com "**** Exim installation ${ver}failed ****"
    exit 1
  else
    echo $com ${BIN_DIRECTORY} created
  fi
fi

# If no arguments, install everything

if [ $# -gt 0 ]; then
  set $@
else
  set exim ${exim_monitor} exim_dumpdb exim_fixdb exim_tidydb \
      exinext exiwhat exim_dbmbuild exicyclog exigrep eximstats
fi

echo $com ""
echo $com Installation directory is ${BIN_DIRECTORY}
echo $com ""

while [ $# -gt 0 ]; do
  name=$1
  shift
  from=
  if [ $name = exigrep -o $name = eximstats ]; then
    from=../util/
  fi
  if ../scripts/newer ${from}${name} ${BIN_DIRECTORY}/${name}; then
    if [ -f ${BIN_DIRECTORY}/${name} ]; then
      echo ${MV} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
      ${real} ${MV} ${BIN_DIRECTORY}/${name} ${BIN_DIRECTORY}/${name}.O
      if [ $? -ne 0 ]; then
        echo $com ""
        echo $com "**** Exim installation ${ver}failed ****"
        exit 1
      fi
    fi
    echo ${CP} ${from}${name} ${BIN_DIRECTORY}
    ${real} ${CP} ${from}${name} ${BIN_DIRECTORY}
    if [ $? -ne 0 ]; then
      echo $com ""
      echo $com "**** Exim installation ${ver}failed ****"
      exit 1
      fi
    if [ $name = exim ]; then
      echo chown ${INST_UID} ${BIN_DIRECTORY}/exim
      ${real} chown ${INST_UID} ${BIN_DIRECTORY}/exim
      if [ $? -ne 0 ]; then
        echo $com ""
        echo $com "**** You must be ${INST_UID} to install exim ****"
        exit 1
      fi
      echo chmod a+x ${BIN_DIRECTORY}/exim
      ${real} chmod a+x ${BIN_DIRECTORY}/exim
      if [ $? -ne 0 ]; then
        echo $com ""
        echo $com "**** Exim installation ${ver}failed ****"
        exit 1
      fi
      echo chmod u+s ${BIN_DIRECTORY}/exim
      ${real} chmod u+s ${BIN_DIRECTORY}/exim
      if [ $? -ne 0 ]; then
        echo $com ""
        echo $com "**** Exim installation ${ver}failed ****"
        exit 1
      fi
    fi
  else
    echo $com ${name} is not newer than ${BIN_DIRECTORY}/${name}
  fi
done

# If there is no configuration file, install the default,
# building the lib directory if necessary.

echo $com ""

if [ ! -f ${CONFIGURE_FILE} ]; then
  echo $com Installing default configuration in ${CONFIGURE_FILE}
  echo $com because there is no existing configuration file.
  echo ${CP} ../src/configure.default ${CONFIGURE_FILE}
  ${real} ${CP} ../src/configure.default ${CONFIGURE_FILE}
  if [ $? -ne 0 ]; then
    echo $com ""
    echo $com "**** Exim installation ${ver}failed ****"
    exit 1
  fi
else
  echo $com Configuration file ${CONFIGURE_FILE} already exists
fi

echo $com ""
echo $com Exim installation ${ver}complete

# End of exim_install
