#!/bin/bash
function usage {
echo "
  This script performs PIOP modulator control. 
 
  -b <CAMAC-branch-name> eg -b LI28
  -t <Klys-device-type>. Must be KLYS or SBST
  -u <Klys-device-unit>. Klystron device unit number
  -f <Modulator control function requested>
     The parameter of -f may be:
            on    for Modulator ON request
          | reset for Modulator RESET request,
          | off   for Modulator OFF request  
  -h prints this help.


  Example 1: To turn on the modulator attached
  to klystron 18-6. modulatorControl will find the relevant crate and 
  module itself (using PLOC):

     modulatorControl -b li18 -t klys -u 61 -f on
 
  Example 2: As above, but for the subbooster in LI18:
 
     modulatorControl -b li18 -t sbst -u1 -f on

  Example 3: Reset a klys modulator:

     modulatorControl -b li18 -t klys -u 21 -f reset


  Implementation details:

  Depending on the station configuration, the script will send commands
  either via camcom or via simple EPICS PVs.

  For each station, the PV KLYS:<loca>:<unit>1:MOD:CTRL indicates 
  which to use. If its value is 0, use camcom. If its value is 1, use
  EPICS PVs.

  The specific CAMAC data values for each of the 3 modulator 
  control commands, are ON=0x4045, RESET=0x4046, OFF=0x04047.

  The modulators camac calls must be made three times, first with the 
  data bit 0, then 1, then 0, with a sleep between each. Note
  that unlike in the SCP, there is no support in this
  script for what used to be called ALL*. You have to give
  a fully qualified branch, crate and module in each call. Also
  there is no rights check made within this file - it'll just
  do it! So user beware.

"
}
#!---------------------------------------------------------
#! Auth: Greg White, 08-Sep-2010
#! Rev: 
#! 
#! Mod: 4-May-2011, Greg White (greg@slac.stanford.edu)
#! Bug fix: Changed the way bitwise comparisons are done from comparing to 
#! int (-eq), wrong, to specific equality with the mask, right.
#! Fixed condition handling of caget and ploc.
#! ========================================================

# Constants
HSTA_MKSU=$((0x0400))

# Process options
br=
type=
unit=
cr=
mo=
data=
while getopts b:t:u:f:h opt
do
   case "$opt" in
      h) usage; exit 0;;
      b) br=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      t) type=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      u) unit=$OPTARG;;
      f) f=$OPTARG; case "$f" in
            on)  data=`echo $((0x4045))`;;
            reset) data=`echo $((0x4046))`;;
            off)   data=`echo $((0x4047))`;;
            *)     echo "Modulator function not recognized. Must be on, off, or reset"; exit 1;;
            esac ;;
      *) echo "Unknown argument, see modulatorControl -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

# Check for insufficient arguments.
if [ -z "$type" ] || [ -z "$br" ] || [ -z "$unit" ] || [ -z "$data"  ]; then
   echo "Too few arguments, see modulatorControl -h for help."
   exit 1;
fi

echo "CAMCOM-controlled station"

# Find the crate and module of the device given
crateandmod="`$LCLS_ROOT/tools/script/ploc -q $type $br $unit`"
stat=$?
if [ $stat != 0 ]; then
   echo "Error getting Crate and Module information from PLOC, exiting"
   exit $stat
fi 
cr=`echo $crateandmod | cut -d" " -f 1`
mo=`echo $crateandmod | cut -d" " -f 2`
  
echo br = "$br", cr = "$cr", mo = "$mo", command = "$f" 
#! (camacdata = "$data")

# Check whether HSTA_MKSU is on. Only proceed if it is.
#
hsta=`caget -t0x $type:$br:$unit:HSTA`
stat=$?
if [ $stat != 0 ]; then
    echo "Unable to acquire KLYS HSTA, exiting modulatorControl"
    exit $stat
fi

if [ $(($HSTA_MKSU & $hsta)) != $(($HSTA_MKSU)) ]; then
  echo HSTA MKSU bit `printf "0x%x" $HSTA_MKSU` is not on for $type:$br:$unit. HSTA = `printf "0x%x" $hsta`.
  exit 1;
fi
 
#! Ok, now do the CAMCOM, which amounts to executing the same
#! camcom commads varying the last arg through 0,1,0. Wait 1s between
#! camcoms (in the SCP this was 500 ms but bash doesn't give that
#! easily).
#!
caCamcom $LCLS_ROOT/tools/script/modulator_onoff.cam $br $cr $mo $data 0 
sleep 1
caCamcom $LCLS_ROOT/tools/script/modulator_onoff.cam $br $cr $mo $data 1
sleep 1
caCamcom $LCLS_ROOT/tools/script/modulator_onoff.cam $br $cr $mo $data 0 

if [ $? == 0 ]; then
   echo "Modulator update $br $cr $mo $f, completed successfully"
else
   echo "Modulator update $br $cr $mo $f, completed *with error(s)*"
fi
 
#!exit 0;


