#!/bin/sh
#
# Invoke the script that mails output back to the user.
# If any errors are generated by this script, collect them
# and mail them to `recipient'.
#
# Modified by Lee McLoughlin for Solaris 2.3 <lmjm@doc.ic.ac.uk>
#
# The path must include:
#
#  - the `mail_receiver' shell script
#  - a mail program that takes the -s option
#  - rm
#  - whoami

PATH=/usr/ucb:/bin:bin
#PATH=/usr/ucb:/bin:/archie/src/3.0/telnet-client/archie-client/mail_back_end

# ---------------- Configure ----------------
# the address, at your site, to which to mail errors generated
# when attepmting to send mail
recipient=archie-errors
#
# temporary file in which to collect any error messages
err=/tmp/mailerr$$
# -------------- End Configure --------------

> $err
# lmjm:
exec 2> $err
if [ $# -ge 1 ] ; then
  if [ "$1" != "-d" ] ; then
    args=""
  else
    exec 2> $err
    set -x
    args="-d"
  fi
fi

fatal()
{
  echo ""
  echo "${prog}: $1"
  exit 1
}

get_home()
{
  if [ $# -ne 1 ] ; then
    fatal "get_home: bad number of arguments"
  fi
  if p=`grep "^${1}:" /etc/passwd` ; then
    echo $p | awk -F: '{print $6}'
  elif p=`ypmatch $1 passwd` ; then
    echo $p | awk -F: '{print $6}'
  else
    fatal "get_home: can't find home directory for $1"
  fi
}

prog=`basename $0`
if cd `get_home \`whoami\`` ; then
  :
else
  fatal "can't change to home directory!"
fi

umask 077
bin/mail_receiver $args 2>> $err
if [ -f $err -a -s $err ] ; then
  mail -s "error from mail_receiver $err" $recipient < $err
fi
rm -f $err