#!/bin/sh

function printUsage {
    echo "Usage: ./testBkhfIn [options]"
    echo " -h         : print this help"
    echo ""
    echo "This test each KM2004 input channel. It goes through"
    echo "each channel waiting for the channel to turn on and"
    echo "then off before going onto the next channel."
}

function testChannel {
    channel=$1
    value="0"
    echo -n "Waiting for signal on channel "$channel": "
    while [ $value == "0" ]; do
	value=`caget -n -t $channel 2> /dev/null`
    done
    echo -n " ON"
    while [ $value != "0" ]; do
	value=`caget -n -t $channel 2> /dev/null`
    done
    echo " OFF"
}

#=== Main ===

while getopts "hr:" opt; do
    case $opt in
	h)
	    printUsage
	    exit 0
	    ;;
	\?)
	    echo "Invalid option: -$OPTARG"
	    exit -1
	    ;;
	:)
	    echo "Option -$OPTARG requires an argument."
	    exit -1
	    ;;
    esac
done


for pv in YAGS:IN10:241 YAGS:IN10:841 OTRS:IN10:465 OTRS:IN10:471; do
    for field in IN_LMTSW OUT_LMTSW FLT1_IN FLT1_OUT FLT2_IN FLT2_OUT SOLENOID_ERR SPARE_STATUS; do
	testChannel $pv:$field
    done;
done;

