#!/bin/sh

PROF1=YAGS:IN10:241
PROF2=YAGS:IN10:841
PROF3=OTRS:IN10:465
PROF4=OTRS:IN10:471

function printUsage {
    echo "Usage: ./testBkhfIn [options]"
    echo " -p <number>: 2 for PM02 or 3 for PM03 (PM01 is default)"
    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 setPM02 {
    PROF1=OTRS:IN10:571
    PROF2=OTRS:IN10:621
    PROF3=OTRS:IN10:711
    PROF4=PROF:IN10:711
}

function setPM03 {
    PROF1=YAGS:IN10:921
    PROF2=YAGS:IN10:995
    PROF3=PROF:IN10:921
    PROF4=PROF:IN10:995
}


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 ===

pm=1
while getopts "hr:p:" opt; do
    case $opt in
	h)
	    printUsage
	    exit 0
	    ;;
	p)
	    pm=$OPTARG
	    if [ $pm == "2" ]; then
		setPM02
	    elif [ $pm == "3" ]; then
		setPM03
	    fi
	    ;;
	\?)
	    echo "Invalid option: -$OPTARG"
	    exit -1
	    ;;
	:)
	    echo "Option -$OPTARG requires an argument."
	    exit -1
	    ;;
    esac
done


for pv in $PROF1 $PROF2 $PROF3 $PROF4; 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;

