#!/bin/sh
# mails a system report

# make a tmp file for the system report

TMPFILE=/tmp/mailreport.`date +%H%M%S`
touch $TMPFILE
chmod 600 $TMPFILE

# proccess the command line args
while getopts s:u:t: OPTION
do
  case $OPTION
  in
    s) SUBJECT=$OPTARG ;;
    u) GPGUSER=$OPTARG;;
    t) MAILTO=$OPTARG ;;
    \?) echo "Invalid option $OPTION";;
  esac
done

# check the command line args
if [ "$MAILTO" ]
then
:
else
echo "FAIL"
echo "No mailto specified."
exit 1
fi

if [ "$SUBJECT" ]
then
:
else
SUBJECT="ESM System Report"
fi

# get the system report
while [ "$LINE" != "DONEREPORT" ]
do
read LINE
echo $LINE >> $TMPFILE
done

# send via mail encrypting if $GPGUSER set
if [ "$GPGUSER" ]
then
  cat $TMPFILE | gpg -ea -r "$GPGUSER" | mail "$MAILTO" -s "$SUBJECT"
else
  cat $TMPFILE | mail "$MAILTO" -s "$SUBJECT"
fi

# cleanup
rm $TMPFILE

echo "SUCCEED"
