#! /bin/sh
#
# exim-config, Copyright Pierre A. Humblet, 2003-2007.
#
# This file is part of the Cygwin port of exim.

#set -x

# ======================================================================
# Routine: getvalue
# Get a verified non-empty string in variable "value"
# Prompt with the first argument.
# The 2nd argument if not empty must be -s.
# ======================================================================
getvalue() {
  while true
    do
    if read $2 -p "$1 " value; then
      [ -n "$2" ] && echo 
      if [ -n "${value}" ]; then
          read $2 -p "Reenter: " verify
          [ -n "$2" ] && echo
          [ "${verify}" = "${value}" ] && break
      fi
    else
      echo -e "Quitting.\n"
      exit 1
    fi
  done
  echo
  return 0

} # === End of getvalue() === #

#############################
# Useful subroutine
#############################
request()
{
  while true
  do
    echo -n "$1 (yes/no) "
    if read answer; then
      if [ "${answer}" = "yes" ]; then
	return 0
      elif [ "${answer}" = "no" ]; then
        return 1
      fi
    else
      echo -e "Quitting.\n"
      exit 1
    fi
  done
}
#############################
# Useful subroutine
#############################
getperiod()
{
  while true
    do
    echo -n "Enter the period of the queue runner, in minutes: [15] "
    if read period; then
      if [ -z "${period}" ]; then 
        period=15
	return 0;
      elif echo "${period}" | grep -q '[^0-9]'; then
        echo "The period must be all digits."
      elif [ "${period}" -eq 0 ]; then
        echo "The period must be positive."
      else
        return 0
      fi
    else
      echo -e "Quitting.\n"
      exit 1
    fi
  done
}
#############################
# Useful subroutine
#############################
getcygenv()
{
  default=$*
  while true
    do
    echo -n "Enter the value of CYGWIN for the daemon: [${default}] "
    if read cygenv; then
      if [ -z "${cygenv}" ]; then 
	cygenv="${default}"
      fi
      failed=0
      for word in ${cygenv}
	do
	[ "$failed" = 0 ] &&
	failed=1
	echo "$word" | grep -q '\(no\)\?\(check_case:strict\)\|\(ntsec\)\|\(smbntsec\)\|\(traverse\)' &&
	failed=0
      done
      [ "$failed" = 0 ] && return
      echo "ERROR: Only [no] \"check_case:strict\" \"ntsec\" \"smbntsec\" \"traverse\" allowed."
    else
      echo -e "Quitting.\n"
      exit 1
    fi
  done
}

