#!/bin/sh -e
# 
# @(#)setup	1.2 97/10/02
#
# This script sets up the benchmarks package and runs a benchmark session
# To see the available options, run setup -h              
# 
# Note to readers: This shell script is more complicated than simply
# running the commands because it must handle many error conditions,
# especially those that first time users are likely to encounter.
# The meat of the script is down below the functions, where the first
# "show" command is issued (that is, the first line that starts with
# "show").  If you simply want to see what's done, read the
# documentation.  The important commands are:
#
# javac -d . *.java 
# rmic -d . benchmarks.DataCollector benchmarks.TestClient benchmarks.RemoteObj   visualBenchmarks.DataServerImpl
# java benchmarks.DataCollector &
# java benchmarks.TestClient -showProg 
#
# To know more about the available options, use the -help option while running
# the DataCollector or the TestClient
# So if you're looking for information on how to run things yourself,
# just ignore the entire script and use the above commands.
#

#
# the directories in the path
#
pathdirs=`echo $PATH | tr ':' ' '`

#
# find a binary somewhere in the path
#
findbin()
{
    prog=$1;
    for dir in $pathdirs; do
	if [ -x $dir/$prog ]; then
	    echo $dir/$prog
	    return;
	fi
    done
    echo "Cannot find $prog in your path--please check your PATH variable" 1>&2
    exit 1
}

#
# run a command, showing it (if -v is given, only in verbose mode)
#
run()
{
    if [ x"$1" = "x-v" ]; then
	show "$@"
	shift
    else
	show % "$@"
    fi
    eval "$@" || (
	echo Running "$@" 1>&2
	exit 2
    )
}

#
# show some string (if -v, only in verbose)
#
show()
{
    if [ x"$1" = x"-v" ]; then
	if [ "$verbose" != "" ]; then
	    shift
	    echo "..." "$@"
	fi
    elif [ x"$1" = x"-c" ]; then
	shift
	echo ""
	echo "$@"
    else
	echo "$@"
    fi
}

#
# kill off all processes
#
killpids() {
    kill 0
}

#
# Process options
#
set +e
while getopts vgcrh opt; do
    case $opt in
      v) verbose="yes";;
      g)
	show -v cleaning out old stuff
	rm -f benchmarks/*.class core;;
      c) compileonly="yes";;
      r) runonly="yes";;
      h) 
	echo "usage: setup [-v] [-c] [-r] [-g] [-h]"
	echo "   -v: verbose"
	echo "   -c: compile only, do not run"
	echo "   -r: run a benchmark session, do not compile"
	echo "   -g: Remove class files before running"
	echo "   -h: print this usage"  
	exit 1
	;;
      \?)
	echo "usage: setup [-v] [-c] [-r] [-g] [-h]"
	echo "   -v: verbose"
	echo "   -c: compile only, do not run"
	echo "   -r: run a benchmark session, do not compile"
	echo "   -g: Remove class files before running"
	echo "   -h: print this usage"  
	exit 1
	;;
    esac
done
shift `expr $OPTIND - 1`
set -e

#
# Ensure that these programs can be found
#
javac=`findbin javac`
rmic=`findbin rmic`
show -v "javac is $javac"
show -v "rmic is $rmic"

#
# (There is no need to run rmiregistry for this example since
# the DataServer class bundles its own registry.)
#

# The top of the package
top=.

show -v origCLASSPATH=$CLASSPATH
origCLASSPATH=$CLASSPATH
export CLASSPATH

case "$CLASSPATH" in
    "")
	CLASSPATH=$top
	;;
    $top:* | *:$top:* )
	;;
    *)
	CLASSPATH="$top:$CLASSPATH"
	;;
esac

show -v "CLASSPATH=$CLASSPATH"

server=benchmarks.DataCollector
client=benchmarks.TestClient
remoteObj=benchmarks.RemoteObj
dataServer=visualBenchmarks.DataServerImpl


#
# If the benchmarks directory does not exist, create it
#
  test ! -d benchmarks && mkdir benchmarks


#
# If the visualBenchmarks directory does not exist, create it
#

  test ! -d visualBenchmarks && mkdir visualBenchmarks

if [ "$runonly" != "yes" ]; then
  #
  # Compile the source
  #
  show -c "Compiling Java sources"
  run javac -d $top *.java

  #
  # If the reports directory does not exist, create it
  #
  test ! -d reports && mkdir reports

  #
  # Run rmic
  #
  show -c "Running rmic on the implementations"
  run rmic -d $top $server $client $remoteObj $dataServer;
fi

#
# Set up traps so that interrupt kills things off
#
#trap killpids INT EXIT

if [ "$compileonly" != "yes" ]; then 
  #
  # Running the server
  #
  
  show -c "Start server $server"
  run java $server &

  # sleep to give the server time to start up and print its message
  run -v sleep 6

  #
  # Run the TestClient
  #
  show -c "Start TestClient $client"
  run java $client -showProg ;
fi

