#!/bin/sh
#
# autoreply
#
# This program is called to generate a reply to a message sent to systems.
#
# It should print a mail-style message to standard output, including
# headers and the body, and it should exit with a status of 0 if the
# message should be sent.
#
# A non-zero exit status will cause the reply to not be sent, but will
# not otherwise affect the req system.
#
# Req does not call this program with command line options.
#
# The PATH will be extremely minimal, if it exists at all.
#
# Environment variables that have been setup include:
#
# REQNUM      The number of the new request.
# REQORIGSUBJ The subject line without the req number in it.
# REQNEWSUBJ  The subject line with the req number in it.
# REQUSESUBJ  Just the part of the subject line with the req number in it.
# REQHEADER   A file name, where the file contains the user's mail header.
# REQBODY     A file name, where the file contains the user's mail body.
# REQAUTO     A file name, where the file contains a useful mail header
#               for the auto-reply program to use.  It does not contain
#               any CC: lines, and it did follow any reply-to: lines.
#               It does _not_ contain any blank lines.
# REQFROM     The information from the user's "from:" line.
# REQUSER     The best guess as to the email address in the from line.
#
# For configuration outside of CCS:
#   - Change the paths below to point to the right place in your site.
#   - Example files for these can be found in the etc/ directory of
#     the req distribution.
#   - Look into the code that checks user's home directories... that
#     may have to change or be commented out.
#   - Change the signature or any other text you'd like.
#

LOCALDOMAIN=ccs.neu.edu
SILENT=/net/request/etc/silent-users
RMOTD=/net/request/etc/rmotd
REQSTAT=/local/adm/bin/reqstat


PATH="/usr/ucb:/bin:/usr/bin"


# ---------------------------------------------------------------------------
# Prechecks to see if we want to autoreply.
# ---------------------------------------------------------------------------

#
# Figure out if the user is from the local net or not.  How you do this
# at your site is completely up to you, since it's dependent on how your
# mail is set up.
#
# We either have it come from "user" or "user@machine.ccs.neu.edu", and
# that's what this set of tests looks for to determine whether or not
# the user is local.
#
USERNAME=`echo $REQUSER | cut -d@ -f1`
DOMAIN=`echo $REQUSER | cut -d@ -f2 -s`
LOCAL=`basename "$DOMAIN" $LOCALDOMAIN`
if [ "$DOMAIN" = "" -o "$LOCAL" != "$DOMAIN" ]; then
  LOCAL="yes"
else
  LOCAL="no"
fi

#
# Check to see if the user is listed in the silent-users file, which
# contains names like 'root', 'systems', etc.
#
if [ -f $SILENT ]; then
  if grep "^$USERNAME$" $SILENT > /dev/null 
  then
    exit 1
  fi
fi

#
# Check to see if the user has a .reqsilent in their home directory.
# If they do, don't send them mail.
#
if [ "$LOCAL" = "yes" ]; then
  if [ -f /home/$USERNAME/.reqsilent ]; then
    exit 1
  fi
fi  


# ---------------------------------------------------------------------------
# Gather data to use later in the text.
# ---------------------------------------------------------------------------

SAVEIFS="$IFS"
DATE=`date | sed 's/[ ][ ]*/ /g'`                      # Remove double spaces
IFS=" "
set - $DATE
WEEKDAY=$1
MONTH=$2
DAY=$3
TIME=$4
ZONE=$5
YEAR=$6

SAVEIFS="$IFS"
IFS=:
set - $TIME
HOUR=$1
MINUTE=$2
SECOND=$3
IFS="$SAVEIFS"