# ======================================================================
# Routine: create_user
# Create a privileged user on Windows 2003
# Returns the name in variable "username" and the password in "password"
# ======================================================================
create_user() {
    SYSCONFDIR="/etc"
    first_account=""
    accounts=""
    ret=0

    for username in cyg_server cron_server sshd_server
    do
      if net user "${username}" 1> /dev/null 2>&1; then
	  [ -z "${first_account}" ] && first_account="${username}"
	  accounts="${accounts}'${username}' "
      fi
    done
    if [ -n "${accounts}" ]; then
	echo "The following accounts were found: ${accounts}."
	username="${first_account}"
    else
	echo "No privileged account could be found."
	username="cyg_server"
    fi
    echo "This script plans to use ${username}."
    echo "The user will only be able to run as a service."
    if request "Do you want to use another name?"; then
	getvalue "Enter the new user name:" ""
	username="${value}"
    fi
    echo

    net user "${username}" >/dev/null 2>&1 && username_in_sam=yes
    if [ "$username_in_sam" != "yes" ]; then
	echo "User ${username} needs a password. It must match"
	echo "the rules in force on your system."
	getvalue "Please enter the password:" "-s"
	password="${value}"
	echo

	net user "${username}" "${password}" /add /fullname:"Privileged server" /yes > /tmp/nu.$$ 2>&1 && username_in_sam=yes
	if [ "${username_in_sam}" != "yes" ]; then
	    echo "ERROR: Creating the user '${username}' failed!  Reason:"
	    cat /tmp/nu.$$
	    rm /tmp/nu.$$
	    echo
	    return 1
	else
	    echo "User '${username}' has been created with password '${password}'."
	    echo "If you change the password, please keep in mind to change the"
	    echo "password for the cron service, too."
	fi
	echo

	passwd_has_expiry_flags=`passwd -v | awk '/^passwd /{print ( $3 >= 1.5 ) ? "yes" : "no";}'`
	if [ "${passwd_has_expiry_flags}" != "yes" ]
	    then
	    echo "WARNING: User ${username} has password expiry set to system default."
	    echo "Please check that password never expires or set it to your needs."
	    echo
	elif ! passwd -e "${username}"
	    then
	    echo "WARNING: Setting password expiry for user ${username} failed!"
	    echo "Please check that password never expires or set it to your needs."
	    echo
	fi
    fi

    _admingroup="$( mkgroup -l | awk -F: '{if ( $2 == "S-1-5-32-544" ) print $1;}' )"
    if [ -z "${_admingroup}" ]; then
	echo "ERROR: Cannot obtain the Administrators group name from 'mkgroup -l'."
	echo
	ret=1
    elif net localgroup "${_admingroup}" | grep -Eiq "^${username}.?$"; then
	true
    else
	net localgroup "${_admingroup}" "${username}" /add > /dev/null 2>&1 && 
          username_in_admingroup=yes
	if [ "${username_in_admingroup}" != "yes" ]
	    then
	    echo "ERROR: Adding user ${username} to group ${_admingroup} failed!"
	    echo "Please add ${username} to group ${_admingroup} before"
	    echo "starting the sshd service!"
	    echo
	    ret=1
	fi
    fi

    if [ ! -x /usr/bin/editrights ]; then
	echo "WARNING: The 'editrights' program cannot be found or is not executable."
	echo"          Unable to insure that ${username} has the appropriate privileges."
	echo
    else
	editrights -a SeAssignPrimaryTokenPrivilege -u "${username}" &&
	editrights -a SeCreateTokenPrivilege -u "${username}" &&
	editrights -a SeDenyInteractiveLogonRight -u "${username}" &&
	editrights -a SeDenyNetworkLogonRight -u "${username}" &&
	editrights -a SeDenyRemoteInteractiveLogonRight -u "${username}" &&
	editrights -a SeIncreaseQuotaPrivilege -u "${username}" &&
	editrights -a SeServiceLogonRight -u "${username}" &&
	username_got_all_rights="yes"
	if [ "${username_got_all_rights}" != "yes" ]
	    then
	    echo "ERROR: Assigning the appropriate privileges to user '${username}' failed!"
	    echo
	    ret=1
	fi
    fi

    pwd_entry="$(mkpasswd -l -u "${username}" | 
      sed -e 's?\(^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\).*?\1/var/empty:/bin/false?')"

    grep -Eiq "^${username}:" "${SYSCONFDIR}/passwd" && username_in_passwd=yes &&
      grep -Fiq "${pwd_entry}" "${SYSCONFDIR}"/passwd && entry_in_passwd=yes
    if [ "${entry_in_passwd}" != "yes" ]; then
      if [ "${username_in_passwd}" = "yes" ]; then
        if [ -w "${SYSCONFDIR}" ]; then
	  temp="${SYSCONFDIR}/passwd.$$.tmp"
        elif [ -d "/tmp" -a -w "/tmp" ]; then
          temp="/tmp/passwd.$$.tmp"
        elif [ -d "${TMP}" -a -w "${TMP}" ]; then
          temp="${TMP}/passwd.$$.tmp"
        elif [ -d "${HOME}" -a -w "${HOME}" ]; then
          temp="${HOME}/passwd.$$.tmp"
        else
	  echo "ERROR: Cannot find writable temporary directory"
          return 1;
        fi
	grep -Ev "^${username}:" "${SYSCONFDIR}/passwd" > "${temp}" &&
          mv -f "${temp}" "${SYSCONFDIR}/passwd" || return 1
      fi
      echo "${pwd_entry}" >> "${SYSCONFDIR}/passwd" || ret=1
    fi
    return "${ret}"
}

