#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisEndCooldown - sets OSIRIS up for a cooldown
#
#SYNOPSIS
#     osirisEndCooldown password
#
#DESCRIPTION
#     Configures all OSIRIS servers so that the instrument is in a safe
#     state to end a cooldown and warmup.  Major tasks performed include:
#        - logging is enabled for all housekeeping servers
#        - detector temperature control is enabled and set to 80K
#        - detectors are reset
#        - all mechanisms are locked
#     All servers should be started before running this script.  The
#     script will report servers that were not configured since they were
#     not running.
#
#OPTIONS
#     password
#          the lockeng password - needed so that the script can unlock
#          servers to configure them (eg. otcs, oprs)
#
#EXAMPLES
#     osirisEndCooldown 123456
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = oids/osds
#          keywords: lock, reset
#     service = otcs
#          keywords: lock, lockeng, trgtmp1, active1, range1,
#                    trgtmp2, active2, timerperiod, logging
#     service = ot1s/ot2s
#          keywords: lock, timerperiod, logging
#     service = op1s/op2s
#          keywords: lock
#     service = om1s/om2s/om3s/om4s/om5s/om6s
#          keywords: lock, lockeng
#     service = oprs
#          keywords: lock, lockeng, setautoon, timerperiod, logging
#
#SCRIPTS CALLED
#     help, syncheck, osirisCheckServer
#
#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:
# 20050202 - 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 int` 
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 = ""

# Get command line arguments
set lockengpw = $1

# Status line
echo ""
echo "Configuring OSIRIS for warmup:"

# OIDS Settings
if (`osirisCheckServer oids` == 1) then
    echo "OIDS:"
    modify -s oids lock=0
#    modify -s oids reset=1
#    modify -s oids reset=1
    modify -s oids powerdown=1
    modify -s oids lock=1
else
    echo "OIDS is not running."
    set unavail = "${unavail}oids "
endif
echo ""

# OSDS Settings
if (`osirisCheckServer osds` == 1) then
    echo "OSDS:"
    modify -s osds lock=0
#    modify -s osds reset=1
#    modify -s osds reset=1
    modify -s osds powerdown=1
    modify -s osds lock=1
else
    echo "OSDS is not running."
    set unavail = "${unavail}osds "
endif
echo ""

# OTCS Settings
if (`osirisCheckServer otcs` == 1) then
    echo "OTCS:"
    modify -s otcs lock=0
    set curval = `show -s otcs -terse lockeng`
    if ($curval == 1) then
	modify -s otcs lockeng=${lockengpw}
    endif
    unset curval
    modify -s otcs trgtmp1=80
    modify -s otcs active1=1
    modify -s otcs range1=3
    modify -s otcs trgtmp2=80
    modify -s otcs active2=1
    modify -s otcs timerperiod=300
    modify -s otcs logging=1
    modify -s otcs lockeng=${lockengpw}
    modify -s otcs lock=1
else
    echo "OTCS is not running."
    set unavail = "${unavail}otcs "
endif
echo ""

# OT1S Settings
if (`osirisCheckServer ot1s` == 1) then
    echo "OT1S:"
    modify -s ot1s lock=0
    modify -s ot1s timerperiod=300
    modify -s ot1s logging=1
    modify -s ot1s lock=1
else
    echo "OT1S is not running."
    set unavail = "${unavail}ot1s "
endif
echo ""

# OT2S Settings
if (`osirisCheckServer ot2s` == 1) then
    echo "OT2S:"
    modify -s ot2s lock=0
    modify -s ot2s timerperiod=300
    modify -s ot2s logging=1
    modify -s ot2s lock=1
else
    echo "OT2S is not running."
    set unavail = "${unavail}ot2s "
endif
echo ""
    
# OP1S Settings
if (`osirisCheckServer op1s` == 1) then
    echo "OP1S:"
    modify -s op1s lock=1
else
    echo "OP1S is not running."
    set unavail = "${unavail}op1s "
endif
echo ""

# OP2S Settings
if (`osirisCheckServer op2s` == 1) then
    echo "OP2S:"
    modify -s op2s lock=1
else
    echo "OP2S is not running."
    set unavail = "${unavail}op2s "
endif
echo ""

# OMS Settings
# leave mechanisms wherever they are
foreach n (1 2 3 4 5 6)
    if (`osirisCheckServer om${n}s` == 1) then
	echo "OM${n}S:"
	modify -s om${n}s lock=0
	set curval = `show -s om${n}s -terse lockeng`
	if ($curval == 0) then
	    modify -s om${n}s lockeng=${lockengpw}
	endif
	unset curval
	modify -s om${n}s lock=1
    else
	echo "OM${n}S is not running."
    set unavail = "${unavail}om${n}s "
    endif
    echo ""
end

# OPRS Settings
if (`osirisCheckServer oprs` == 1) then
    echo "OPRS:"
    modify -s oprs lock=0
    set curval = `show -s oprs -terse lockeng`
    if ($curval == 1) then
	modify -s oprs lockeng=${lockengpw}
    endif
    unset curval
    modify -s oprs setautoon=2
    modify -s oprs timerperiod=300
    modify -s oprs logging=1
    modify -s oprs lockeng=${lockengpw}
    modify -s oprs lock=1
else
    echo "OPRS is not running."
    set unavail = "${unavail}oprs"
endif
echo ""

if ("${unavail}" != "") then
    echo "WARNING:"
    echo "The following servers were not running and could not be"
    echo "configured so that OSIRIS is in a safe mode for the warmup:"
    echo "-> "${unavail}
else
    # verify that the detector controllers are powered down
    echo "VERIFY:"
    set imagasic = `show -s oids -terse asicpwr`
    set specasic = `show -s osds -terse asicpwr`
    if ($imagasic == 0 && $specasic == 0) then
      echo "NOTICE:"
      echo "All servers were configured so that OSIRIS is in a safe mode"
      echo "and ready for the warmup to begin."
    else
      echo "WARNING:"
      echo "SPEC Power is $specasic"
      echo "IMAG Power is $imagasic"
      echo "Power must be 0 for both detectors BEFORE cooldown"
      echo "Control with POWERDOWN keywords and verify with ASICPWR keywords"
    endif
endif

goto done

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

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