#!/bin/csh -f
#+
# OSIRIS library of scripts
#
# NAME
#      om2s  - read or set expected switch values
#
# SYNOPSIS
#     om2s [x] [-f(ile)|-k(eyword)]
#
# DESCRIPTION
#      prints the position number, name, and switch values of the mechanism
#
# ARGUMENTS
#     x  - good|bad
#     -f - use contents of file 
#          /kroot/rel/default/data/<service>_keyword_config_file
#     -k - use keywords (default)
#
# OPTIONS
#
# EXAMPLES
#    om2s good  - set om2s switch values to good values
#    om2s bad   - set om2s switch values to bad values
#
# ENVIRONMENT VARIABLES
#
# FILES
#
# SERVERS & KEYWORDS
#    
#
#
# SCRIPTS CALLED
#
# EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#
#
# Modification History:
# 2016 Mar 18 jlyke Original
# 2019 Apr 29 jlyke Allow both file and keyword info...
#------------------------------------------------------


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

if ("$1" == "-h") then
    help $0
    exit $status
endif

# Formulate lockengpw
set lockengpw = `date '+%y%m%d'`
set line = "=========================================="

# server specific stuff
set serv = om2s
set homeoff = 0
set no_of_pos = 24
# define num
set i = 0
set num = ()
while ($i < $no_of_pos)
  set num = ($num $i)
  @ i++
end 
set ref = keyword
set digits = 9
### Note that we are not using the good/bad for this motor
#set good = ()
# This set of "bad" reduces all switch positions with logical 8
# 11XXX1111 are the normal switch values (XXX means no switch)
# 11XXXB111 has logical 8 as "bad"
#set bad = ()


if ( $#argv > 0 ) then
  switch ($1)
#    case g*:
#      set val = ( $good )
#      breaksw
#    case b*:
#      set val = ( $bad )
#      breaksw
    case h*:
    case -h*:
      help $0
      exit $status
      breaksw
    case f*:
    case -f*:
      set ref = file
      breaksw
    default: 
      breaksw
   endsw
endif

if ($?val) then
  set lock = `show -s $serv -terse lockeng`
  if ( $lock == 1 ) then
    modify -s $serv lockeng=$lockengpw
  endif
  foreach n ($num)
    set i = `math "$n + 1"`
    modify -s $serv switch$n=$val[$i]
  end
  if ( $lock == 1 ) then
    modify -s $serv lockeng=$lockengpw
  endif
endif

if ($ref == "file" ) then
  # init variables
  set homeoff = 0
  set posname = ()
  set switch = ()
  set location = ()
  set config = /kroot/rel/default/data/${serv}_keyword_config_file
  foreach key (posname switch location)
    set $key = (`grep ^"$key"\[0-9] $config | gawk '/val=/ {f=NR} f&&NR-1==f' RS=\" | sed -e 's/ /_/g'`)
  end
  # find offset to home position in motor counts
  set homepos = `grep homepos $config | gawk '/val=/ {f=NR} f&&NR-1==f' RS=\" | sed -e 's/ /_/g'`
  set homeoff = `grep location$homepos $config | gawk '/val=/ {f=NR} f&&NR-1==f' RS=\" | sed -e 's/ /_/g'`
  #echo $homepos $homeoff
  set i = 0
  set num = ()
  while ($i < $#switch)
    set num = ($num $i)
    @ i++
  end
endif

echo $line
printf "%7s %5s %9s %7s %10s\n" "Filter" "Pos" "Location" "Switch" "Binary"
echo $line
foreach n ($num)
  set i = `math "$n + 1"`
  if ($ref == "file" ) then
    set output = ( $posname[$i] $location[$i] $switch[$i])
  else
    set output = (`show -s $serv -terse posname$n location$n switch$n`)
  endif
  set homesteps = `math $output[2] + $homeoff`
  set binary = `dec2bin -d $digits $output[3]`
  set printout = ( $output[1] $n $homesteps $output[3] $binary)
  printf "%7s %5s %9s %7s %10s\n" $printout
end
echo $line