#############################
# Start
#############################

name="$(basename $0)"
if [ "${name}" = "exim" ]; then
  echo "You must run exim-config before running exim."
  echo "Help is available from /usr/share/doc/Cygwin/exim-*.README and \"man exim\"."
  exit 127
elif [ "${name}" != "exim-config" ]; then
  echo "Unexpected script name $name"
  exit 1
fi

#############################
# Introduction
#############################

echo "************************************************"
echo "This script sets some key parameters of the exim"
echo "installation and starts the exim daemon."
echo "See the details in /usr/share/doc/Cygwin/exim-X-Y.README."
echo "    Interrupt with ^D."
echo
# Check if /etc/passwd and /etc/group are readable.
PASSWD=/etc/passwd
GROUP=/etc/group
ALIASES=/etc/aliases
if [ ! -r "${PASSWD}" -o ! -r "${GROUP}" ]; then
  echo "${PASSWD} and ${GROUP} must exist and be readable by all."
  exit 1
fi
# Check if running on NT
#nt=$(uname -s | sed -ne 's/^CYGWIN_NT-\([^.]*\).\([^ ]*\)/\1\2/p')
nt=$(uname -s | sed -ne 's/^CYGWIN_NT-\([^ ]*\)/\1/p')
if [ -n "$nt" ]; then
  pingpath="$(cygpath -m --sysdir)/ping.exe"
  export CYGWIN=ntsec
  chmod -c o+r "${PASSWD}" "${GROUP}"

  SYSTEMUID=$(sed -ne '/^[^:]*:[^:]*:[0-9]*:[0-9]*:[^:]*,S-1-5-18:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' "${PASSWD}")
  if [ -z "$SYSTEMUID" ]; then
    echo "WARNING: The Local SYSTEM SID S-1-5-18 was not found in ${PASSWD}."
    echo -e "This script will not set file ownership and exim may malfunction.\n"
  fi

  ADMINSGID=$(sed -ne '/^[^:]*:S-1-5-32-544:.*:/{s/[^:]*:[^:]*:\([0-9]*\):.*$/\1/p;q}' "${GROUP}")
  if [ -z "$ADMINSGID" ]; then
    echo "WARNING: The Local Administrators SID S-1-5-32-544 was not found in ${GROUP}."
    echo -e "This script will not set file ownership and exim may malfunction.\n"
  elif id -G | grep -qE '(^| )'"$ADMINSGID"'( |$)'
    then true
  else
    echo "WARNING: You are not in the Local Administrators group."
    echo -e "This script may fail to set file ownership and permissions.\n"
  fi
else
  pingpath="$(cygpath -m --windir)/ping.exe"
fi

####################################
# Check if usernames contain spaces
####################################
grep -o -e '^[^:]* [^:]*:[^:]*:[^:]*:[^:]*:' "${PASSWD}"
if [ "$?" -eq 0 ]; then
  echo -e "\nWARNING: The usernames displayed above contain spaces."
  echo "    They cannot receive mail nor appear in ${ALIASES}."
  if [ -n "$nt" ]; then
    echo "    Edit ${PASSWD} and use identifiers without spaces."
  fi
  else
  echo "OK. No usernames contain spaces."  
fi
echo

#############################
# Get the postmaster name
#############################
if grep -iqs \^postmaster: "${ALIASES}"; then
  echo "OK. The ${ALIASES} file contains \"postmaster\"."
