#!/bin/csh -f
#    sort and awk must be gnusort and gnuawk
#   if the script said you should set an alias, see
#   the lines down to "-----------" below
if( $#argv != 1 && $#argv != 3) then
 echo   "Usage: fordpmjet  configfile"
 echo           "or"
 echo   "fordpmjet  configfile GLBfile INPfile"
 echo   "If GLB and INP files are not specified, dpmjet.GLB and dpmjet.inp"
 echo   "are assumed"
 exit 1
endif
set awkex = `which awk ` 
if( x$awkex  == "x" )  then
	echo "set awk as an alias of gnu awk in Scrpt/fordpmjet"
	exit 1
endif
set sortex = `which sort`
if( x$sortex == "x" ) then
	echo "set sort as an alias of gnu sort in Scrpt/fordpmjet"
	exit 1
endif

# --------------------------
set incex = `awk '$1 ~ "#inc" {print "inc"; exit}' $1`
if( x$incex == "x" ) then
    echo "$1  is a simple config file"
    if( $#argv == 3 ) then
	(source $EPICSTOP/Scrpt/fordpmjetGLB $1) > $2
	echo $2 "has been created"
        ($EPICSTOP/Scrpt/fordpmjetINP $1) > $3
	echo $3 "has been created"
    else
	(source $EPICSTOP/Scrpt/fordpmjetGLB $1) > dpmjet.GLB
	echo "dpmjet.GLB has been created"
	(source $EPICSTOP/Scrpt/fordpmjetINP $1) > dpmjet.inp
	echo "dpmjet.inp has been created"
    endif
else
#  include file exists so use testConfig in Util
    echo "$1  has #inc so we need a bit of time"
    set cnfgpath = "`pwd`/$1"
    (cd $EPICSTOP/Util; rm -f temp.cnfg;  rm -f a.out;  make -f testCnf1.mk;  echo "$cnfgpath" |  ./a.out > temp.cnfg)
#          next line is needed to skip to that place in fordpmGLB /fordpmINP
    echo "-----------------------------------" > temp$$.cnfg
      # $4:  SCIN etc.  $2 level    
    awk '$1~/[0-9]+/ && $4~/[a-z][A-Z]?*/ {$2=""; print}' $EPICSTOP/Util/temp.cnfg >> temp$$.cnfg
####
	cp temp$$.cnfg /tmp/kasahara/temp.cnfg
#####
    if( $#argv == 3 ) then
	(source $EPICSTOP/Scrpt/fordpmjetGLB temp$$.cnfg) > $2
	echo $2 "has been created"
        ($EPICSTOP/Scrpt/fordpmjetINP temp$$.cnfg) > $3
	echo $3 "has been created"
    else
	(source $EPICSTOP/Scrpt/fordpmjetGLB temp$$.cnfg) > dpmjet.GLB
	echo "dpmjet.GLB has been created"
	(source $EPICSTOP/Scrpt/fordpmjetINP temp$$.cnfg) > dpmjet.inp
	echo "dpmjet.inp has been created"
    endif
    rm -f temp$$.cnfg
    rm -f $EPICSTOP/Util/temp.cnfg
endif
    

    








