#!/bin/bash
function usage {
echo "
  This script provides control for a sector of klystron modulators. 
 
  -b Sector name eg -b LI28 (must be between LI20 and LI30)
  -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  
          | offreseton   for Modulator OFF&RESET&ON request  
  -h prints this help.


  Example 1: To turn on the klystron modulators in LI28
  modulatorControl will find the relevant crates and 
  modules themselves (using PLOC):

     modulatorControl -b li28 -f on
 
  Example 2: Reset the modulators modulator:

     modulatorControl -b li28 -f reset

  Most sectors have 8 klystrons (1-8). Some sectors have a subset of these.
  These details have been hard-coded in the script so that the unsual 
  sectors are handled correctly as follows:
      LI20 has klystrons 5-8
      LI24 has klystrons 1-6,8
"
}
#!---------------------------------------------------------
#! Auth:
#! Rev: 
#! 
#! Mod: 
#! ========================================================

# Process options
br=
cmd=
while getopts b:f:h opt
do
   case "$opt" in
      h) usage; exit 0;;
      b) br=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      f) f=$OPTARG; case "$f" in
            on)  cmd="$f";;
            reset) cmd="$f";;
            off)   cmd="$f";;
            offreseton)   cmd="$f";;
            *)     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 "$br" ] || [ -z "$cmd"  ]; then
   echo "Too few arguments, see modulatorControl -h for help."
   exit 1;
fi

# Define array of modulator units in this sector.
# Sector must be between LI20 and LI30. LI20 and LI24 are special cases. 
if [ "LI21" == "$br" ] || [ "LI22" == "$br" ] || [ "LI23" == "$br" ] || \
   [ "LI25" == "$br" ] || [ "LI26" == "$br" ] || [ "LI27" == "$br" ] || \
   [ "LI28" == "$br" ] || [ "LI29" == "$br" ] || [ "LI30" == "$br" ]; then
   units=(11 21 31 41 51 61 71 81);
elif
   [ "LI20" == "$br" ]; then
   units=(51 61 71 81);
elif
   [ "LI24" == "$br" ]; then
   units=(11 21 31 41 51 61 81);
else
   echo "Sector must be between LI20 and LI30."
   exit 1;
fi

# Loop through modulators, performing requested function
if [ "offreseton" == "$cmd" ]; then
    for i in "${units[@]}"
      do
       caput KLYS:$br:$i:MOD:HVON_SET 0
       sleep .2
    done
    for i in "${units[@]}"
      do
      caput KLYS:$br:$i:MOD:RESET 1
      sleep .2
    done
    for i in "${units[@]}"
      do
      caput KLYS:$br:$i:MOD:HVON_SET 1
      sleep .2
     done
else
    for i in "${units[@]}"
      do
      case "$f" in
	"on")    caput KLYS:$br:$i:MOD:HVON_SET 1;;  
        "reset") caput KLYS:$br:$i:MOD:RESET 1;;
        "off")   caput KLYS:$br:$i:MOD:HVON_SET 0;;
        *) echo "Modulator function not recognized. Must be on, off, or reset"; exit 1;;
      esac
    done
fi

exit 0