else
  while true
    do
    echo -n "Who is the local postmaster? [${USER}] "
    if read postmaster; then
      [ -z "${postmaster}" ] && postmaster="$USER"
      if echo "${postmaster}" | grep -Fq " "; then
        echo "ERROR. The postmaster name cannot contain spaces."
      elif grep -iq \^"${postmaster}:" "${PASSWD}"; then
        echo "postmaster: ${postmaster}" >> "${ALIASES}"
        if [ -n "$nt" ]; then
          chmod 0664 "${ALIASES}"
          echo "OK. The ${ALIASES} file was updated and given mode 0664."
          if [ -n "${SYSTEMUID}" -a -n "${ADMINSGID}" ]; then
            chown "${SYSTEMUID}:${ADMINSGID}" "${ALIASES}" &&
              echo "OK. The ${ALIASES} file has owner ${SYSTEMUID} and group $ADMINSGID."
          fi 
        else
          echo "OK. The ${ALIASES} file was updated."
        fi
        break
      else
        echo "ERROR: User ${postmaster} is not in ${PASSWD}."
      fi
    else
      echo -e "Quitting.\n"
      exit 1
    fi
  done
fi
echo 

#############################
# Need /etc/resolv.conf?
#############################
RESOLV=/etc/resolv.conf
w95="`expr "$sys" : "CYGWIN_95"`"
if [ "$w95" -ne 0 ]; then
  if grep -qsF 'nameserver ' "${RESOLV}"; then
    echo "OK. The ${RESOLV} file appears to be valid."
  else
    echo "If you use DHCP you must enter the dns server(s) IP address(es) in ${RESOLV}."
    if request "Do you want to enter them now?"; then
      count=""
      while true
        do
	if [ -z "${count}" ]; then
	  echo -n "Enter the first address: "
        else
	  echo -n "Enter the next address, if any: "
        fi
        if read address; then
	  if [ -z "${address}" ]; then
            if [ -n "${count}" ]; then
	      break
            fi
          elif echo "${address}" | grep -qE '^([0-9]{1,3}\.){3}[0-9]{1,3}$'; then
            echo "nameserver ${address}" >> "${RESOLV}"
            count="positive"
          else
            echo "ERROR: Invalid address."
          fi
        else
          echo -e "Quitting.\n"
	  exit 1
        fi
      done
    else
      echo -e "You can run this script again to set the address(es).\n"
      exit 1
    fi
  fi
  echo
fi

#############################
# Create exim.conf
#############################

CONF=/etc/exim.conf
CONF_DEF=/etc/defaults/etc/exim.conf
setowner=
if [ ! -e "${CONF}" ]; then
  if cp "${CONF_DEF}" "${CONF}"; then
    echo "OK. ${CONF} created from ${CONF_DEF}."
    setowner=yes
  else
    echo "ERROR: Cannot create ${CONF} from ${CONF_DEF}."
    exit 1
  fi
  echo
fi

###########################
# Set the primary host name
###########################

