#!/bin/csh -f
#+
#osirisAbortOnly - OSIRIS library of scripts
#
#NAME
#     osirisAbortOnly - aborts current IMAG or SPEC exposure in dataset
#
#SYNOPSIS
#     osirisAbortOnly mode [-t #] [-m]
#
#DESCRIPTION
#     this script is intended to be called by the OSIRIS Dataset Execution 
#     Client (ODEC) in order to abort the current IMAG/SPEC exposure, and then
#     disable IMAG/SPEC exposures for the remainder of the dataset.  it sets
#     the appropriate keyword in the OSIRIS Global Server (OGS) to trigger this
#     behavior and sends a message acknowledging receipt of the abort to the
#     observer.
#
#OPTIONS
#     mode    specfies detector, "SPEC" or "IMAG".
#
#     -t #
#          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 ";".
#
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#EXAMPLES
#     osirisAbortOnly imag -t 123
#          Abort the current IMAG exposure and attach "123" to all messages to OGS
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = osiris
#          keywords: abortimagnow/abortspecnow, scriptwarn
#
#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
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20041124 - MB: Initial version created
#

# 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 {[-t] int} {[-m]}`
#unset noglob

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

# End of help/syncheck boiler plate.

# Set up to trap control-C
onintr ctrlc

# Decide if we should use 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 "${0} ${mode}: Specified mode must be SPEC or IMAG - aborting."
	exit 1
	breaksw
endsw
shift
##echo mode=${mode}
##echo M=${M}

# Set default variable values
set cmdpre = ""
set cmdsuf = ""
set idnum = 0

# Check for more flags
set noglob
while ($#argv != 0)
##echo "${0} ${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 "${0} ${mode}: Invalid script ID number specified <${2}> - using ${idnum}."
	    endif
	    unset Checkstatus
	    breaksw
	case -m:
	    set sim
	    set cmdpre = "echo ${0} ${mode}: sim:"
	    set cmdsuf = "-m"
	    breaksw
	default:
	    echo "${0} ${mode}: Invalid command line flag $1 specified."
	    echo "${0} ${mode}: Usage: $0 mode [-t #] [-m]"
	    # set the error code for an error with command line input
	    exit 3
	    breaksw
    endsw
    shift
end
unset noglob


# Set the appropriate keywords in OGS
$cmdpre modify -s osiris abort${mode}only=1
$cmdpre modify -s osiris ${M}abort=1

# Notify the observer that the abort has been initiated
set msg = "${0} ${mode}: <Abort ${mode} Only> has been sent.  Please wait for the current ${mode} exposure to abort."
echo $msg
$cmdpre modify -s osiris silent scriptwarn="${idnum};$msg"
unset msg

goto done

ctrlc:
# Block of code to handle interrupts.
exit 1

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