#!/bin/bash
#
# 245-contactrange.sh
# Greg Menke
# April 5, 2011
# copied from 1000.loopback test

# script used in "make test" to ensure ion is functioning properly.
# script is expected to be run by automake, meaning:
# 1: Environment variable "srcdir" exists and points to the root directory of
# the ion source code.
# 2: The current working directory contains the built programs of ion.



echo "**** Starting ION..."
CONFIGDIR="./config"
ionstart                           \
    -i ${CONFIGDIR}/245-contactrange.ionrc \
    -l ${CONFIGDIR}/245-contactrange.ltprc \
    -b ${CONFIGDIR}/245-contactrange.bprc  \
    -s ${CONFIGDIR}/245-contactrange.ionsecrc \
    -p ${CONFIGDIR}/245-contactrange.ipnrc


# set contacts & ranges for 30 years from now
echo "**** Adding contacts & ranges..."

contactfile="/tmp/245-contacts.text"

(
    curyear=`date +"%Y"`
    outyear=$((curyear+1))

    date1="${outyear}/4/1-0:0:0"
    date2="${outyear}/4/1-3:14:15"
    date3="${outyear}/4/2-0:0:0"
    date4="${outyear}/4/2-3:14:15"

    echo > $contactfile
    echo "a contact ${date3} ${date4} 1 4 10000" >> $contactfile
    echo "a contact ${date1} ${date2} 1 3 10000" >> $contactfile
    echo "a contact ${date1} ${date2} 1 5 10000" >> $contactfile
    echo "a contact ${date1} ${date2} 1 6 10000" >> $contactfile
    echo "a contact ${date1} ${date2} 1 4 10000" >> $contactfile
    echo "a range ${date1} ${date2} 1 2 1" >> $contactfile
    echo "a range ${date3} ${date4} 1 2 1" >> $contactfile
    echo "a range ${date1} ${date2} 1 3 1" >> $contactfile
    echo "a range ${date1} ${date2} 1 4 1" >> $contactfile
    echo "a range ${date1} ${date2} 1 6 1" >> $contactfile
)


# measure how long ionadmin takes to load the contacts and check for congestion
ionadmin < $contactfile

# count number of contact/range lines to make sure we actually loaded something
echo "l contact" > /tmp/245-cmdfile
numc=`ionadmin < /tmp/245-cmdfile | grep "From" | wc -l`
echo "l range" > /tmp/245-cmdfile
numr=`ionadmin < /tmp/245-cmdfile | grep "From" | wc -l`
rm -f /tmp/245-cmdfile

numadded=$((numc+numr))


# perform the wildcard deletions
echo "d contact * 1 4" > /tmp/245-cmdfile
ionadmin < /tmp/245-cmdfile
echo "d range * 1 2" > /tmp/245-cmdfile
ionadmin < /tmp/245-cmdfile
rm -f /tmp/245-cmdfile


# count number of contact/range lines to make sure we actually deleted something
echo "l contact" > /tmp/245-cmdfile
numc=`ionadmin < /tmp/245-cmdfile | grep "From" | wc -l`
echo "l range" > /tmp/245-cmdfile
numr=`ionadmin < /tmp/245-cmdfile | grep "From" | wc -l`
rm -f /tmp/245-cmdfile

numleft=$((numc+numr))

numdeleted=$((numadded-numleft))

echo "**** Checking contacts..."

if [ $numadded -eq 10 ]; then
    if [ $numdeleted -eq 4 ]; then
        echo "Success, wildcard contact/range deletion worked; ${numadded} added, ${numdeleted} deleted"
        RETVAL=0
    else
        echo "Oh Noes!  wildcard deletion of contact/range failed; ${numadded} added, ${numdeleted} deleted"
        RETVAL=1
    fi
else
    echo "Oh Noes!  contact/range did not load"
    RETVAL=2
fi

# clean up
rm -f $contactfile

exit $RETVAL
