#!/usr/bin/env bash
function usage {
echo "
  This script calls pydm for Klystron LLRF ATCA control.
 
  -f <Facility> facet,lcls,testfac,dev
  -l <Accelrator Area> li10
  -s <Klys station> 1,2,3,4,5,5,6,7
  -t Optional switch indicating RF Hut diagnostic signals
  -d Optional display (default=atcaLLRF.py)
  -h prints this help.

  Example 1: To start the facet-ii li10 station 2 displays
   klys_stn  -f facet -l li10 -s 2 -d facult_seq
"
}

# Process options
stn=
loca=
acc=
device=
ioc_unit=
type=
# Hardcode until displays replease to $PYDM/klys 
kdisp=atcaLLRF.py
disp=$PYDM/klys

while getopts l:s:f:t:d:h opt
do
   case "$opt" in
      h) usage; exit 0;; 
      l) loca=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      s) stn=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      t) type=`echo $OPTARG | tr '[:lower:]' '[:upper:]'`;;
      d) kdisp=$OPTARG;;
      f) acc=$OPTARG;
         case "$acc" in
           lcls)
            source /usr/local/$acc/epics/setup/epicsenv-7.0.3.1-1.0.bash
.           $TOOLS/script/use_pydm.sh;;
           facet)
            source /usr/local/$acc/epics/setup/epicsenv-7.0.3.1-1.0.bash
.           $TOOLS/script/use_pydm.sh;;
           acctf)
            source /afs/slac/g/$acc/epics/setup/epicsenv-7.0.3.1-1.0.bash
.           $TOOLS/script/use_pydm.sh;;
          dev)
            source /afs/slac/g/lcls/epics/setup/epicsenv-7.0.3.1-1.0.bash
.           $EPICS_SETUP/epicsenv-7.0.3.1-1.0.bash
.           $TOOLS/script/use_pydm.sh;;
           *) echo "Unknow facility"; exit 1;;
           esac ;;
      *) echo "Unknown argument, see  klys_llrf_stn -h"; exit 1;;
    esac
done
shift $((OPTIND-1))

pydm --version

if [ -z "$type" ]; then
   device=KLYS:${loca}:${stn}1
   ioc_unit=KY0${stn}
else
   echo "here we are"
   device=ACCL:${loca}:${stn}1
   ioc_unit=KY${stn}1
fi 

PYDM_STYLESHEET_INCLUDE_DEFAULT=1 pydm \
    -m "DEV=${device},IOC_UNIT=${ioc_unit},INST=0,LOCA=${loca}" \
    --hide-nav-bar \
    --stylesheet $TOOLS/pydm/stylesheet/default.qss \
    --stylesheet $disp/llrf.qss \
    $disp/$kdisp&

# End of script

