#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisHomeMech
#
#SYNOPSIS
#     osirisHomeMech arg1 mode [-t #] [-m] etc...
#
#DESCRIPTION
#     Homes and unlocks mechanisms if their servers are running.  
#         This is a portion of osirisSetupNight
#
#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
#     osirisHomeMech 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
# 20060201 - jlyke: Created from osirisSetupNight to include only the 
#                   mechanism homing portions
# 20111002 - jlyke: Remove om4s from homing due to lenslet mask issue 
# 20120202 - jlyke: Restore om4s
# 20181027 - jlyke: Remove om4s from homing due to lenslet mask issue
# 20190620 - 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.

set noglob
set CheckStatus = `syncheck -command $0 $* -pattern` 
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


# confirm OSIRIS_DATA_ROOT
if (`printenv OSIRIS_DATA_ROOT` == '') then
    echo "$OSIRIS_DATA_ROOT not set.  Please set to appropriate directory."
    exit
endif 

set DATA_ROOT = ${OSIRIS_DATA_ROOT}

# set default variable values
set unavail = ""

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

# confirm home mechs
echo -n "Do you want to home the mechansims [Y/N]:"
set confirm=$<

# unlock motors and home
foreach service (om1s om2s om3s om4s om5s om6s)
    if (`osirisCheckServer $service` == 1) then
	echo "${service}:"
	modify -s ${service} lock=0
	if (${confirm} == "Y" || ${confirm} == "y") then 
	     modify -s ${service} home=1
	endif
    else 
        echo "ERROR: ${service} is not running."
        set unavail = "${unavail}${service} "	
    endif
end
if ("${unavail}" != "") then
    echo "WARNING:"
    echo "The following motor servers were not running and"
    echo "the mechanism controlled by each one could not be homed:"
    echo "-> "${unavail}
else
    if (${confirm} == "Y" || ${confirm} == "y") then 
	echo "All mechanisms unlocked and homed."
    else
	echo "All mechanisms unlocked."
    endif
endif

goto done

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

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