#!/bin/sh
# cg_run: fire up the main program
# this probably gets called from <root>/input.data

# Remove previous dump and solution files if they are about
# to be regenerated
rm -f ../local.data/err* ../log.dat ../mem.dmp* 
if [ `cat trc.dat | awk 'BEGIN {val=0} $1==6 {val=$2} END {print val}'` -gt 0 ]
then
	echo Remove old local data
	(cd ../local.data ; make clean)
fi
if [ `cat trc.dat | awk 'BEGIN {val=0} $1==8 {val=$2} END {print val}'` -eq 1 ]
then
	echo Remove old dumped solution
	rm -f ../local.data/sol*
fi

if [ `cat trc.dat | awk 'BEGIN {val=0} $1==1 {val=$2} END {print val}'` -gt 0 ]
then
    if [ `hostname` -ne 'sun4.epm.ornl.gov' ]
    then
	echo Start XAB monitor
	../../../bin/SUN4/abmon3 > ../local.data/cg.xab &
	sleep 2
    fi
fi

if [ `hostname` = 'sun4.epm.ornl.gov' ] 
then
	rm -f ../nodetrace
	echo Loading program on rx
	rsh rx -n 'cd iterative ; load node ; waitcube'
#	rsh ipsc2 -n 'cd iterative ; load -D node ; waitcube'
#	rcp ../nodetrace eijkhout@cupid.cs.utk.edu:paragraph
else 
	echo Loading PVM program
	../cghost 
fi

# Hm. the host program doesn't wait for the node code to return,
# so the next statements are called too early.
#if [ \
#    `cat trc.dat | awk 'BEGIN {val=0} $1==4 {val=$2} END {print val}'` \
#    -eq 1 ]
#then
#    xterm -e less ./log.dat &
#fi
#if [ \
#    `cat trc.dat | awk 'BEGIN {val=0} $1==4 {val=$2} END {print val}'` \
#    -eq 2 ] 
#then
#    echo Popup xgraph
#    xgraph ../local.data/grdhst.xg &
#fi
# ouch! we never regenerate the spectrum anymore!
# if ( \
#    `cat trc.dat | awk 'BEGIN {val=0} $1==4 {val=$2} END {print val}'` \
#    == 1 ) xgraph ../local.data/spectrum.xg &

if [ `cat trc.dat | awk 'BEGIN {val=0} $1==1 {val=$2} END {print val}'` -gt 0 ]
then
    if [ `hostname` -eq 'sun4.epm.ornl.gov' ]
    then
	echo Copying PG trace file
	rcp ../nodetrace eijkhout@cupid.cs.utk.edu:paragraph
	echo Copying done
    else
	../../../xab3/bin/topg cg.xab
    fi
fi

if [ `cat trc.dat | awk 'BEGIN {val=0} $1==1 {val=$2} END {print val}'` -gt 0 ]
then
	echo Translate XAB file to PG. Some day.
#	(cd ../../../xab3/bin ; topg3 ../../examples/cg/local.data/cg )
fi

exit 0

pop_data:
end:

