#!/bin/csh -f
#
# Cup install and test script
# Scott Hudson 8/31/95
#  
# Last revision 7/3/96 (for v0.10a)
# By Frank Flannery
#
# Last revision 11/16/96 (for v0.10b)
# By Daniel Wang
#
# Updated version number 7/24/99 for 0.10k
# By C. Scott Ananian
echo 
echo "================================"
echo "Installing and testing Cup v0.10k"
echo "================================"
echo

# check for this directory in CLASSPATH 
#
set cwd = `pwd`
set c_path = `printenv CLASSPATH`
if ($c_path !~ "*$cwd*") then
  echo " "
  echo "WARNING:"
  echo "WARNING: The current directory does not appear in your CLASSPATH"
  echo "WARNING: it will be added for this install/test only"
  echo "WARNING:"
  echo " "
  setenv CLASSPATH $cwd':'$c_path
  echo "CLASSPATH now set to " 
  printenv CLASSPATH
endif
  
# change to the demo directory
#
echo " "
echo "changing to simple_calc subdirectory..."
echo "cd java_cup/simple_calc"
cd java_cup/simple_calc 

# remove old copies of parser.java and sym.java
#
echo " "
echo "removing any old copies of parser.java and sym.java..."
echo "rm -f parser.java sym.java"
rm -f parser.java sym.java

# compile java_cup and run it against the demo program
#   the -cs (for "checksource") option here will force the 
#   java_cup and java_cup.runtime source to be compiled prior 
#   to running it.
#
echo " "
echo "compiling java_cup then generating demo program..."
echo "java -cs java_cup.Main < parser.cup"
java -cs java_cup.Main < parser.cup 

# make sure parser.java and sym.java now exist
#
if ( ! -e parser.java) then
  echo " "
  echo "ERROR: for some reason parser.java was not created"
  echo "ERROR: install was not successful"
  exit 1
endif
if ( ! -e sym.java) then
  echo " "
  echo "ERROR: for some reason sym.java was not created"
  echo "ERROR: install was not successful"
  exit 1
endif

# run the demo
#  again, the -cs option will cause compilation of all the parts 
#  of the demo program (including parser.java and sym.java that 
#  should have been generated in the previous step).
#
echo "removing old test results..."
echo "rm -f test_results"
rm -f test_results
echo " "
echo "executing the demo program..."
echo "echo '1*-2+2;' | java -cs java_cup.simple_calc.Main >& test_results"
echo '1*-2+2;' | java -cs java_cup.simple_calc.Main >& test_results

# compare with standard results 
#
set res = `tail -1 test_results`
if ("$res" !~ "= 0") then
  echo "ERROR: test program produced the wrong results"
  echo "ERROR: output was:"
  cat test_results
  echo "ERROR: install was not successful"
  rm -f test_results
  exit 2
endif
  
# all is well
#
rm -f test_results
echo " "
echo "=============================="
echo "Install and test was successful"
echo "=============================="
exit 0
