#! /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.

# If INFO_DIRECTORY is defined in any of the local Makefiles, and the Exim doc
# directory contains the Texinfo documentation, this script also installs a
# the info files in INFO_DIRECTORY.

# 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 -generic` || 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`
INFO_DIRECTORY=`grep "^ *INFO_DIRECTORY=" $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_INFO_DIRECTORY" in ?*) INFO_DIRECTORY="$INST_INFO_DIRECTORY";; esac
case "$INST_UID"   in '') INST_UID=root;; *) INST_UID="$INST_UID";; esac
case "$INST_CP"    in '') CP=cp;;         *) CP="$INST_CP";; esac
case "$INST_MV"    in '') MV=mv;;         *) MV="$INST_MV";; esac
case "$INST_CHOWN" in '') CHOWN=chown;;   *) CHOWN="$INST_CHOWN";; esac
case "$INST_CHMOD" in '') CHMOD=chmod;;   *) CHMOD="$INST_CHMOD";; 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_info"  in ?*) INFO_DIRECTORY="$inst_info";; esac
case "$inst_uid"   in ?*) INST_UID="$inst_uid";; esac
case "$inst_cp"    in ?*) CP="$inst_cp";; esac
case "$inst_mv"    in ?*) MV="$inst_mv";; esac
case "$inst_chown" in ?*) CHOWN="$inst_chown";; esac
case "$inst_chmod" in ?*) CHMOD="$inst_chmod";; esac

# chown is a special case; in at least one OS it is in /usr/etc instead
# of in /usr/bin, and therefore not likely to be on the path. Another OS
# has it in /usr/sbin. This fudge tries to cope with these variations.

if [ "${CHOWN}" = "chown" -a -x /usr/sbin/chown ] ; then
  CHOWN=/usr/sbin/chown
fi

if [ "${CHOWN}" = "chown" -a ! -f /usr/bin/chown -a -f /usr/etc/chown ] ; then
  CHOWN=/usr/etc/chown
fi

# 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 \
      exiqsumm exim_lock
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 -o $name = exiqsumm ]; then
    from=../util/
  fi

  if [ ! -s ${from}${name} ]; then
    echo $com ""
    echo $com "*** `pwd`/${from}${name} does not exist or is empty"
    echo $com "*** Have you built Exim successfully?"
    echo $com "*** Exim installation ${ver}failed ***"
    exit 1
  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.

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

# Install info files if the directory is defined and the Texinfo
# source documentation is present.

if [ "${INFO_DIRECTORY}" != "" -a -f ../doc/spec.texinfo ] ; then
  echo $com ""
  if [ ! -d "${INFO_DIRECTORY}" ] ; then
    echo mkdir -p ${INFO_DIRECTORY}
    ${real} mkdir -p ${INFO_DIRECTORY}
    if [ $? -ne 0 ]; then
      echo $com ""
      echo $com "*** Exim installation ${ver}failed ***"
      exit 1
    else
      echo $com ${INFO_DIRECTORY} created
    fi
  fi

  echo $com Info installation directory is ${INFO_DIRECTORY}
  echo $com ""

  makeinfo --no-split --output exim_overview.info ../doc/oview.texinfo
  echo ${CP} exim_overview.info ${INFO_DIRECTORY}
  ${real} ${CP} exim_overview.info ${INFO_DIRECTORY}
  install-info --section="Exim" \
      --entry "* Exim Overview: (exim_overview).   Overview of the Exim system" \
      ${INFO_DIRECTORY}/exim_overview.info ${INFO_DIRECTORY}/dir
  makeinfo --no-split --output exim.info ../doc/spec.texinfo
  echo ${CP} exim.info ${INFO_DIRECTORY}
  ${real} ${CP} exim.info ${INFO_DIRECTORY}
  install-info --section="Exim" \
      --entry "* Exim Specification: (exim).       Exim manual" \
      ${INFO_DIRECTORY}/exim.info ${INFO_DIRECTORY}/dir
  makeinfo --no-split --output exim_filter.info ../doc/filter.texinfo
  echo ${CP} exim_filter.info ${INFO_DIRECTORY}
  ${real} ${CP} exim_filter.info ${INFO_DIRECTORY}
  install-info --section="Exim" \
      --entry "* Exim Filtering: (exim_filter).    Filtering mail with Exim" \
      ${INFO_DIRECTORY}/exim_filter.info ${INFO_DIRECTORY}/dir
fi

# Everything OK

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

# End of exim_install
