#!/bin/csh -f
#+
#WFREADY - OSIRIS library of scripts
#
#NAME
#     wfready - checks and waits for detector to be ready before starting exposure
#
#SYNOPSIS
#     wfready mode [-t #] 
#
#DESCRIPTION
#     waits for SPEC or IMAG detector to be ready to start a new exposure
#     (by monitoring sready or iready keyword) and then exits.
#     if waiting period exceeds a threshold, then wfready times out and exits
#     with an error code.
#
#OPTIONS
#     mode
#          specifies detector, can be "SPEC" or "IMAG"
#
#     -t NUM
#          ID number to attach to all script generated errors, warnings, and
#          questions that are passed to the OGS.  The ID number is separated
#          from the rest of the message with a ";".
#
#EXAMPLES
#     wfready imag
#          Waits for IMAG detector to be ready
#
#ENIVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = osiris
#          keywords: sready/iready
#
#SCRIPTS CALLED
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     4 - time out error
# 
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20041018 - MB: Copied from wfgo and adapted.
#
 
# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0 | more
    exit $status
endif

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

set noglob
set CheckStatus = `syncheck -command $0 $* -pattern [SPEC,spec,IMAG,imag] {text int}` 
unset noglob

if ("$CheckStatus" != "OK") then
    echo 'Type "'${0}' -h" for more help.'
    #help $0 | more
    # set appropriate exit status for syncheck error
    exit 2
endif

# End of help/syncheck boiler plate.

# Check for xcount trigger level.

#if (! ${?XCOUNT_TRIGGER} ) setenv XCOUNT_TRIGGER 60

#  set up to trap control-C

onintr reset

# Set default variable values
set idnum = ""
set fcmd = $0
set cmd = ${fcmd:t}

# Decide if we should check SPEC or IMAG
set mode = $1
switch ($mode)
    case "SPEC":
    case "spec":
	set M = "s"
	set mode = "spec"
	breaksw
    case "IMAG":
    case "imag":
	set M = "i"
	set mode = "imag"
	breaksw
    default:
	echo "${cmd} ${mode}: Specified mode must be SPEC or IMAG"
	# set appropriate exit status for command line error
	exit 3
	breaksw
endsw
shift
 
# Check for more flags
set noglob
while ($#argv != 0)
##echo "${cmd} ${mode}: Checking ${1}..."
    switch ($1)
	case -t:
	    set CheckStatus = `syncheck -command $1 $2 -pattern int`
	    if ("$CheckStatus" == "OK") then
		set idnum = $2
		shift
	    else
		echo "${cmd} ${mode}: Invalid script ID number specified <${2}> - using default."
	    endif
	    unset Checkstatus
	    breaksw
	default:
	    echo "${cmd} ${mode}: Invalid command line flag $1 specified."
	    echo "${cmd} ${mode}: Usage: $0 mode [-t #]"
	    # set the error code for an error with command line input
	    exit 2
	    breaksw
    endsw
    shift
end
unset noglob

##echo idnum = ${idnum}

# How long should we wait before giving up?
set wait = 60

# Start waiting...
echo "${cmd} ${mode}: Waiting for ${mode} detector ready."
set count = 0
set test = `show -s o${M}ds -terse ready`

while( ($test == 0) && ($count <= $wait) )
    @ count++
    echo -n .
    sleep 1
    set test = `show -s o${M}ds -terse ready`
end

if ( $test == 1 ) then
    echo "OK"
else
    echo ""
    echo "${cmd} ${mode}: ${mode} ready timed out."
    # set appropriate exit status for time out
    exit 3
endif

exit
 
reset:
###put stuff here
# set appropriate exit status for abort
exit 1