case $WEEKDAY in 
  Sun* )
    WEEKDAY="Sunday" ;   CLOSED="yes" ;;
  Mon* )
    WEEKDAY="Monday" ;   CLOSED="no"  ;;
  Tue* )
    WEEKDAY="Tuesday";   CLOSED="no"  ;;
  Wed* )
    WEEKDAY="Wednesday"; CLOSED="no"  ;;
  Thu* )
    WEEKDAY="Thursday";  CLOSED="no"  ;;
  Fri* )
    WEEKDAY="Friday";    CLOSED="no"  ;;
  Sat* )
    WEEKDAY="Saturday";  CLOSED="yes" ;;
esac

case $MONTH in 
  Jan* )   MONTH="January"  ;;
  Feb* )   MONTH="February" ;;
  Mar* )   MONTH="March" ;;
  Apr* )   MONTH="April" ;;
  Jun* )   MONTH="June" ;;
  Jul* )   MONTH="July" ;;
  Aug* )   MONTH="August" ;;
  Sep* )   MONTH="September" ;;
  Oct* )   MONTH="October" ;;
  Nov* )   MONTH="November" ;;
  Dec* )   MONTH="December" ;;
esac

if [ "$HOUR" -gt 19 -o "$HOUR" -lt 7 ]; then
  CLOSED=yes
fi


#
# Insert code here to get req stats once they're available.
#
TOTALREQ=`$REQSTAT -active`
HIGHPRI=`$REQSTAT -high`
REQSTODAY=`$REQSTAT -day`
REQSTHISWEEK=`$REQSTAT -week`


# ---------------------------------------------------------------------------
# Printing the output message
# ---------------------------------------------------------------------------

# First we print the headers, and we believe that req-mailgate set them
# up correctly.
# Append the blank line seperator to that.
#
cat $REQAUTO
echo ""

echo "Greetings.  (This is an automated response.  There is no need to reply.)"
echo ""
echo "Your message regarding:"
echo "  $REQORIGSUBJ"
echo "was received by the Systems Group on $WEEKDAY, $MONTH $DAY, at ${HOUR}:${MINUTE}."
echo ""
echo "It has been assigned request number $REQNUM."
echo ""
echo "In order help us track the progress of this request, we ask that you"
echo "include the string $REQUSESUBJ in the subject line of any further mail"
echo "about this particular request."
echo "For example:"
echo "    Subject: $REQNEWSUBJ"
echo ""
echo "You may do this simply by replying to this email."
echo ""

if [ "$CLOSED" = "yes" ]; then
  echo "Unfortunately, the Systems Group does not have enough staff to work"
  echo "24 hours a day, 7 days a week, although we often try.   We will"
  echo "respond to your request as soon as we can."
  echo ""
else
  echo "We will work on your request as soon as we can."
  echo ""
fi

echo "In order to give you an idea of how long it may take us to solve to"
echo "your request, we have included a summary of the current requests in"
echo "the queue:"
echo "                 High priority items: $HIGHPRI"
echo "   Requests received in the last day: $REQSTODAY"
echo "  Requests received in the last week: $REQSTHISWEEK"
echo "         Total requests in the queue: $TOTALREQ"
echo ""

if [ "$LOCAL" = "yes" ]; then
  echo "You may check on the status of this request at any time by running"
  echo "   status $REQNUM"
  echo "on any UNIX computer within the College."
  echo ""
fi

#
# Include a message of the day, that may say something like:
#  Boyoboy are we busy with the mac labs today.  Give up and go home.
#
if [ -f $RMOTD ]; then
  if [ `cat $RMOTD | wc -w` -gt 0 ]; then
    UPDATE=`ls -l $RMOTD | awk '{print $7 " on " $5 " " $6}'`
    echo "The current status of the group: (last updated at $UPDATE)"
    cat $RMOTD | sed 's/^/  /'
    echo ""
  fi
fi


echo "For reference purposes, your mail has been included below."
echo ""
echo "The CCS Systems Group - systems@ccs.neu.edu"

# fortune, why, how, yow, or random systems quote inserted here

echo ""
echo "-----------------------------------------------------------------------"
echo ""
cat $REQBODY

exit 0