# Check if primary_hostname is already defined
hostname="`sed -ne '/^[ \t]*primary_hostname[ \t]*=/{
  s/[^=]*=[ \t]*\(.*\)/\1/p
  q
}' "${CONF}"`"
if [ -z "${hostname}" ]; then
  echo "Looking for the fully qualified primary hostname."
  # Check if the fqhn can be found in one step
  host="`uname -n`"
  # Make sure we can find the Windows ping 
  if [ ! -x "$pingpath" ]; then 
    echo "ERROR: $pingpath is not executable. Trying ping."
    pingpath=ping
  fi
  pingout="`eval "$pingpath" -n 1 "${host}" 2> /dev/null`"
  # Get the fqhn and the ip address
  fqhn1="`echo "${pingout}" | sed -ne '2{
    s/^Pinging \([^ ]*\).*/\1/
    y/ABCDEFGHIKLMNOPQRSTUVWXYZ/abcdefghiklmnopqrstuvwxyz/
    p
  }'`"
  [ -z "${fqhn1}" ] && fqhn1="${host}"
  if request "Is it \"${fqhn1}\"?"; then
    echo "OK. Exim will autodiscover that name."
  else 
    address="`echo "${pingout}" |  sed -ne '2s/.*\[\(.*\)\].*/\1/p'`"
    # Get the fqhn from the ip address, excluding numerical forms.
    if [ -n "${address}" ]; then
      fqhn2="`"$pingpath" -n 1 -a "${address}" | sed -ne '2{
        s/^Pinging \([^ ]*\).*/\1/
        s/^[0-9.]*$//
        y/ABCDEFGHIKLMNOPQRSTUVWXYZ/abcdefghiklmnopqrstuvwxyz/
        p
       }'`"
      if [ "${fqhn1}" != "${fqhn2}" -a -n "${fqhn2}" ]; then
        request "Is it \"${fqhn2}\"?" && hostname="${fqhn2}"
      fi
    fi
    while [ -z "${hostname}" ]
      do
      echo "Enter your fully qualified hostname."
      if read hostname; then
        if echo "${hostname}" | grep -q '[^a-zA-Z0-9.-]'; then
          echo "ERROR: The hostname contains invalid characters."
          hostname=""
        fi
      else 
        echo -e "Quitting.\n"
        exit 1
      fi	
    done
    # Look for the commented line '# primary_hostname = '
    if sed -e 's/^ *# *primary_hostname *= *$/primary_hostname = '"${hostname}"'/' "${CONF}" > /tmp/exim_conf && \
       grep -Fqm 1 "primary_hostname = ${hostname}" /tmp/exim_conf && \
       mv -f /tmp/exim_conf "${CONF}"
      then
      echo "OK. The hostname ${hostname} was saved in ${CONF}."
      setowner=yes
    else
      echo "WARNING: The script could not set the primary hostname in ${CONF}."
      echo "Please edit that file to set the primary hostname."
    fi
  fi
else
  echo "OK. The primary hostname ${hostname}"
  echo "    is already defined in ${CONF}."
fi
echo

#####################################
# Verify exim.conf ownership and mode
####################################
if [ -n "$nt" -a -n "${SYSTEMUID}" -a -n "${ADMINSGID}" ]; then
  if [ -z "${setowner}" ]; then
    ls="$(/bin/ls -ln ${CONF})" 
    case "$ls" in
      -r??r???-?\ *1\ *${SYSTEMUID}\ *${ADMINSGID}\ *)
        true;;
      *)
        echo -e "WARNING: The ${CONF} file has unusual mode, owner or group:\n${ls}"
        if request "Do you want to set them to standard values?"
          then
          setowner=yes
          else
          setowner=no
        fi;;
    esac
  fi
  if [ "${setowner}" = "yes" ]; then
    chmod 664 "${CONF}"
    chown "${SYSTEMUID}:${ADMINSGID}" "${CONF}" &&
      echo "OK. The mode and ownership of the ${CONF} file are now:" &&
      /bin/ls -ln "${CONF}"
  fi
  if [ -n "${setowner}" ]; then
    echo
  fi
fi

##################################
# Look for /usr/bin/exim.exe
##################################
# A /usr/bin/exim.exe symlink will mask
# the /usr/bin/exim symlink.
# Its existence has been reported but is not explained.
# It is perhaps due to bad ln .exe magic.

if [ -L /usr/bin/exim.exe ]; then
   echo "WARNING: The symbolic link /usr/bin/exim.exe to $(/bin/ls -l /usr/bin/exim.exe | sed -e 's/^.*-> *//')"
   echo "    will be deleted as it masks the /usr/bin/exim symlink."
   rm -f /usr/bin/exim.exe && echo "OK. Done."
   echo
elif [ -e /usr/bin/exim.exe ] ; then
   echo "WARNING: The file /usr/bin/exim.exe masks the /usr/bin/exim symlink."
   echo "    In normal installations, delete that file."
   echo
fi

#####################
# Link exim if needed
#####################
# When exim points to exim-config, it returns 127
# This is the exit value reported by a shell when
# a program cannot be found or is not executable
exim=127
if [ -x /usr/bin/exim ]; then
  /usr/bin/exim 1> /dev/null 2>&1
  exim="$?"
fi

