#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisGetSetnum
#
#SYNOPSIS
#     osirisGetSetnum IMAG|SPEC [-t #] [-m] 
#
#DESCRIPTION
#     Returns the current set number based upon the data directories.
#	Useful for recovering detector server crashes.  Normally, the 
#       set number is returned to 0 upon a restart which can confuse
#       the data directories.
#
#OPTIONS
#     -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).
#
#     others?
#
#EXAMPLES
#     script_name junk1 -t 123
#          What would happen?
#
#ENVIRONMENT VARIABLES
#     list of environment variables used
#
#FILES
#     list of files used
#
#SERVERS & KEYWORDS
#     
#     oids   / setnum
#     osds   / setnum
#     osiris / setnum  (higher between oids and osds)
#     osiris / isetnum (meta-keyword)
#     osiris / ssetnum (meta-keyword)
#
#SCRIPTS CALLED
#     list of the scripts called by this script, for example:
#
#     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 - no data directories set for this date and this user
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 2006 Feb 02 jlyke: original adapted from osirisSetupNight
#

# 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,OSDS,osds,IMAG,imag,OIDS,oids]` 
unset noglob

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

set buf = $0
set cmd = $buf:t

# End of help/syncheck boiler plate.

# Set up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Handle input
# Decide if we should use SPEC or IMAG
set mode = $1
switch ($mode)
    case "SPEC":
    case "spec":
    case "OSDS":
    case "osds":
        set M = "s"
        set mode = "spec"
        breaksw
    case "IMAG":
    case "imag":
    case "OIDS":
    case "oids":
        set M = "i"
        set mode = "imag"
        breaksw
    default:
        osirisScriptMsg -T "${cmd} ${mode}: Specified mode must be SPEC or IMAG- aborting."
        exit 3
        breaksw
endsw

# set default variable values

# get current date
set date=`date -u "+%y%m%d"`

# we assume this is run at night while observering and that
# the global server is alive and well
set soutdir = `show -s osiris -terse soutdir`
set ioutdir = `show -s osiris -terse ioutdir`

set count = 0
# Determine the current highest setnum
switch (${mode})
    case spec:
	set service = osds
	set count = `ls -1 ${soutdir} | wc -l`
	if ( ${count} == 0 ) then
	    set setnum = 0
	else
	    set setnum = `ls -1 ${soutdir} | cut -c10-12 | tail -1`
	endif
        breaksw
    case imag:
	set service = oids
	set count = `ls -1 ${ioutdir} | wc -l`
	if ( ${count} == 0 ) then
	    set setnum = 0
	else
	    set setnum = `ls -1 ${ioutdir} | cut -c10-12 | tail -1`
	endif
        breaksw
endsw

# print results to screen
echo $setnum

goto done

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

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