#!/usr/bin/env python

# generate_edl_file.py
#
#   python script to generate the evnt edl file for the specified AREA and SUBSYSTEM, eg. evnt_in20_im01.
#   This script uses msi substitution to generate the edl file.
#   Script create_edm_event_msi.py can be used to create the msi substitution file.
#   An example msi substitution file uses header, body, and footer templates and substitution data that is
#   specific to the Area and Subsystem.
# USAGE:
# ./generate_edl_file.py IOC Subsystem
# or, to display correct usage, type:
#   ./generate_edl_file.py -h
#---------------------------------------------------------------------------------------------------------
# Author:  E. Grunhaus
# Modification History
#       14-Mar-2012. Murali: Use EDM/templates instead of EDMSETUP
#       03-Mar-2012. Murali: Use os.environ for EDMSETUP
#       16-Feb-2012, Lahey: Add file header
#        1-Feb-2012, Lahey: Small change to parsing of arguments to support 3 letter subsystem names
#
#========================================================================================================


import sys
import os
import subprocess

vnumargs = len(sys.argv)

def ShowUsage():
  print
  print "********************"
  print "USAGE:"
  print "./generate_edl_file.py Ioc Subsystem"
  print "Example: ./generate_edl_file.py in20 bpm1"
  print "To display this usage info, type: ./generate_edl_file.py -h"
  print "********************"
  print
  return


if ( (vnumargs < 2) or (vnumargs > 3) ):
  ShowUsage()
  raise SystemExit
elif (vnumargs == 2):
  varghelp = sys.argv[1].strip()
  varghelp = varghelp.lower()
  if (varghelp == "-h"):
    ShowUsage()
  else:
    print "\"Show Usage\" argument should be \"-h\""
    print
  raise SystemExit
else:
  varghelp = sys.argv[1].strip()
  varghelp = varghelp.lower()
  if (varghelp == "-h"):
    ShowUsage()
    raise SystemExit
  else:
    vIoc       = sys.argv[1].strip()
    vSubsystem = sys.argv[2].strip()

    vIoc       = vIoc.lower()
    vSubsystem = vSubsystem.lower()
    
    print "IOC="       + vIoc
    print "Subsystem=" + vSubsystem

    templateLocation = os.environ['EDM'] + '/templates'
    print "EDM templates from " + templateLocation
    subprocess.call(['msi', '-oevnt_' + vIoc + '_' + vSubsystem + '.edl', '-Sevnt_' + vIoc + '_' + vSubsystem + '_msi.substitution', '-I' + templateLocation], shell=False)
