#!/bin/bash

############################################################
# NAME: SXRSSstp                                           #
# Author: Tonee Smith                                      #
# Created: 11/11/2014                                      #
# Modified:                                                #
#                                                          #
# Description: Shamelessly lifted from Shawn Alverson's    #
#              quickSTP, this one generates a STP params   #
#              file based on current SXRSS guardian limits #
#                                                          #
############################################################

#Initialize variables
BG="65535     65535     65535"
FG="0     0     0"
SampleMax=7200
nolim=0
MAX=""
MIN=""

#Might want some of this later...
#
# Check Options
#if [ $# -ne 0 ];then
#    while getopts "xhslM:m:" Option
#    do
#      case $Option in
#         x  )  BG="0     0     0"
#               FG="65535     65535     65535";;
#	 s  )  SampleMax=65535;;
#	 l  )  nolim=1;; #No limits.  Suitable for SLC devices without HOPR/LOPR values
#	 M  )  MAX=$OPTARG;;
#	 m  )  MIN=$OPTARG;;
#	 :  )  echo "Option -$OPTARG requires an argument! See help (-h) for details."
#	       exit 1;;
#         *  )  echo -e "\nSyntax: makestp [OPTION] [PVs]"
#               echo -e "Options: -x  Black background"
#               echo -e "         -s  Max sample points (def. 7200)"
#	       echo -e "         -M [Range Max] Max value for y axis (cannot be used with -l)"
#	       echo -e "         -m [Range Min] Min value for y axis (cannot be used with -l)"
#               echo -e "         -h  Help\n"
#               exit 0;;
#      esac
#    done
#    shift $(($OPTIND-1))
#else
#    echo "No PVs supplied! Quiting..."
#    exit 1
#fi

#Create temp file
TMPSTP=$(mktemp /tmp/sxrssMonSTP.XXXXXXXX) || exit 1
#echo $TMPSTP

#Create xml config
echo 'StripConfig                     1.2' >> $TMPSTP
echo 'Strip.Time.Timespan             300' >> $TMPSTP
echo "Strip.Time.NumSamples           $SampleMax" >> $TMPSTP
echo 'Strip.Time.SampleInterval       0.500000' >> $TMPSTP
echo 'Strip.Time.RefreshInterval      0.500000' >> $TMPSTP
echo 'Strip.Color.Grid                49071     49071     49071' >> $TMPSTP
echo "Strip.Color.Background          $BG" >> $TMPSTP
echo "Strip.Color.Foreground          $FG" >> $TMPSTP
#echo "Strip.Color.Color0              40000     40000     0" >> $TMPSTP
echo "Strip.Color.Color1              27499     36494     8995" >> $TMPSTP
echo "Strip.Color.Color2              42405     10794     10794" >> $TMPSTP
echo "Strip.Color.Color3              24415     40606     41120" >> $TMPSTP
echo "Strip.Color.Color4              65535     42405     0" >> $TMPSTP
echo "Strip.Color.Color5              41120     8224      61680" >> $TMPSTP
echo "Strip.Color.Color6              65535     0         0" >> $TMPSTP
echo "Strip.Color.Color7              65535     55255     0" >> $TMPSTP
echo "Strip.Color.Color8              48316     36751     36751" >> $TMPSTP
echo "Strip.Color.Color9              39578     52685     12850" >> $TMPSTP
echo "Strip.Color.Color10             0         0         65535" >> $TMPSTP
echo "Strip.Option.GridXon            1" >> $TMPSTP
echo "Strip.Option.GridYon            1" >> $TMPSTP
echo "Strip.Option.AxisYcolorStat     1" >> $TMPSTP
echo "Strip.Option.GraphLineWidth     2" >> $TMPSTP

#Populate PVs to monitor
#j=1 
#for i in "$@"; do
#    if [ $j -le 10 ];then
#	#echo "Adding PV: $i"
#	PV[$j]=$i
#	echo "Strip.Curve.$(($j-1)).Name              ${PV[$j]}" >> $TMPSTP
#	let "j += 1"
#    fi
#done

