#!/bin/sh
#
# this script appends some text to the end of an incoming message for
#    the mailing list, saves a copy in the archive, then sends the 
#    message out to everyone on the mailing list.
#
# Usage: mailer.sh list-name
# (e.g. mailer.sh apo35-general)
#
TEMP=/tmp/mailer.$$
TEMP2=/tmp/mailer2.$$
ARCDIR=/u/strauss/apo/mailer/$1
MACHINE=astro.princeton.edu
REQUEST=apo35-request
PERL=/u/strauss/apo/mailer/bin/mail2html

# first, copy the incoming message to the file $TEMP
cat > $TEMP

# next, figure out the ID number of this message - it should be the integer
#    one larger than the current largest file number in the archive 
#    directory.  all files in the archive have names like "msg.23".
n=`/bin/ls $ARCDIR/msg.*.html | sed -e 's/^.*msg\.\([0-9]*\)\.html/\1/' | sort -n | tail -1 | awk -e '{ print $1 + 1}' `
if [ X"$n" = X"" ]; then
	n=1
fi

# tack an extra bit on the end of the file, telling people about
#   the mailing list and about this message
cat >> $TEMP <<EOF
APO APO APO APO APO  Apache Point Observatory 3.5m  APO APO APO
APO
APO  This is message $n in the $1 archive. You can find
APO  the archive on http://www.astro.princeton.edu/APO/$1/INDEX.html
APO  To join/leave the list, send mail to $REQUEST@$MACHINE
APO  To post a message, mail it to $1@$MACHINE
APO
APO APO APO APO APO APO APO APO APO APO APO APO APO APO APO APO
EOF
#     pipe a copy of the msg through Robert's perl script that
#     creates a .html version of the message.  Note that we must
#     chdir ARCDIR before running the PERL script!  Note also that
#     the PERL script assumes that the "tail" has been added to the
#     message already.
(chdir $ARCDIR; $PERL < $TEMP > msg.$n.html)

# now, make a copy of the message, but change some of the mail-header
#    information so that errors don't cause floods, and so that
#    users of the "vacation" program won't reply to the group, either.
cat > $TEMP2 <<EOF
Reply-To: $REQUEST@$MACHINE
Errors-To: $REQUEST@$MACHINE
Precedence: bulk
EOF
sed -e '/^Reply-To:/d' -e '/^Sender:/d' -e '/^From /d' $TEMP >> $TEMP2
cat $TEMP2 | /usr/lib/sendmail -f owner-apo35 $1-out

echo /bin/rm -f $TEMP $TEMP2

exit 0


