#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     lamp
#
#SYNOPSIS
#     lamp [name [action]] -t # -m
#
#DESCRIPTION
#     Controls the AO bench calibration light sources
#
#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).
#
#     name 
#          one of: argon, krypton, neon, xenon, white, variable dome.  
#          If no argument, prints status of all lamps
#
#     action
#          one of: 0, 1, or status.  If no argument,
#          prints status named lamp
#
#EXAMPLES
#     lamp junk1 -t 123
#          What would happen?
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     apc:
#            neon, argon, krypton, xenon, spectral
#
#SCRIPTS CALLED
#     
#
#     help, 
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     3 - error parsing command line input
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20120131     jlyke     Original version
# 20120531     jlyke     Switched to apc keyword service
# 20130324     jlyke     Added variable intensity lamp
# 20190613     jlyke     Added dome lamps
# 20200711     jlyke     Chng flat servics to dcs
# 20201215     jlyke     Chng apc service to telsimpower
# 20210507     jlyke     Fix dome lamp handling, ensure -debug takes 
#                        no action
#
#-----------------------------------------------------------------------
# current mapping of telsimpower keywords
#                  outlet1_name = OSIRIS Argon Lamp
#                       outlet1 = Off
#                  outlet2_name = OSIRIS Krypton Lamp
#                       outlet2 = Off
#                  outlet3_name = OSIRIS Neon Lamp
#                       outlet3 = Off
#                  outlet4_name = OSIRIS Xenon Lamp
#                       outlet4 = Off
#                  outlet5_name = OSIRIS White Light
#                       outlet5 = Off
#                  outlet6_name = OSIRIS Variable Lamp
#                       outlet6 = On
#                  outlet7_name = Outlet 7
#                       outlet7 = Off
#                  outlet8_name = Outlet 8
#                       outlet8 = Off
#
# if no args passed, return the status
# currently assumes apc/telsimpower and ofls keyword services are running
if ( $#argv == 0 ) then
  echo "                      ===Internal==="
  foreach n (1 2 3 4 5)
    set str = outlet${n}
    show -s telsimpower ${str}_name $str
  end
  set intensity = `show -s ofls -terse intensity`
  echo "                      variable = $intensity"
  echo "                      ===Dome======="
#  show -s dcs flamp1 fpower1 flamp2 fpower2
  show -s dcs flamp1 flamp2 fpower
endif

# Boiler plate for "-h" support for command autohelp.

if ("$1" =~ -h*) then
    help $0 | more
    exit $status
endif

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

# define usage
set buf = $0
set cmd = $buf:t
set usage = "${cmd}: Usage: ${cmd} [-t #] [-m] [neon|argon|krypton|xenon|spectral|flat|white|arc|all|variable|dome] [on|off|status|10-100|0-20]"

# Set default variable values
set cmdpre = ""
set cmdsuf = ""
set lamps = ()
set variable = 0
set dome = 0
set status = 0

# Check for 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:
	case -debug:
            set sim
            set cmdpre = "echo ${cmd}: sim:"
            set cmdsuf = "-m"
            breaksw
        case neon:
	case ne:
	    set lamps = ($lamps outlet3)
            breaksw
        case argon:
	case ar:
	    set lamps = ($lamps outlet1)
            breaksw
        case krypton:
	case kr:
	    set lamps = ($lamps outlet2)
            breaksw
        case xenon:
	case xe:
	    set lamps = ($lamps outlet4)
            breaksw
        case spectral:
	case white:
	case flat:
	    set lamps = ($lamps outlet5)
            breaksw
        case v*:
            set variable = 1
	    breaksw
        case arc*:
	    set lamps = (outlet1 outlet2 outlet3 outlet4 )
            breaksw
	case all:
	    set lamps = (outlet1 outlet2 outlet3 outlet4 outlet5)
            breaksw
        case d*:
            set dome = 1 
            breaksw
        case on:
        case On:
            set action = On
            set level = 100
            breaksw
        case [0-9]:
        case [0-9].[0-9]*:
	case [1-9][0-9]:
	case [1-9][0-9].[0-9]*:
	case 100:
	    set action = On
	    set level = $1
	    breaksw
        case off:
        case Off:
            set action = Off
	    breaksw
        case stat*:
            set status = 1
	    breaksw
        default:
            echo "${0}: Invalid command line flag $1 specified."
            echo "${usage}"
            # set the error code for an error with command line input
            exit 3
            breaksw
    endsw
    shift
end
unset noglob
if ( $?action ) then
  # turn lamp(s) on or off
  if ( $dome == 1 ) then
    # external to AO: do not need AO HEPA filter on
    if ( $action == "On" ) then
      # verify level [0-20], where 0=bright, 20=faint
      if (`compareNumbers $level ">=" 0.0` && \
          `compareNumbers $level "<=" 20.0`) then
        echo "setting dome flat power level to $level"
        $cmdpre modify -s dcs fpower=$level
      else
        echo "Specified power level is outside the allowed range:  "
        echo "    High =  0.0  (brightest) "
        echo "    Low  = 20.0  (faintest) "
        echo "Please set power level to a value of 0-20. "
        exit 1
      endif
      # turn ON the domeflat lamps
      $cmdpre modify -s dcs flamp=on
      $cmdpre modify -s oids comment="Domeflat=on, level=$level"
    else
      # turn OFF the domeflat lamps
      $cmdpre modify -s dcs flamp=off
      $cmdpre modify -s oids comment="Domeflat=off"
    endif
  else
    # internal to AO: do need the AO HEPA filter on
    if ( $action == On ) then 
      if ( $?sim ) then
        $cmdpre modify -s ao obhpaon=1
      else 
        # verify that the HEPA filter is on....
        set hepa = `show -s ao -terse obhpasta`
        while ( $hepa == off )
          echo "Turning HEPA filter on..."
          $cmdpre modify -s ao obhpaon=1
          sleep 1
          set hepa = `show -s ao -terse obhpasta`
        end
      endif
    endif
    foreach lamp ($lamps)
      if ( $?sim ) then
        $cmdpre modify -s telsimpower $lamp=$action
      else
        set lampstat = `show -s telsimpower -terse $lamp`
        while ( $lampstat != $action )
          $cmdpre modify -s telsimpower $lamp=$action
          sleep 1
          set lampstat = `show -s telsimpower -terse $lamp`
        end
      endif
    end
    if ( $variable == 1 ) then 
      if ( $action == On ) then
        set key = lamp
      else
        set key = lampoff
        set level = 1
      endif
      $cmdpre modify -s ofls $key=$level
    endif
  endif
else
  # show status of lamp(s)
  $cmdpre show -s telsimpower $lamps
  if ( $variable == 1 ) then
     set intensity = `show -s ofls -terse intensity`
     echo "                      variable = $intensity" 
  endif
  if ( $dome == 1 ) then
    #show -s dcs flamp1 fpower1 flamp2 fpower2
    show -s dcs flamp1 flamp2 fpower
  endif
endif
  

goto done

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

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