# Base source of start-up shell script for the Exim Monitor. Used to set the
# required environment variables before running the program. Using script
# rather than a configuration file means that computation can be done.
# The build process concatenates on the front of this various settings from
# os-specific files and from the user's configuration file.

# Copyright (c) 2001 University of Cambridge.
# See the file NOTICE for conditions of use and distribution.


# See if this installation is using the esoteric "USE_NODE" feature of Exim,
# in which it uses the host's name as a suffix for the configuration file name.
# Set $hostsuffix if a suffixed file is found.

configure_file_use_node=CONFIGURE_FILE_USE_NODE
if [ "$configure_file_use_node" = "yes" ]; then
  host=`uname -n`
  if [ ! "$host" != "" ]; then
      if [ -f CONFIGURE_FILE.$host ]; then
          hostsuffix=.$host
      fi
  fi
fi

# Set the configuration file name

config=CONFIGURE_FILE$hostsuffix

# Determine where the spool directory is and whether there is any setting of
# log_file_path. Search for an exim_path setting in the configure file;
# otherwise use the bin directory. Call that version of Exim to find the spool
# directory and the setting of log_file_path.

config=${EXIMON_EXIM_CONFIG-$config}

# Add code here to redefine "config" if an alternative configuration file
# should be used in some circumstances. If you do that, you should also
# arrange for the value to be set in EXIMON_EXIM_CONFIG, and to export
# that variable into the environment.

EXIM_PATH=`grep '^[	 ]*exim_path' $config | sed 's/.*=[	  ]*//'`
if test "$EXIM_PATH" = ""; then EXIM_PATH=BIN_DIRECTORY/exim; fi

SPOOL_DIRECTORY=`$EXIM_PATH -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
LOG_FILE_PATH=`$EXIM_PATH -C $config -bP log_file_path | sed 's/.*=[  ]*//'`

# If log_file_path is "syslog" then logging is only to syslog, and the monitor
# is unable to display a log tail unless EXIMON_LOG_FILE_PATH is set to tell
# it where the log data is. Otherwise, remove any occurrences of
# "syslog:" or ":syslog" (spaces allowed in various places) and look at the
# remainder of the entry. If it's null, the default is "mainlog" in the
# "log" directory in the spool directory. Otherwise, set the name from the
# given path.

if [ "$EXIMON_LOG_FILE_PATH" != "" ] ; then
  LOG_FILE_NAME="$EXIMON_LOG_FILE_PATH"
elif [ "$LOG_FILE_PATH" = "syslog" ] ; then
  LOG_FILE_NAME=""
  echo \*\*\*
  echo Exim is using the syslog interface for its log data. If you redirect all
  echo MAIL.INFO syslog messages into a separate file, you can point eximon at
  echo that file with the EXIMON_LOG_FILE_PATH environment variable.
  echo \*\*\*
else
  LOG_FILE_NAME=`echo $LOG_FILE_PATH | \
    sed -e 's/ *:\{0,1\} *syslog *:\{0,1\} *//' -e 's/%s/main/'`
  if [ "$LOG_FILE_NAME" = "" ] ; then
    LOG_FILE_NAME=$SPOOL_DIRECTORY/log/mainlog
  fi
fi

# The basename and hostname commands vary from system to system

basename=BASENAME_COMMAND
hostname=HOSTNAME_COMMAND

# SunOS5 is a pain in that they may be in one of two places. So is Linux
# in the case of basename. Set up a general mechanism for searching for
# them in several places.

if [ "${basename}" = "look_for_it" ] ; then
  if [ -f /usr/bin/basename ] ; then
    basename=/usr/bin/basename
  else
    if [ -f /bin/basename ] ; then
      basename=/bin/basename
    else
      basename=/usr/ucb/basename
    fi
  fi
fi

if [ "${hostname}" = "look_for_it" ] ; then
  if [ -f /usr/bin/hostname ] ; then
    hostname=/usr/bin/hostname
  else
    if [ -f /bin/hostname ] ; then
      hostname=/bin/hostname
    else
      hostname=/usr/ucb/hostname
    fi
  fi
fi

# Set hostname to the full hostname with the specified domain
# stripped off its end. On Solaris 2, the default basename
# command treats its suffix argument as a pattern. Consequently,
# if fullhostname contains no dots but ends with what looks like
# the domain, straightforward use of basename screws things up.
# Use a general test for this case, just in case any other OS
# do the same.

fullhostname=`${hostname}`
case `${basename} abc .c` in
  a) hostname=`${basename} ${fullhostname} '\.'${DOMAIN}` ;;
  *) hostname=`${basename} ${fullhostname} .${DOMAIN}` ;;
esac


# Arrange for the window title field to be substituted by the shell
# so that it can contain either the full or the short host name. This
# is a tedious little bit of magic, but I don't know how to do it
# in a less tortuous way.

WINDOW_TITLE=`fullhostname=${fullhostname} hostname=${hostname} /bin/sh <<xx
echo ${WINDOW_TITLE}
xx
`

export EXIM_PATH \
  LOG_BUFFER LOG_DEPTH LOG_FILE_NAME LOG_FONT LOG_WIDTH \
  ACTION_OUTPUT ACTION_QUEUE_UPDATE\
  MENU_EVENT MIN_HEIGHT MIN_WIDTH \
  QUALIFY_DOMAIN QUEUE_DEPTH QUEUE_FONT QUEUE_INTERVAL QUEUE_MAX_ADDRESSES \
  QUEUE_STRIPCHART_NAME QUEUE_TOTAL QUEUE_WIDTH SPOOL_DIRECTORY \
  START_DEPTH LOG_STRIPCHARTS SIZE_STRIPCHART SIZE_STRIPCHART_NAME \
  START_SMALL STRIPCHART_INTERVAL \
  TEXT_DEPTH WINDOW_TITLE

# Exec to the program we really want to run, thereby continuing in
# just the one process, and let it run in parallel with whatever
# called this script.

exec ${EXIMON_BINARY} $*

# End
