#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisSetupMech - configures OSIRIS IMAG or SPEC mechanisms for an exposure
#
#SYNOPSIS
#     osirisSetupMech mode [-t #] [-f name] [-x #] [-m]
#
#DESCRIPTION
#     setup filter and/or scale for SPEC or IMAG
#     ...ADD MORE LATER...
#
#OPTIONS
#     mode
#          specfies "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 ";".
#
#     -f name
#          filter to use
#
#     -x name
#          scale to use, only applicable if mode=SPEC  
#
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#EXAMPLES
#     setup_mech imag -t 12345 -f Kbb
#          Selects Kbb filter for IMAG, and uses ID=12345 for all messages
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = osiris
#          keywords: sfilter/ifilter, sscale
#
#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
#     10 - error setting filter
#     11 - error setting spec scale
#
#SEE ALSO
#     ???
#-
#
# Modifcation History:
# 20041116 - MB: Initial version created
# 20050824 - MB: Added wfmech before trying to move mechs
# 20051007 - MB: Changed instances of "z" to "Z"
# 20080304 - JLW: Updated filter list with new pupil filters
# 20111002 - jlyke: Work-around broken om4s lenslet mask stage
# 20120202 - jlyke: Restore om4s
#

# 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.

### More advanced argument checking is required, see below

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

#if ("$CheckStatus" != "OK") then
#    help $0
#    exit 1
#endif

# End of help/syncheck boiler plate.

# Set up to trap control-C
onintr break

# Set default variable values
set fcmd = $0
set cmd = ${fcmd:t}
set cmdpre = ""
set cmdsuf = ""
set idnum = 0
set scalelist = "[0.020,0.02,0.035,0.050,0.05,0.100,0.10,0.1]"

# Decide if we should use SPEC or IMAG
set mode = $1
switch ($mode)
    case "SPEC":
    case "spec":
	set M = "s"
	set mode = "spec"
	set filtlist = "[Jbb,Hbb,Kbb,Zbb,Kcb,Jn1,Jn2,Jn3,Jn4,Hn1,Hn2,Hn3,Hn4,\
			  Hn5,Kn1,Kn2,Kn3,Kn4,Kn5,Zn4,Kc3,Kc4,Kc5,Drk]"
	breaksw
    case "IMAG":
    case "imag":
	set M = "i"
	set mode = "imag"
	set filtlist = "[Opn,Jbb,Hbb,Kbb,Zbb,Jn1,Jn2,Jn3,Hn1,Hn2,Hn3,Hn4,Hn5,\
			  Kn1,Kn2,Kn3,Kn4,Kn5,Zn3,Drk]"
	breaksw
    default:
	echo "${cmd} ${mode}: Specified mode must be SPEC or IMAG - aborting."
	exit 3
	breaksw
endsw
shift
##echo mode=${mode}
##echo M=${M}

# 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 ${idnum}."
	    endif
	    unset Checkstatus
	    breaksw
	case -f:
#	    set CheckStatus = `syncheck -command $1 ${2} -pattern ${filtlist}`
            set CheckStatus = "OK"
	    if ("$CheckStatus" == "OK") then
		set filter = $2
                set filter = `echo $filter | sed -e 's/Drk-/Drk/g'`
		shift
	    else
		set errmsg = "${cmd} ${mode}: Invalid filter specified <${2}> - aborting."
		echo $errmsg
		unset errmsg
		exit 2
	    endif
	    unset Checkstatus
	    breaksw
	case -x:
	    ##echo $1 $2
	    set CheckStatus = `syncheck -command $1 $2 -pattern ${scalelist}`
	    if ("$CheckStatus" == "OK" && "$mode" == "spec") then
		set sscale = $2
		shift
	    else
		if ("$mode" == "imag") then
		    set errmsg = "${cmd} ${mode}: Cannot set scale in IMAG mode - aborting."
		else
		    set errmsg = "${cmd} ${mode}: Invalid scale specified <${2}> - aborting."
		endif
		echo $errmsg
		unset errmsg
		exit 2
	    endif
	    unset Checkstatus
	    breaksw
	case -m:
	    set sim
	    set cmdpre = "echo ${cmd} ${mode}: sim:"
	    set cmdsuf = "-m"
	    breaksw
	default:
	    echo "${cmd} ${mode}: Invalid command line flag $1 specified."
	    echo "${cmd} ${mode}: Usage: $0 mode [-t #] [-f name] [-x #] [-m]"
	    # set the error code for an error with command line input
	    exit 3
	    breaksw
    endsw
    shift
end
unset noglob

###should do something if there are no arguments... currently, script does nothing
###and exits successfully

# Make sure nothing is moving before trying to move mechanisms
$cmdpre wfmech $mode

# Set filter
if ($?filter) then
    # $cmdpre ${M}filter $filter ### Direct subserver command
    $cmdpre modify -s osiris ${M}filter="$filter"
    set error = $status
    if ($error != 0) then
	osirisScriptMsg -T "${cmd}: Error setting {$M}filter keyword (modify error #${error})."
	exit 10
    endif
endif

if ($?sscale) then
    if ("$mode" == "spec") then
	# $cmdpre sscale $sscale ### Direct subserver command
	$cmdpre modify -s osiris sscale="$sscale"
	set error = $status
	if ($error != 0) then
	    osirisScriptMsg -T "${cmd}: Error setting sscale keyword (modify error #${error})."
	    exit 11
	endif
    endif
endif

goto done

break:
# Block of code to handle interrupts.
###modify this stuff for OSIRIS
exit 1

done:
# is there anything that needs to go here?
exit