#! /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 it runs setuid to (e.g.) exim, it 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   

# Get the values of BIN_DIRECTORY and CONFIGURE_FILE

. ${LOCAL_MAKEFILE-../Local/Makefile}

# 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 "$dest" in ?*) BIN_DIRECTORY="$dest";; esac
case "$conf" in ?*) CONFIGURE_FILE="$conf";; esac
case "$cp" in ?*) CP="$cp";; esac
case "$mv" in ?*) MV="$mv";; esac
case "$uid" in ?*) INST_UID="$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