# Return 127 is explained above.
# 126 is when a file with executable permissions
# isn't really executable  
if [ "$exim" = 126 -o "$exim" = 127 ]; then
  for file in $(echo /usr/bin/exim-*.exe)
   do
   exim="${file}"
  done
  
  if [ -z "${exim}" ]; then
    echo "ERROR: Cannot find exim executable."
    exit 1
  elif ln -sf "${exim}" /usr/bin/exim; then
    echo "OK. /usr/bin/exim points to ${exim}."
  else
    echo "ERROR: Cannot point /usr/bin/exim to ${exim}" 
    exit 1
  fi
  echo
fi
    
######################################
# /usr/sbin/sendmail
# Thanks to Robert R Schneck-McConnell
######################################
offer=0
isexim=0
echo "INFO: Some programs expect /usr/sbin/sendmail to handle mail."
if [ -d /usr/sbin/sendmail ]; then
  sendmail="is a directory"
elif [ -L /usr/sbin/sendmail ]; then
  sendmail="is a symbolic link to $(/bin/ls -l /usr/sbin/sendmail | sed -e 's/^.*-> *//')"
  isexim=$(expr "$sendmail" : '.*exim$' )
  offer=1
elif [ -e /usr/sbin/sendmail ]; then
  sendmail="exists but is not a symbolic link"  
else 
  sendmail="does not exist"
  offer=1
fi

echo "Currently it ${sendmail}."
if [ "$offer" -eq 0 ]; then
  echo "If you wish it to be a link to exim, you'll need to do so by hand."
elif [ "$isexim" -eq 0 ]; then
  echo "Linking to exim may affect other programs such as cron."
  if request "Do you want to link /usr/sbin/sendmail to /usr/bin/exim ?"; then
      ln -s -f /usr/bin/exim /usr/sbin/sendmail &&
      echo -e "OK. Done."
  fi
fi
echo

#############################
# Install a service?
#############################
service="job"
if [ -z "$nt" ]; then
  eximregpath='\HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices\Exim'
  eximregdata=`regtool -q get $eximregpath`
  servtest=""
elif [ ! -e /usr/bin/cygrunsrv.exe ]; then 
  echo "INFO: Download cygrunsrv to start the mailer daemon as a service."
  servtest=no
else
  cygrunsrv -Q exim > /dev/null 2>&1
  servtest=$?
fi
  
if [ -z "$nt" -a -n "$eximregdata" ]; then
  echo "Exim is already installed as a service."
  echo "    Key: $eximregpath"
  echo "    Value: $eximregdata".
  if request "Do you want to remove or reinstall it?"; then
    regtool unset "$eximregpath" && echo "OK. The exim service was removed."
  else
    servtest=no
  fi
  echo
elif [ -n "$nt" -a "${servtest}" = "0" ]; then
  username="$(cygrunsrv -VQ Exim | sed -n -e 's/^Account[ :]*//p')"
  echo "Exim is already installed as a service under account '${username}'."
  if request "Do you want to remove or reinstall it?"; then
    cygrunsrv -R exim && echo "OK. The exim service was removed."
  else
   service="service"
   servtest=no
  fi
  echo
fi

if [ "${servtest}" = "no" ]; then
  true
