#! /usr/bin/env bash
#
# Usage: send-mail subject [destination] <txt
#
# Sends stdin per mail to recipients, adding some common headers/footers

. `dirname $0`/broctl-config.sh

if [ "${sendmail}" == "" ]; then
   exit 0
fi

if [ ! -e "${sendmail}" ]; then
    echo "${sendmail} is not executable"
    exit 1
fi

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

from="${mailfrom}"
replyto="${mailreplyto}"
subject="${mailsubjectprefix} $1"

if [ $# = 2 ]; then
   to=$2
else
   to="${mailto}"
fi

tmp=${tmpdir}/mail.$$.tmp

rm -f $tmp
touch $tmp

echo From: $from >>$tmp
echo Subject: $subject >>$tmp
echo To: $to >>$tmp
echo User-Agent: Bro Control ${version} >>$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
