#!/bin/bash -f
function usage {
echo "
   zort sorts an input list of PV names by their nominal Z position
   in the LCLS machine. 

   Usage: zort [-h] [-b beamline] PV-names   

   Options:

   -h (optional) Prints help

   -b (optional) Sets the modelling beamline from which zort determines the
                 beamline areas (IN20, LI21 etc) to include, and their order.
                 That is, the PV-names file given to zort need not include 
                 only modelled devices, since zort only uses the beamline to 
                 find a formal specification of beamline areas and their order.                  zort applies the set of areas and their order, to the input 
                 list of PV names. zort will use the latest Design model of 
                 that beamline. If not given, zort uses the FullMachine 
                 beamline. 

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

   The PV-names argument is an unformatted (ie text) file containing PV names.

   Output: 
    
    
   Examples:
             1) zort a list of PV names generated by aidalist, directly 
                from stdin:

                aidalist VCGG:%:P | zort 

             2) zort a list of PV names generated by aidalist, using the 
                CathodeTo52SL2 beamline as the source for which areas are 
                to be included: 
               
                aidalist VGCC:%:P | zort -b 52

             3) zort a list of PV given names in a file:

                aidalist VCGG:%:P > vacuum_guages.txt
                zort vacuum_guages.txt

             4) zort a list of PV names, where the PV names are not all of 
                the same kind. In this case using a HERE file to give
                input: 

                zort <<EOF
                > BPMS:UND1:1990:YRMSHST1
                > XCOR:LI22:554:BDES
                > WIRE:LTU1:53:X
                > EOF
"
}

AWK_DIR=/usr/local/lcls/tools/script
# Process options
beamline=FullMachine
while getopts hb: opt
do
   case "$opt" in
       h) usage; exit 0 ;;
       b) case "$OPTARG" in 
               FullMachine|full) beamline=FullMachine;;
	       CathodeTo135MeVSpect|135) beamline=CathodeTo135MeVSpect;;
	       CathodeTo52SL2|52) beamline=CathodeTo52SL2;;
	       CathodeToGunSpect|gun) beamline=CathodeToGunSpect;;
               *) echo "Beamline option b - unknown beamline given: '$OPTARG'."
                  echo "Check case and spelling"; exit 1;;
	   esac ;;
       *) echo "Unknown Arguments, see zort -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

# If a file-name has been given on the command line, redirect stdin from there.
if [ $# -ne 0 ]; then
   exec < ${1}
fi

# run zort.awk against the input, passing on the given beamline.
awk -v beamline=${beamline} -f ${AWK_DIR}/zort.awk

exit 0;
 