elif request "Do you want to install the exim daemon as a service?"; then
  if mount | grep -Eq "on / type.*system"; then
      true
  else
      echo -e "ERROR: / must be mounted as system.\n"; 
      exit 1;
  fi
  getperiod
  if [ -n "$nt" ]; then
    getcygenv ntsec notraverse

    username=""

    if [ "$nt" \> 5.1 ]; then
      echo -e "\nTo setuid for deliveries, the exim daemon must run under a privileged account."
      echo "On Windows2003, you need to have or to create a special privileged account."
      echo "This script will help you do so." 
      if request "Do you want to create such an account?"; then
	if ! create_user; then
	    echo "There was a serious problem creating a privileged user."
	    request "Do you want to proceed anyway?" || exit 1
	fi
      else
	echo "OK. Using the SYSTEM account, which will not setuid."
      fi
    fi
    
    for file in /var/log/exim/cygrunsrv_out.log /var/log/exim/cygrunsrv_err.log; do
       [ -f "${file}" ] && chgrp "${ADMINSGID}" "${file}" && chmod g+w "${file}"
    done
    if [ -n "${username}" ]; then
      if [ -z "${password}" ]; then
	getvalue "Please enter the password for user ${username}:" "-s"
        password=${value}
      fi

      if cygrunsrv -I exim -p /usr/bin/exim -e CYGWIN="${cygenv}" \
          -a "-bdf -q${period}m" -d "Exim" -f "Mail Transfer Agent" \
          -1 /var/log/exim/cygrunsrv_out.log -2 /var/log/exim/cygrunsrv_err.log \
	  --dep Tcpip -u "${username}" -w "${password}"
        then
        service="service"
      else
        service="off"
      fi
    else
      if cygrunsrv -I exim -p /usr/bin/exim -e CYGWIN="${cygenv}" \
          -a "-bdf -q${period}m" -d "Exim" -f "Mail Transfer Agent" \
          -1 /var/log/exim/cygrunsrv_out.log -2 /var/log/exim/cygrunsrv_err.log \
          --dep Tcpip
        then
        service="service"
      else
        service="off"
      fi
    fi
  else
    eximregdata="\"`cygpath -wl /bin/sh.exe`\" -c \"/usr/bin/exim -bd -q${period}m\""
    if regtool set "$eximregpath" "$eximregdata"; then
      echo "OK. The registry was set:"
      echo "    Key: $eximregpath"
      echo "    Value: $eximregdata".
      echo "    This will only take effect at the next reboot."
    fi
  fi
fi
echo

#############################
# Start the daemon?
#############################
if ps -es | grep -Fqi 'exim-'; then
  echo "INFO: An exim process is already running."
elif [ "${service}" != "off" ]; then
  rm -f /var/spool/exim/exim-daemon.pid
  if request "Do you want to start the exim daemon as a ${service} now?"; then
    if [ "${service}" = "job" ]; then
      [ -z "${period}" ] && getperiod
      /usr/bin/exim -bd -q${period}m
    else
      cygrunsrv -S exim
    fi
    if [ $? -eq 0 ]; then
      echo "OK. The mail daemon is now accepting mail."
      echo "    You can test it with \"telnet localhost 25\"."
    fi
  elif [ "${service}" = "job" ]; then
    echo "OK. Type \"/usr/bin/exim -bd -q15m\" to start the exim daemon job."
  elif [ -n "$nt" ]; then
    echo "OK. Type \"cygrunsrv -S exim\" to start the exim daemon service."
    echo "    It will restart automatically at each reboot."
  fi
fi
echo

#############################
# Check exim user
#############################
if [ -n "$nt" ] && grep -q -i '^exim:' "${PASSWD}"; then
  echo "WARNING: You have defined an \"exim\" user in ${PASSWD}."
  echo "This is a trouble prone feature, do not use it initially."
  echo -e "See requirements in /usr/share/doc/Cygwin/exim-X-Y.README.\n"
fi

#############################
# Cleanup
#############################
if [ -e /usr/local/bin/exim ]; then
  echo "You have an old installation of exim in /usr/local/bin."
  if request "Do you want to delete the executables? (recommended)"; then
    cd /usr/local/bin
    rm -f exim.exe exim exim-*.exe exim_dumpdb.exe exim_fixdb.exe exim_tidydb.exe exinext exiwhat
    rm -f exim_dbmbuild.exe exicyclog exigrep eximstats exiqsumm exim_lock.exe exim_checkaccess
    echo "OK. The cleanup not affect files under /usr/local/exim."
  fi
  echo
fi

#############################
# End
#############################
echo "INFO: The exim log files are in /var/log/exim."
echo
echo "When everything else fails,"
echo "    read /usr/share/doc/Cygwin/exim-X-Y.README,"
echo "    or look up the documentation on www.exim.org."
echo "Many happy mailings :)"
echo