#Hard-code PVs for now
#
echo "Strip.Curve.0.Name            FBCK:FB02:GN01:S1P1" >> $TMPSTP
echo "Strip.Curve.1.Name            FBCK:BCI0:1:CHRG_S" >> $TMPSTP
echo "Strip.Curve.2.Name            FBCK:FB04:LG01:S3P1" >> $TMPSTP
echo "Strip.Curve.3.Name            ACCL:LI21:1:L1S_PDES" >> $TMPSTP
echo "Strip.Curve.4.Name            FBCK:FB04:LG01:S5P1" >> $TMPSTP
echo "Strip.Curve.5.Name            FBCK:FB04:LG01:CHIRPDES" >> $TMPSTP
echo "Strip.Curve.6.Name            LASR:IN20:475:PWR" >> $TMPSTP
echo "Strip.Curve.7.Name            COLL:LI21:236:LVPOS" >> $TMPSTP
echo "Strip.Curve.8.Name            COLL:LI21:235:LVPOS" >> $TMPSTP
echo "Strip.Curve.9.Name            FOIL:LI24:804:LVPOS" >> $TMPSTP
echo "Strip.Curve.0.Units           nC" >> $TMPSTP
echo "Strip.Curve.1.Units           nC" >> $TMPSTP
echo "Strip.Curve.2.Units           mrad" >> $TMPSTP
echo "Strip.Curve.3.Units           mrad" >> $TMPSTP
echo "Strip.Curve.4.Units           MeV" >> $TMPSTP
echo "Strip.Curve.5.Units           uJ" >> $TMPSTP
echo "Strip.Curve.6.Units           mm" >> $TMPSTP
echo "Strip.Curve.7.Units           mm" >> $TMPSTP
echo "Strip.Curve.8.Units           microns" >> $TMPSTP
echo "Strip.Curve.9.Units           " >> $TMPSTP
echo "Strip.Curve.0.Comment         Calc'd RB of bunch chg" >> $TMPSTP
echo "Strip.Curve.1.Comment         matlab bunch charge" >> $TMPSTP
echo "Strip.Curve.2.Comment         Calc'd RB of BC1fbck" >> $TMPSTP
echo "Strip.Curve.3.Comment         L1S Master Set Value" >> $TMPSTP
echo "Strip.Curve.4.Comment         Calc rdbk BC2 feedback" >> $TMPSTP
echo "Strip.Curve.5.Comment         Desired chirp value " >> $TMPSTP
echo "Strip.Curve.6.Comment         Laser heater power meter BSA" >> $TMPSTP
echo "Strip.Curve.7.Comment         CE11x2 Position" >> $TMPSTP
echo "Strip.Curve.8.Comment         CE11x1 position" >> $TMPSTP
echo "Strip.Curve.9.Comment         Slotted Foil Position" >> $TMPSTP
echo "Strip.Curve.0.Precision       4" >> $TMPSTP
echo "Strip.Curve.1.Precision       4" >> $TMPSTP
echo "Strip.Curve.2.Precision       4" >> $TMPSTP
echo "Strip.Curve.3.Precision       4" >> $TMPSTP
echo "Strip.Curve.4.Precision       1" >> $TMPSTP
echo "Strip.Curve.5.Precision       1" >> $TMPSTP
echo "Strip.Curve.6.Precision       4" >> $TMPSTP
echo "Strip.Curve.7.Precision       4" >> $TMPSTP
echo "Strip.Curve.8.Precision       4" >> $TMPSTP
echo "Strip.Curve.9.Precision       1" >> $TMPSTP


# Set up y-axis limits
#if [ $nolim -eq 0 ];then
#    for ((i=1; i<$j; i++));do
#	if [ "$MAX" != "" ];then
#	    echo "Strip.Curve.$(($i-1)).Max               $MAX" >> $TMPSTP
#	else
#	    echo "Strip.Curve.$(($i-1)).Max               `caget -t ${PV[$i]}.HOPR`" >> $TMPSTP
#	fi
#	if [ "$MIN" != "" ];then
#	    echo "Strip.Curve.$(($i-1)).Min               $MIN" >> $TMPSTP
#	else
#	    echo "Strip.Curve.$(($i-1)).Min               `caget -t ${PV[$i]}.LOPR`" >> $TMPSTP
#	fi
#    done
#fi

for ((i=1;i<11; i++));do
     echo "Strip.Curve.$(($i-1)).Min               `caget -t SIOC:SYS0:ML02:AO$((204+$i))`" >> $TMPSTP
done
for ((i=1;i<11; i++));do
     echo "Strip.Curve.$(($i-1)).Max               `caget -t SIOC:SYS0:ML02:AO$((214+$i))`" >> $TMPSTP
done

#more $TMPSTP &

# Launch StripTool app and load temporary xml file
StripTool $TMPSTP || exit 2

exit 0
