#!/bin/bash
# use: ./checkdestination phonenumber count mailaddress onlyfailed
alias rm=rm
rm -f status
for ((i=1; i<=$2; i++)); do
    ./cleanup 2>/dev/null
    ./dotest -f 5.2.1.xml -t -n ${1} 2>/dev/null >/dev/null
    ls *.ok 2>/dev/null >> status
    ls *.fail 2>/dev/null >> status
done

printf "    ok: %s\n" $(grep ok status | wc -l)
printf "failed: %s\n" $(grep fail status | wc -l)

printf "    ok: %s\n" $(grep ok status | wc -l) > status2
printf "failed: %s\n" $(grep fail status | wc -l) >> status2

if [ -z ${4} ] ;then
    cat status2 | mail -s "checkdestination ${1} ${2} ${3} ${4}" "${3}"
else
    let errorcount=$(grep fail status | wc -l)
    if (( "${errorcount}" > "0" )) ; then
        cat status2 | mail -s "checkdestination ${1} ${2} ${3} ${4}" "${3}"
    fi
fi
