#
#  mailout   (5/31/88)
#-----------------------------------------------------------------------------
#  mailout will create a mail text file and send it to the users indicated
#          by the input arguments
#
#  usage: mailout [-f mailfile] [-s subject] [--] user [user ...]
#
#  Mailout will use the indicated [-f mailfile] file as text, or will allow
#  you to enter text from the keyboard through the cp program if the mailfile
#  is omitted.  It will use the indicated text [-s subject] as a subject, or
#  it will prompt you for the subject if the subject is omitted.  It will
#  get the name of the sender from the current value of $LOGNAME and the
#  message date/time from the current system date.  It will then send the
#  mail text as a text file to the appropriate mail directories as indicated
#  by the user input arguments.
#
#  If one of the input arguments is a mail alias as indicated in the file
#  ${MAIL}/malias, mailout will send a copy of the text to each user set up
#  for that alias.
#
#  Start by setting up a trap to remove any temporary file, and then use the
#  getopt program to analyze the input arguments.  Once done, set the -s and
#  -f values (if any) from the input arguments.
#-----------------------------------------------------------------------------
trap 'rm -fs ${TEMPDIR}tempmail \
             ${TEMPDIR}names' 0 2                # set cleanup trape
set -/                                           # use -/ flag
set -- `getopt s:f: $*`                          # process input args
if [ $? -ne 0 ]                                  # if errors
   then
   echo "mailout: invalid input options."        # print error message
   exit 2                                        # and quit
fi
for ARG in $*                                    # for all args
   do
   case $ARG in
     -s) SUBJECT=$2; shift; shift;;              # handle -s subject
     -f) FILEID=$2;  shift; shift;;              # handle -f mailfile
     --) shift;      break;;                     # end of -xxx args
   esac
done
#-----------------------------------------------------------------------------
#  Set header values.  Sender comes from $LOGNAME.  Subject comes from -s xxx
#  argument or from the keyboard.  File id comes from -f xxx or from the
#  keyboard.  Date comes from the current system date.
#  usage: mailout [-f mailfile] [-s subject] [--] user [user ...]
#-----------------------------------------------------------------------------
SENDER=$LOGNAME                                  # set sender
case $SUBJECT in
   "") echo -n "subject "                        # if no -s xxx, get subject
       read SUBJECT;;                            # from the keyboard
esac
case $FILEID in                                  # if no -f xxx, get the mail
   "") echo "enter your message.  To end, type CTRL/Z at the start of a line."
       cp con ${TEMPDIR}tempmail                 # from the keyboard
       echo "(end of message)";;
   ${TEMPDIR}tempmail) ;;                        # leave tempmail file as is
   *)  cp $FILEID ${TEMPDIR}tempmail;;           # else copy mail text
esac                                             # to tempmail file
DATE="`udate \"+%a %D %r\"`"                     # set date
#-----------------------------------------------------------------------------
#  Now loop through the list of recipients.  If a name is a mail alias, then
#  loop through the alias values sending the proper header and mail text
#  to each intended recipient.
#
#  NOTE: for each letter to be sent, mailout creates a file name in the
#  recipient's mail directory made up of the sender's name and a number
#  (starting with 1).  As long as the constructed file name exists, mailout
#  will create file names with even increasing numbers for the extension.
#  When it has a name that is not yet in the recipient's directory, it will
#  send the mail to that name.
#
#  NOTE: the two [...] ranges in the following sed command contain a tab
#  character and a space character as well as any visible characters
#-----------------------------------------------------------------------------
for SENDTO                                       # for each input arg
   do                                            # see if it is an alias
   if grep "^$SENDTO" ${MAIL}/malias > ${TEMPDIR}names
     then                                        # if so, get group names
       LIST="`sed -e \"s&^[^     ]*[     ]*\(.*\)$&\1&\" ${TEMPDIR}names`"
     else
       LIST=$SENDTO                              # else use arg as name
   fi
   for ADDR in $LIST                             # for each final address
     do                                          # make base file name & the
     FILE=${MAIL}/$ADDR/$LOGNAME                 # appropriate dir if needed
     test -d ${MAIL}/$ADDR || mkdir ${MAIL}/$ADDR
     PTR=1                                       # start with name.1
     while [ -f "$FILE.$PTR" ]                   # while file exists
       do                                        # increment extension value
       PTR=`expr $PTR + 1`                       # until non-existing file
     done                                        # is found
     FILE="$FILE.$PTR"                           # this is final output file
#-----------------------------------------------------------------------------
#  NOTE:  The following block works well with version of MS-DOS that do not
#  require the -z flag to suppress extra blank lines.
#-----------------------------------------------------------------------------
#    {
#      echo "From:    $SENDER"                   # put header into file
#      echo "To:      $ADDR"
#      echo "Subject: $SUBJECT"
#      echo "Date:    $DATE"
#      echo ""                                   # then add blank line
#      cat ${TEMPDIR}tempmail                    # then add text
#    } > $FILE                                   # to the output file
#-----------------------------------------------------------------------------
#  The following block works with version of MS-DOS that do require the -z
#  flag to suppress extra blank lines.  It is slower than the previous block,
#  but works well with Version 2.x of MS-DOS
#-----------------------------------------------------------------------------
     echo "From:    $SENDER"  >  $FILE           # put header into file
     echo "To:      $ADDR"    >> $FILE
     echo "Subject: $SUBJECT" >> $FILE
     echo "Date:    $DATE"    >> $FILE
     echo ""                  >> $FILE           # then add blank line
     cat ${TEMPDIR}tempmail   >> $FILE           # then add text
     echo "--mail sent to $ADDR--"               # and report progress
   done                                          # done with an address
done                                             # done with input arg
