#!/bin/bash -f
function usage {
echo "
   valz prints a table of the PV names, S position, and extant PV 
   values, of the PVs matching the given device name and device 
   attribute patterns. 

   Usage: valz [-h] [-b {FullMachine|CathodeTo135MeVSpect|...
                         CathodeTo52SL2|CathodeToGunSpect}] ...
                    [-s start-time] [-e end-time] ... 
             PV-device-name-regex PV-attribute-name-regex
  
   Options:

   -h (optional) Prints help

   -b (optional) Sets the modelling beamline. If given, valz will use the
                 latest Design model of that beamline, to match the 
                 PVs given the two arguments PV-device-name-regex and 
                 PV-attribute-name-regex.

                 Presently 4 beamlines are recognized (case sensitive):
                    FullMachine or full - This is the default beamine.
	            CathodeTo135MeVSpect or 135
	            CathodeTo52SL2 or 52
	            CathodeToGunSpect or gun

   -s (optional) Archive data start time. 
                 -s Instructs to get the archived history values of the devices
                 matching PV-device and PV-attribute, rather than the 
                 present values. The output will then be 3 columns, not 2.
                 -s takes an argument, the starting time from which the 
                 archive data will be retrieved. If -s is given, a 
                 valid start time must follow. See below.
   -e (optional) Archive data end time. 
                 -e Instructs to get the archived history values of the devices
                 matching PV-device and PV-attribute, rather than the 
                 present values. The output will then be 3 columns, not 2.
                 -e takes an argument, the ending time from which the 
                 archive data will be retrieved. If -e is given, a 
                 valid end time must follow it. See below. 

   Valid times for -s and -e:

       Times must be in the syntax understood by the date command, ie one of
       syntaxes in the examples below (see man date):
 
      '10 minutes ago'
      '1 hour ago'
      '8 hours ago'
      '1 day ago'
      '1 week ago'
      '1 month ago'
      '1 year ago'
      'now'                    - System time 
      '01/13/2011 12:15:00'
      '13-Jan-2011 12:24:00'
      '4:30'                   - numerals are 24 hour clock
      '4:40pm'                 - 4:30 in the afernoon 


   Arguments:

      Both the device name arguments may be given as a regular
      expression. Specifically, the regular expressions must be given
      in the syntax understood by awk in posix mode (see man page for
      awk). Make sure to quote regular expressions, so they're not
      processed by the shell before being passed to valz.

   Output: 

      The table is sorted by the formal S position of the devices, as
      specified by the model of the beamline used. The FullMachine
      beamline is used by default, though the beamline can be
      specified by the -b option.  

   Examples:

      valz XCOR BDES      - all XCORs and their BDES
      valz QUAD EACT      - all QUADs and their last calced energy
      valz QUAD:LI24 IVB  - polynomial coefficients of all the LI24 QUADs
      valz 'QUAD:LI2[4-9]' EACT               - EACTs of QUADs in the 2nd 
                                                    half of the Linac. 
                                                    Note quotes pattern. 
      valz -b 52  BPMS X1                     - The X1 of all the 52 
                                                    line BPMS.
      valz -b 135 'QUAD:IN20:[300,500]' BDES  - BDESes of only the IN20  
                                                    QUADs who's unit number is 
                                                    300-500 and that are in the
                                                    CathodeTo135MeVSpect line.

      Examples getting archive history:

          valz -s '2 hours ago' XCOR BDES           The history of all XCORs
                                                    BDES since 2 hours ago.
          valz -s '3:30pm' -e '4:00pm' QUAD:LI24 BDES
                                                    History of some QUAD 
                                                    BDESes for
                                                    1/2 hr 3.30 to 4.00 pm.
          valz -b gun -s '12:00am' '.*' EACT 
                                                    History of all EACTs 
                                                    in CathodeToGun since
                                                    start of shift.

   Author: Greg White, 25-Jan-2011, SLAC. 
" 
}

if [ $# -eq 0 ]; then
   echo "No arguments, see valz -h for help"
   exit 1;
fi

AWK_DIR=/usr/local/lcls/tools/script
# Set Defaults
beamline=FullMachine
starttime='1 hour ago'
endtime='now'
gethist=0
printname=0

while getopts s:e:b:h opt; do
   case "$opt" in
       h) usage; exit 0 ;;
       b) 
	   # beamline takes an argument - the name of the beamline
	   case "$2" in 
               FullMachine|full) beamline=FullMachine;; 
	       CathodeTo135MeVSpect|135) beamline=CathodeTo135MeVSpect;; 
	       CathodeTo52SL2|52) beamline=CathodeTo52SL2;; 
	       CathodeToGunSpect|gun) beamline=CathodeToGunSpect;; 
               *) echo "Beamline option b - unknown beamline given: '$2'." \
                       "Check case and spelling"; 
                  exit 1;;
	   esac ;;
       s) starttime="${OPTARG}"; gethist=1;;
       e) endtime="${OPTARG}"; gethist=1;;
       *) echo "Unknown Arguments, see valz -h"; exit 1;;
   esac
done
shift $((OPTIND-1))
  
devPattern=`(echo $1|tr '[[:lower:]]' '[[:upper:]]')`
attrPattern=`(echo $2|tr '[[:lower:]]' '[[:upper:]]')`

if [ $gethist == 0 ]; then
    aidaget modelTwiss//Design.${beamline} | awk -v patt=$devPattern -vattr=$attrPattern -f $AWK_DIR/get_val.awk 
else
    aidaget modelTwiss//Design.${beamline} | awk -v patt=$devPattern -v attr=$attrPattern -v st="${starttime}" -v et="${endtime}" -f $AWK_DIR/get_hist.awk
fi

exit 0;

