#!/bin/sh
#
# Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# emailrelay-loop-test.sh
#
# Runs a polling E-MailRelay server that loops back to itself.
#
# usage: emailrelay-loop-test.sh [-v] [<exe>]
#        -v  - use valgrind
#
# To get a good valgrind report make use of the admin interface to 'terminate' 
# the server cleanly. The admin port is recorded in the ".info" file.
#

# configure
thisdir="`cd \`dirname $0\` ; pwd`"
if test "$1" = "-v" ; then v="valgrind --leak-check=full --show-reachable=yes --num-callers=50 " ; shift ; fi
if test "$1" != "" ; then exe="$1" ; fi
if test "$1" = "" ; then exe="$thisdir/../src/main/emailrelay" ; test -x "$exe" || exe="$thisdir/../emailrelay" ; fi
submit="${exe}-submit"
tmp="/tmp"
base="$tmp/`basename $0`.$$.tmp"
port_1=`perl -e 'print int(rand()*1000) + 1025'`
port_2=`expr $port_1 + 1`
files="100"
lines="100" # per file
characters="10000" # per line

# check
test -x "$exe" -a -x "$submit" || echo `basename $0`: error: cannot execute "[$exe] or [$submit]" >&2 
test -x "$exe" -a -x "$submit" || exit 1

# initialise
trap "e=\$? ; rm -rf $base ; exit $e" 0 
trap "rm -rf $base ; exit 127" 1 2 3 13 15
mkdir "${base}"
perl -e 'print "To: me\n\n";for($i=0;$i<'"$lines"';$i++){print "x" x '"$characters"',"\n"}' > $base/.tmp
for i in `perl -e 'print join(" ",1..'"$files"'),"\n"'` ; do $submit -f me -s $base me < $base/.tmp ; done
echo $port_1 $port_2 > $base/.info
echo $base > $tmp/`basename $0 .sh`.txt

echo '#!/bin/sh' > $base/verifier
echo 'echo ""' >> $base/verifier
echo 'echo $1' >> $base/verifier
echo 'exit 1' >> $base/verifier
chmod +x $base/verifier

# run
set -x
${v}${exe} --spool-dir=$base -lt -p $port_1 -a $port_2 -Q --forward-to=localhost:$port_1 --poll 1 --verifier $base/verifier --log-time --user daemon --size 9999999 --pid-file $base/pidfile
