#!/bin/sh

# usage:
#   ./bigtest recipient count [forks [port [from]]]

if [ -z "$2" ]; then
  grep '^# ' $0
  exit 1
fi

port=25
from=$1
forks=1

if [ "$3" ]; then
  export forks=$3
fi

if [ "$4" ]; then
  export port=$4
fi

if [ "$5" ]; then
  export from=$5
fi

tempdir=/tmp/sagatorbigtest

function mktestdir() {
  ./download_viruses.sh
  mkdir -p $1
  ./pattern 1000 > $1/bigpattern-1000
  ./pattern 300 > $1/bigpattern-300
  dd if=/dev/urandom of=$1/mrnd.exe bs=1024 count=200
  ./mimeattach.py $1/mrnd.exe > $1/mimerandom
  rm -f $1/mrnd.exe
  cp -a clean spam tmp/* $1/
  cp -a *.eml $1/ 2>/dev/null || true
  rm -rf tmp
}

function rmtestdir() {
  rm -rf $tempdir/
}

mktestdir $tempdir

myhostname=`hostname`
host1=`echo $1 | cut -d@ -f2`
host="$SMTPHOST"
if [ -z "$host" ]; then
  host=`host -t mx $host1 | sort -n -k 6 | head -n 1 | cut -d" " -f7`
fi
if [ -z "$host" ]; then
  host=`host $host1 | grep -v IPv6 | cut -d" " -f4`
fi
dirsize=`du -s $tempdir/ | cut -f1`
total=`expr $dirsize \\* $2`
sentsize=0

for fi in `seq 1 $3`; do # forks
  for i in `seq 1 $2`; do
    for p in bigpattern-1000 bigpattern-300 mimerandom clean Eicar *.eml
    do
      if [ -f "$p" ]; then
      ((
        echo "HELO $myhostname"
        echo "MAIL FROM: $from"
        echo "RCPT TO: $1"
        echo "DATA"
        cat $tempdir/$p
        echo "."
        echo "QUIT"
      ) | nc $host $port >/dev/null && \
        echo "PROGRESS: $i/$2 $fi/$3  [$dirsize kB ... $sentsize/$total kB] $p" )
      fi
    done
    sentsize=`expr $sentsize + $dirsize`
  done &
done

#rmtestdir
