#!/bin/csh


# C Program EXEs
set HOM = "/n/park/0/cise/Repository/Developing/TCP/scripts"
set hpchall = "$HOM/hpchall"
set hpalterps = "$HOM/hpalterps"

# New C program EXEs
set hplogparam = "$HOM/hplogparam"
set hplog = "$HOM/hplog"
set hpcount = "$HOM/hpcountparam"

# New scripts
set plot = "$HOM/plot+"
set extract = "$HOM/extract+"

# files created in the current subdirectory 
set graphinfo = "./graphinfo"
set dotgrep = "./.grep"
set originalsnap = "./original"

# check the host type and set executable names accordingly
if ($HOSTTYPE == "hp700") then 
         set changepgm = $hpchall
         set alterpspgm = $hpalterps
         set logpgm = $hplog
         set logparampgm = $hplogparam
         set countpgm = $hpcount
endif

# parameter strings and files vectors
set numparams = 1
set param = (1)
set f = (1) 

# save the current working directory for the creation of the output dirs
set olddir = "$cwd"

# Use /tmp for all the tmp directories
cd /tmp

# find a unique name for the temporary directory
set ctr = 1
while (1)
  set direct = `ls | grep temp.$ctr`
  if ($#direct == 0) break
  @ ctr ++
end

# creating the temporary directory
set unique = "temp.$ctr"
mkdir $unique
cd $unique
echo Temporary directory under tmp is called $unique

#---------------------------------------------------------------------
#   Take care of command line arguments
#--------------------------------------------------------------------
 
 if ($#argv != 0) then
    echo usage : $0 "<"  extractfilename
    exit
 endif

#------------------------------------------------------------------
#   F I L E format
#-------------------------------------------------------------------
 ## sim executable name
 ## 1st parameter to be varied
 ## Ist parameter varyfile
 ## do the same stuff for the 2nd till the 25th parameters
# -----------------------
 ## Snap file name
 ## Number of parameters to log
 ## FOR EACH PARAMETER
 ##   Component name
 ##   Substring to identify this parameter in snap file
 ## Number of graphs to be generated
 ## FOR EACH GRAPH
 ##   Graph title
 ##   Name of required ps file (WITHOUT extension)
 ##   X axis label
 ##   Y axis label
 ##   l for lines or p for points
 ##   draft 1 or 0 - zero is not implemented yet
 ##   End time
 ##   Y Range
 ##   Number of curves on this graph
 ##   FOR EACH CURVE
 ##     Title for Curve on graph
 ##     Parameters (only used for non-draft)
 ##     Component name
 ##     Substring to identify parameter in snap file
 ##     How to plot this curve? n = normal, r = reverse, p = points
 ##     Compression? n = none, q = queue, o = other


###-----------
set osim = $<
set sim = $olddir/$osim

# read the parameter strings and file names into 2 arrays
@ ctr = 1
while ($ctr <= $numparams)
  set param[$ctr] = $<
  set f[$ctr] = $<
  @ ctr++
end

# number of simulations is initially 1
set numsim = 1

while (1)
  set snapfile = $<
  echo $snapfile
  if ("$snapfile" == "#") break 
  set snapnopath = $snapfile
  set snapfile = $olddir/$snapfile
  set snapwithpath = $snapfile
 
# clear all parameters to be logged
  set numlogs = $<
  $logpgm "6a" "2a" $snapwithpath $originalsnap

# log the appropriate parameters
  @ ctr = 1
  while ($ctr <= $numlogs)
    set componentname = $<
    set idstring = $<
    if ($ctr == 1) then
	$logparampgm "$componentname" "$idstring" "6a" $originalsnap ./param2
    else if (($ctr % 2) == 0) then
         $logparampgm "$componentname" "$idstring" "6a" ./param2 ./param1
    else
         $logparampgm "$componentname" "$idstring" "6a" ./param1  ./param2
    endif
    @ ctr ++
  end
			    
# final version will be in param2 if the number of parameters is odd else param1
  if (($numlogs % 2) == 0) then
       set snapwithlogs = "./param1"
  else
       set snapwithlogs = "./param2"  
  endif

# copy graph information to the graphinfo file
  set numgraphs = $<
  echo $numgraphs >! $graphinfo
  @ ctr = 0
  while ($ctr < $numgraphs)
    set title  = $<
    echo $title >> $graphinfo
    set filename = $<
    echo $filename >> $graphinfo
    set xlabel = $<
    echo $xlabel >> $graphinfo
    set ylabel = $<
    echo $ylabel >> $graphinfo
    set linesorpoints = $<
    echo $linesorpoints >> $graphinfo
    set draft = $<
    echo $draft >> $graphinfo
    set xmax = $<
    set yrange = $<
    echo $yrange >> $graphinfo
    set numcurves = $<
    echo $numcurves >> $graphinfo

    @ count = 0
    while ($count < $numcurves) 
      set curvetitle = $<
      echo $curvetitle >> $graphinfo
      set nondraftparams = $<
      echo $nondraftparams >> $graphinfo
      set componentname = $<
      set idstring = $<
      $countpgm "$componentname" "$idstring" $snapwithlogs $graphinfo
      set howtoplot = $<
      echo $howtoplot >> $graphinfo
      set howtoplot = $<
      echo $howtoplot >> $graphinfo    
      @ count++
    end
    @ ctr ++
  end

#-------------------------------------------------------------------------
#  The varying section begins here
#------------------------------------------------------------------------

  foreach arg1 (`cat $HOM/$f[1]`)

# place the argument values in a vector for easy access
    set argvector = ($arg1)

    \rm -f $dotgrep

# update snap file with parameter values
    @ ctr = 1
    while ($ctr <= $numparams)
      if ($ctr == 1) then
	 $changepgm "'$param[$ctr]'" $argvector[$ctr] $snapwithlogs ./vp1.vm
      else if (($ctr % 2) == 0) then
	 $changepgm "'$param[$ctr]'" $argvector[$ctr] ./vp1.vm ./vp2.vm
      else
         $changepgm "'$param[$ctr]'" $argvector[$ctr] ./vp2.vm ./vp1.vm
      endif
      @ ctr ++
    end

# final version will be in vp1 if the number of parameters is odd else vp2
    if (($numparams % 2) == 0) then
      set snapfile = "./vp2.vm"
    else
      set snapfile = "./vp1.vm"  
    endif

    echo \$sim -x -s 9497 $snapfile $xmax
    $sim -x -s 1 $snapfile  $xmax

#--A log file will be created in the current directory ## 
    $extract `ls| grep log`
    \rm -f *snap*

    $plot < $graphinfo

# increment number of simulations counter
    @ numsim ++


    foreach psfile (`ls *.ps`)
       mv $psfile $snapwithpath.ps
    end

   \rm -f ./final.ps
   \rm -f ./inter.ps

   \rm -f ./*.dat
   \rm -f ./*log*

#END: foreach arg1
   end
#END: while(1)
end

# Remove temporary directory and all the files located there
cd /tmp
\rm -f -r $unique 

## \rm `ls -al | grep $USER | awk '{print $9}'`






