#!/bin/bash
#!-*- sh -*-
#
# Copyright: See the COPYRIGHT that is included with this distribution.
#            EPICS exampleCPP is distributed subject to a Software License
#            Agreement found in file LICENSE that is included with this
#            distribution.
#
# Abs: run_test is a simple test script to test the Channel Archiver
#      service. It starts a server, sends a query and checks the results
#      against a file. 
#
# Usage: see help (gethist -h or gethist --help) for options                   
# 
# ----------------------------------------------------------------------------
# Auth: Dave Hickin (david.hickin@diamond.ac.uk)
# ============================================================================


TOP=$(dirname $0)

Error()
{
    echo "$1"
    exit 1
}

CheckForFile()
{
    test -f $1 ||  { echo "$1 not found."; Error "TEST ABORTED."; }
}

usage()
{
cat << EOF
Usage
    run_test [OPTIONS]
    run_test -h
    see run_test -h for details
EOF
}

showhelp()
{
cat << EOF
Name
    run_test

EOF

usage

cat << EOF

Options
    -h,--help              display help
    -v                     verbose.    
    -n=NUM_LINES           show first NUM_LINES lines. 
    -e=EGET_PATH           use EGET_PATH as path for eget. 

Description
    run_test tests the Channel Archiver Service by starting a server and
    issuing requests using the Channel Archiver Service client and eget and
    comparing the actual results with those expected. eget must be in your
    path, or given by the EGET_PATH environmental variable or supplied by the
    -e option.
EOF

exit 0
}

SERVICE="archiveService" 

DEBUG="FALSE"
NUM_LINES=4
OPTIONS="-q"
EGET_OPTIONS=
DIFF_OPTIONS="-b"

while getopts :hvn:e: OPTION
do
    case $OPTION in
    h) showhelp
       ;;

    v) DEBUG="TRUE"
       OPTIONS=
       DIFF_OPTIONS=-s        
       ;;

    n) DEBUG="TRUE"
       NUM_LINES="$OPTARG"
       ;;

    e) EGET_PATH="$OPTARG"
       ;;

    ?) echo "Invalid option or missing argument."
       usage
       exit 1
       ;;
    esac
done
shift $((OPTIND-1))

echo "Running Channel Archiver Test"

RESULTS="$TOP"/results.txt
if [[ -e "$RESULTS" ]]
then
    echo "Deleting previous results ($RESULTS)."   
    rm "$RESULTS" 
fi

EGET_RESULTS="$TOP"/eget_results.txt
if [[ -e "$EGET_RESULTS" ]]
then
    echo "Deleting previous results ($EGET_RESULTS)."   
    rm "$EGET_RESULTS"
fi


ARCH_INDEX="$TOP"/../data/fredjanet/index
CheckForFile "$ARCH_INDEX"

xterm -e "$TOP""/../start_server" "$ARCH_INDEX" "$SERVICE" &
trap 'kill "$!"' SIGINT SIGTERM EXIT
sleep 1


echo "Channel Archiver client Test"

EXPECTED=$TOP/expected.txt

GET_HIST="$TOP"/../gethist
CheckForFile $GET_HIST

$GET_HIST -S "$SERVICE" -n $OPTIONS -s "Sep 21 2005" -e "Sep 22 2005" -f "$RESULTS" fred


if [[ "$DEBUG" = "TRUE" ]]
then
    echo "Compare results with expected results:"
    echo "Expected:"
    head -n $NUM_LINES $EXPECTED
    echo "..."
    echo "Got:"
    head -n $NUM_LINES $RESULTS
    echo "..."
fi

EXPECTED=$TOP/expected.txt
CheckForFile $EXPECTED
diff --brief "$RESULTS" $EXPECTED

if [[(($? = 0))]]
then
    echo "PASS"
else
    echo "FAIL"
fi

echo "Test using eget"

if [[ "$EGET_PATH" != "" ]]
then
    EGET_PATH+='/'
fi


EGET=$EGET_PATH"eget"

$EGET $EGET_OPTIONS -s "$SERVICE" -a "starttime=496105200" -a "endtime=496191600" -a "entity=fred" > $TOP/"eget_results.txt"

EGET_EXPECTED=$TOP/eget_expected.txt
CheckForFile $EGET_EXPECTED

if [[ "$DEBUG" = "TRUE" ]]
then
    echo "Compare results with expected results:"
    echo "Expected:"
    head -n $NUM_LINES $EGET_EXPECTED
    echo "..."
    echo "Got:"
    head -n $NUM_LINES $EGET_EXPECTED
    echo "..."
fi

diff --brief $DIFF_OPTIONS $EGET_RESULTS $EGET_EXPECTED

if [[(($? = 0))]]
then
    echo "PASS"
else
    echo "FAIL"
fi


