#! /bin/sh

############################################################
# This shell script is a "front-end" to twobody which 
# expedites the analysis of the results from that code,
# including the generation of Postscript plots of the 
# particle position, d(energy), d(angular momentum) as a 
# function of time using gnuplot.
############################################################
P=`basename $0`

############################################################
# Set defaults 
############################################################
tmax=5.0
dt=0.05
tol=1.0d-6

############################################################
# Usage
############################################################
Usage() {
cat<<END
usage: $P <y0> [<tol>]

       Default tol: $tol

       y0 = 1.0 will produce circular orbit.

       To enable automatic previewing of Postscript files
       set GV environment variable to any non-blank
       value, e.g.
   
       setenv GV on
END
exit 1
}

############################################################
# Subroutine (fcn) to produce postscript version of 
# supermongo ('sm') plot of data stored in file $1
# Postscript file will be called $1_sm.ps.  If optional 
# second argument is supplied, the resulting Postscript file 
# will be 'gv'ed.   
############################################################
sm_it(){
smit_psname="$1_sm.ps"
ylab=`echo $1 | tr '_' '-'`

sm<<END
   device postencap $smit_psname
   erase
   data "$1"
   read {x 1 y 2}
   limits x y
   expand 1.0001
   box
   expand 1.25
   points x y
   expand 1.25
   xlabel x
   ylabel $ylab
   quit
END
if [ "${2}undefined" != undefined ]; then
   if [ -f $smit_psname ]; then
      (gv $smit_psname) &
   else
       echo "smit: $smit_psname.ps does not exist"
   fi
fi
}


############################################################
# Argument handling
############################################################
case $# in
1|2) y0=$1; tol=${2-$tol};;
*) Usage;;
esac

############################################################
# Build application, run it, and process the results.
############################################################
make -f Makefile twobody

tag="$y0"_"$tol"
ofile=out_"$tag"

twobody 0.0 $y0 1.0 0.0 $tmax $dt $tol > $ofile

nth 2 3 < $ofile > xcyc_$tag
nth 1 2 < $ofile > xc_$tag
nth 1 3 < $ofile > yc_$tag
nth 1 4 < $ofile > dEtot_$tag
nth 1 5 < $ofile > dJtot_$tag

for f in xcyc_$tag xc_$tag yc_$tag dEtot_$tag dJtot_$tag; do
   sm_it $f $GV
   /bin/rm $f
done
/bin/ls -l *$tag*.ps

exit 0