#!/usr/bin/env bash
#
# Purpose: Automatically generate the timing trigger screens based on macros already contained in 
#   the myApp/Db/<area>/*trig*.substitions file(s) 
#
# This bash script is called from the Makefile in myApp/Db/<area> wherever *trig*.substitutions 
#    file(s) live
# Author: DROGIND
# Date  : 3/14/2012
#
# Instructions: 
# Steps to autogenerate your evnt trigger screen:
# 1) Add the following comment tags to your *trig*.substitutions file:
#    
#    file evrDevTrig.db 
#    {
#    #EVR EVR:XT01:IM01
#    #FILE evnt_xt01_im01  ie, output edl file name in form evnt_<area>_<unit>
#    #CONTROLPV1 SIOC:SYS6:AL00:MODE
#    #CONTROLPV2 SIOC:SYS6:AL00:TOD 
#
#  where #EVR is your EVR root name, #FILE is the output evnt*.edl filename,
#  and CONTROLPV* indicate the PV names for mode and time of day at screen bottom,
#  indicating PRODUCTION or DEVELOPMENT, and Time of Day from approproate IOC. 
#  Note that the "SYS*" area will change depending on the machine.
#  Note that if CONTROLPV* tags are omitted, the PVs default to "SYS0" for LCLS
#
# 2) Add to your Db/..Makefile:
#    gen_trig_edl:
#	    gen_trig_edl.sh arg1 arg2 arg3
#
#    where arg1 = trigger substitution filename
#          arg2 = edl file area
#          arg3 = edl file unit
#    Example: gen_trig_edl.sh IOC-XT01-IM01trig.substituions xt01 im01
#
#    Note: If you have multiple trigger substitution files due to multiple EVRs,
#          first concatenate the trigger files into one file to generate the proper 
#          navigation buttons in the footer 
#
# -------------------------------------------------------------
# Note:
# $EDM/templates contain the edm template files used
# $TOOLS/script contain the .py scripts used (thanks to Murali and Elie)
# $TOOLS/script contains gen_trig_edl.sh
# $EDM/install is the area to which generated screens are copied
# 
#
# -------------------------------------------------------------
#source /afs/slac/g/acctest/tools/script/ENVS_acctest.bash

expected_args=3

if [ $# -ne $expected_args ]
then
  echo "Missing arguments - check Makefile"
  echo "Usage: gen_trig_edl.sh *trig.substitutions area unit"
  echo "Example - in Makefile -"
  echo "   gen_trig_edl:"
  echo "         gen_trig_edl.sh ioc-xt01-im01trig.substitions xt01 im01"
  exit
fi

create_edm_event_msi.py $1
generate_evnt_edl_file.py $2 $3

edlfile="evnt_${2}_${3}.edl"
echo -e "generated file to be installed into \${EDM}/install = $edlfile"
msifile="evnt_${2}_${3}_msi.substitution"
#echo -e temp msi file =$msifile

# clean up intermediate edl:
rm $msifile
# cp generated edl file to $EDM/install
cp $edlfile $EDM/install
rm $edlfile


exit;
# See example in /afs/slac/g/acctest/epics/iocTop/R3-14-12/BeamCharge/MAIN_TRUNK/BeamChargeApp/Db/xta
#  and inspect IOC-XT01-IM01trig.substitutions, Makefile, and $TOOLS/script/gen_trig_edl.sh
#
# Note that the following example output from this script is normal:
#
#  gen_trig_edl.sh IOC-XT01-IM01trig.substitutions xt01 im01
#  channel count =  1
#  System as determined from CONTROLPV1  SYS6
#  Done generating  evnt_xt01_im01_msi.substitution
#  IOC=xt01
#  Subsystem=im01
#  EDM templates from /afs/slac/g/acctest/tools/edm/display/templates
#  Warning: unexpanded macros in ouput
#  generated file to be installed into ${EDM}/install = evnt_xt01_im01.edl
#
#
# For any other errors, please consult with Murali or Debbie
