#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisConnect - sets up OSIRIS software when instrument 
#                     is connected (in AO room or at RT3)
#
#SYNOPSIS
#     osirisConnect
#
#DESCRIPTION
#     Prepares OSIRIS software for use after a move from the deck
#     to the AO room or vice versa.  Performs the following:
#       - Starts all servers and initializes them.  
#       - Starts logging
#       - Homes mechanisms (if ok to move)
#       - Locks mechanisms
#
#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
#     list of the servers and keywords used, for example:
#
#     service = osiris
#          keywords: sfilter/ifilter, sscale
#
#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
#     other errors...
#
#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.

set noglob
#set CheckStatus = `syncheck -command $0 $* -pattern text` 
set CheckStatus = OK
unset noglob

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

# End of help/syncheck boiler plate.

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

# Set default variable values
set unavail = ""
set oktoinitspec = 0  # flag for initializing detectors ok
set spectempnum = 1   # temp sensor number to check for detector init
set spectempmax = 80  # maximum temperature to allow detector init
set oktoinitimag = 0  # flag for initializing detectors ok
set imagtempnum = 2   # temp sensor number to check for detector init
set imagtempmax = 80  # maximum temperature to allow detector init
set oktomove = 0     # flag for mech moves ok
set mottempnum = 4   # temp sensor number to check for mech moves
set mottempmax = 80  # maximum temperature to allow mech moves
set waittime = 0
set waitmax = 5

# loop through each server
foreach service (osiris oprs ot1s ot2s otcs om1s om2s om3s om4s om5s om6s osds oids op1s op2s)
    if (`osirisCheckServer ${service}` == 1) then
	echo "${service}:"
	modify -s ${service} lock=0

	# put special server processing here
	switch (${service})
	    case osiris:
		osiris stop ogs
		breaksw
	    case osds:
	    case oids:
		modify -s ${service} powerdown=1
	        sleep 1
                set asicpwr = `show -s ${service} -terse asicpwr`
                while ($asicpwr == 1 && $waittime < $waitmax)
                  echo "ASIC Power is $asicpwr.  Attempt # is $waittime"
                  echo "Attempting powerdown again..."
                  modify -s ${service} powerdown=1
                  sleep 1
                  @ waittime++
                  set asicpwr = `show -s ${service} -terse asicpwr`
                end
		osiris stop ${service}
		breaksw
	   case ot1s:
	   case ot2s:
	   case otcs:
		osiris stop ${service}
		breaksw
	   case oprs:
		osiris stop ${service}
		breaksw
	   case om1s:
	   case om2s:
	   case om3s:
	   case om4s:
	   case om5s:
	   case om6s:
		osiris stop ${service}
		breaksw
	    case op1s:
	    case op2s:
		# need to lockeng=0 here
		modify -s ${service} pwstatall=0
		osiris stop ${service}
		breaksw
	endsw
    else
	echo "ERROR: ${service} is not running."
	set unavail = "${unavail}${service} "
    endif 

end

goto done

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

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