#! /usr/bin/env bash
#
# $Id: send-mail.in 6811 2009-07-06 20:41:10Z robin $
#
# Usage: send-mail subject [destination] <txt
#
# Sends stdin per mail to recipients, adding some common headers/footers

if [ "1" != "1" ]; then
   exit 0
fi

if [ $# -lt 1 -o $# -gt 2 ]; then
   echo Wrong usage.
   exit 1
fi

from="Big Brother <bro@ppc-comm02>"
replyto=""
subject="[Bro] $1"

if [ $# = 2 ]; then
   to=$2
else
   to="mockbuild"
fi

tmp=/var/spool/bro/tmp/mail.$$.tmp

rm -f $tmp
touch $tmp

echo From: $from >>$tmp
echo Subject: $subject >>$tmp
echo To: $to >>$tmp
echo User-Agent: Bro Control 0.3 >>$tmp

if [ "$replyto" != "" ]; then
   echo Reply-To: $replyto >>$tmp
fi

echo >>$tmp

cat >>$tmp

cat >>$tmp <<EOF

-- 
[Automatically generated.]

EOF

sendmail -t -oi <$tmp && rm -f $tmp
