#!/bin/bash
# Postfix sets a number of environment variables including
# $SENDER and $RECIPIENT.
#
# See man 8 local, man 8 sendmail
#

KITTEN_MSG="You found kitten! Way to go, robot!"
REPLY=/tmp/autobot_reply.$$
rfkmsg="/usr/local/bin/rfkmsg"

if [ ! $SENDER ]; then
  exit
fi

if [ `echo $SENDER | grep secondlife.com` ]; then
  # keep track of the game piece pool
  MY_MSG="not yet implemented"
else
  # just give a random message
  N_KITTENS=20
  N_MSGS=`$rfkmsg`
  MSG_N=$(($RANDOM % ($N_MSGS + $N_KITTENS)))
  if [ $MSG_N -lt $N_MSGS ]; then
    MY_MSG=`$rfkmsg $MSG_N`
  else
    MY_MSG=$KITTEN_MSG
  fi
fi


# Create response file
echo "To: $SENDER"                                          > $REPLY
echo "From: robotfindskitten@xenotrout.com"                >> $REPLY
#echo 'Precedence: junk (autoreply)'                        >> $REPLY
echo 'Subject: robotfindskitten'                           >> $REPLY
echo ''                                                    >> $REPLY
echo $MY_MSG                                               >> $REPLY
echo $MSG_N  of $N_MSGS                                    >> $REPLY
echo '.'                                                   >> $REPLY

# Send email off
/usr/sbin/sendmail -r robotfindskitten@xenotrout.com $SENDER < $REPLY
echo /usr/sbin/sendmail -r $RECIPIENT $SENDER >> /tmp/rfklog
#/usr/sbin/sendmail -r $RECIPIENT kinscore < $REPLY

rm -f $REPLY   &> /dev/null

exit
