#!/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
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                run eget tests using 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 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="$OPTARG"
       ;;

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

echo "Running Channel Archiver Test"

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

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

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


echo "Channel Archiver client Test"

EXPECTED=$TOP/expected.txt

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

mkdir "${RESULTS_DIR}"
RESULTS="${RESULTS_DIR}/results.txt"
$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


# Check eget can be found. If eget not supplied look in path.

if [[ "$EGET" != "" ]]
then
    if [ ! -f $EGET ]; then
        echo "Can't find specified eget: $EGET"
        echo "Skipping eget test."
        echo "Do run_test -h for details."
        exit 1
    fi    
else
    EGET="eget"
    which $EGET >/dev/null 2>/dev/null
    if [[(($? != 0))]]; then
        echo "Can't find eget in PATH. Skipping eget test."
        echo "Make sure eget is in your path or specify using EGET variable or -e option"
        echo "Do run_test -h for details."
        exit 1
    fi
fi

echo "Test using eget"

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

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


