#! /bin/bash
#
# check if user has template specified
PGM=ck_tmplate
USAGE="usage: $PGM <runid> <tok> <option> "

#@! Check arguments
#@! ---------------
if [ $# -lt 3 ] ; then
  echo $USAGE 1>&2
  exit 1
fi

runid=$1
tok=$2
opt=$3

#
# Check if a component is being used
#   looks for NL${_comp}_${_type}=TRUE
#     if not set outputs ""
#     if set and no template output ""
#     else output {_component}_${_type}_TEMPLATE value
#
#  _type      = ECH or ICH or LHH
#  _comp      = GEN or CQL3D
#  _component = GENRAY or CQL3D
#
ck_tmpl(){
   _runid="$1"
   _type="$2"
   _comp="$3"
   _component="$4"
   _nml=${_runid}TR.DAT
   _NLGEN=`grep -i "^ *NL${_comp}_${_type}" $_nml |  egrep -i '= *t|= *\.t' 2> /dev/null`
   if [ -n "$_NLGEN" ]; then
      # check if user provides template
      _U_T=`grep -i "^ *${_component}_${_type}_TEMPLATE" $_nml  |sed -e "s/.*= *'//" -e "s/'.*//" `
      if [ -n "$_U_T" ]; then
         echo "$_U_T"
      else
         echo "DEFAULT"
      fi
   else
      echo ""
   fi
  return 0
}

if [ ! -f ${runid}TR.DAT ]; then
   echo "?ck_tmplate -E- ${runid}TR.DAT not found"
   exit 1
fi
status=0
#check If genray is used
   LIST="ECH ICH LHH"
   for rf in $LIST; do
      file=`ck_tmpl $runid $rf GEN GENRAY`
      if [ -n "$file" ]; then
         # If DEFAULT: template is added on server by trp_pre 
         if [ "$file" = "DEFAULT" ]; then
            echo GENRAY_${rf}: using DEFAULT
            if [ "$opt" = "pre" ]; then
               echo GENRAY_${rf}: trigger tr_pretr.pl
               echo "$CODESYSDIR/source/nml/${tok}_GENRAY_${rf}_STANDARD.IN" >${runid}_${rf}.RFTMP
               status=2
            fi 
         else
         # If customized: template is added by tr_start or create_remote 
            if [ "$opt" = "start" ]; then
               if [ -f $file ]; then
                  echo "GENRAY_${rf}: using $file"
                  echo "$file" > ${runid}_${rf}.RFTMP
                  status=2
               else
                  echo "?ck_tmplate -E- GENRAY_${rf}: $file does not exist"
                  exit 1
               fi
            fi
         fi
      fi

      file=`ck_tmpl $runid $rf CQL3D CQL3D`
      if [ -n "$file" ]; then
         # If DEFAULT: template is added on server by trp_pre 
         if [ "$file" = "DEFAULT" ]; then
            echo CQL3D_${rf}: using DEFAULT
            if [ "$opt" = "pre" ]; then
               echo CQL3D_${rf}: trigger tr_pretr.pl
               echo "$CODESYSDIR/source/nml/${tok}_CQL3D_${rf}_STANDARD.IN" >${runid}_${rf}.CQL3DTMP
               status=2
            fi 
         else
         # If customized: template is added by tr_start or create_remote 
            if [ "$opt" = "start" ]; then
               if [ -f $file ]; then
                  echo "CQL3D_${rf}: using $file"
                  echo "$file" > ${runid}_${rf}.CQL3DTMP
                  status=2
               else
                  echo "?ck_tmplate -E- CQL3D_${rf}: $file does not exist"
                  exit 1
               fi
            fi
         fi
      fi
   done
exit $status
