#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     trickDichroic - shows or sets the TRICK dichroic poistion
#
#SYNOPSIS
#     trickDichroic [open|home|kBand|hBand|h2band|hAnnular] [-key] [-pos|-numeric]
#
#DESCRIPTION
#     Shows or sets the named position of the TRICK dichroic (OBTD).
#     Script is blocking and will wait for stage to reach its position or a
#     timeout (currently set to 70 seconds).  As of Aug 2020, the stage takes
#     about 62 seconds to go between the extreme positions.  Script only checks 
#     for ao.OBTDSTST "INPOS" or "FAULT" stati.
#
#OPTIONS
#     namedposition - one of open, home, kBand, hBand, h2band, hAnnular
#                     N.B. open/home and hBand/h2band are identical
#     -key          - if given, script will print the name of the keyword
#                     ao.OBTDNAME or ao.OBTD if -pos/-numeric also given
#     -pos|-numeric - show the position of the TRICK dichroic in mm.  
#
#EXAMPLES
#     trickDichroic
#          open
#     trickDichroic kband
#          setting OBTDNAME = kBand (wait)
#          Waiting for TRICK dichroic.  Timeout set to 70 seconds
#     trickDichroic
#          kBand
#     trickDichroic -key
#          ao.OBTDNAME
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = ao
#          keywords: OBTDNAME, OBTD, OBTDSTST
#
#SCRIPTS CALLED
#     help
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     other errors...
#
#SEE ALSO
#     none
#
#-
#
# Modification History:
# 20200819 - jlyke: Original
#=========================================================================
# defaults
set service = ao
set defaultkey = OBTDNAME
set stage = "TRICK dichroic"
set key = $defaultkey
set echokwd = 0
set timeout = 70
set count = 0
set mycmd = `basename $0`
set usage = "$mycmd [open|home|kBand|hBand|h2band|hAnnular] [-key] [-pos|-numeric]"

# catch a plea for help
if ( $#argv == 1 && $1 == "help" ) then
  help $0 | more
  echo " USAGE:"
  echo "$usage"
  exit $status
endif

# parse args
while ($#argv > 0 )
  # make args lower case.  note that "-n" is an option to echo so cannot be used
  set myarg = `echo $1 | /usr/bin/tr "[A-Z]" "[a-z]"`
  switch ($myarg)
    case -h*:
      help $0 | more
      echo " USAGE:"
      echo "$usage"
      exit $status
      breaksw   
    case -n?*:
    case -p*:
      set key = OBTD
      breaksw
    case -k*:
      set echokwd = 1
      breaksw
    case -t*:
      if ($2 =~ [0-9]*) then
        set timeout = $2
      endif
      shift
      breaksw
    case k*:
      set namedpos = kBand
      breaksw
    case h:
    case h2*:
    case hb*:
      set namedpos = hBand
      breaksw
    case home:
      set namedpos = home
      breaksw
    case o*:
      set namedpos = open
      breaksw
    case ha*:
      set namedpos = hAnnular
      breaksw
    default:
      breaksw
  endsw
  shift
end
  
if ( $echokwd == 1 ) then
  echo $service.$key
  exit $status
endif

if ($?namedpos) then
  # ensure we modify the correct keyword
  set key = $defaultkey
  modify -s ao $key=$namedpos
  # let stage start moving, then wait for move to complete
  sleep 2
  echo "Waiting for $stage.  Timeout set to $timeout seconds"
  set stat = `show -s $service -terse obtdstst`
  if ($stat == "FAULT") then 
    echo "$stage is faulted"
    exit 1
  endif
  while ($stat != "INPOS" && $count < $timeout)
    if ($stat == "FAULT") then 
      echo "$stage is faulted"
      exit 1
    endif
    @ count++
    echo -n .
    sleep 1
    set stat = `show -s $service -terse obtdstst`
  end
  # clean up the echo -n
  echo ""
  if ( $count >= $timeout ) then
    set stat = `show -s $service -terse obtdstst`
    echo "Timeout waiting for $stage.  Status is $stat"
    exit 1
  endif
else
  # show position
  show -s $service -terse $key
endif



exit
