#!/bin/sh
# Run the CT Corba Demo as an application
# Usage: NonlinearClientApplication [tnamehost modelserverhost]


# This script is fairly fragile.
#
# If this script is called with no arguments, then everything is
# started on the localhost
# If this script is called with two arguments, then the
# pieces of this demo are started up on the two remote hosts
# and this host


# $PTII must be set
if [ -z "${PTII-}" ]; then
    echo "$0: \$PTII was not set, exiting."
    exit 3
fi

CLASSPATH=$PTII
export CLASSPATH

if [ $# -ne 2 ]; then
    # The user did not specify what hosts to run on, so run
    # everything local

    echo "$0: Called with no arguments, so "
    echo "    everything runs locally"
    echo "    To run on multiple hosts, call"
    echo "    $0 hostname1 hostname1"

    tnameserv -ORBInitialPort 1050 &
    # Get the pid of the last process started up in the background
    TPID=$!

    java ptolemy.domains.ct.demo.Corba.ModelServer \
	-ORBInitialHost ${TNAMEHOST:-localhost} -ORBInitialPort 1050 &
    MPID=$!

    # Upon exit, kill these processes
    trap "echo killing $TPID $MPID; kill $TPID $MPID" 1 2 3 9

    java ptolemy.domains.ct.demo.Corba.NonlinearClientApplication \
	   -ORBInitialHost ${TNAMEHOST:-localhost} -ORBInitialPort 1050

else
    # User specified two host names
    TNAMEHOST=$1
    MODELSERVERHOST=$2

    # Under NT, do
    # SSH=ssh
    # export SSH

    if [ -z "${SSH-}" ]; then
	SSH=ssh
    fi

    $SSH $TNAMEHOST tnameserv -ORBInitialPort 1050 &
    # Get the pid of the last process started up in the background
    TPID=$!

    $SSH $MODELSERVERHOST "cd $PTII/ptolemy/domains/ct/demo/Corba;
	 java -cp $PTII ptolemy.domains.ct.demo.Corba.ModelServer \
        -ORBInitialHost ${TNAMEHOST:-localhost} -ORBInitialPort 1050" &
    MPID=$!

    # Upon exit, kill these processes
    trap "echo killing $TPID $MPID; kill $TPID $MPID" 1 2 3 9

    java ptolemy.domains.ct.demo.Corba.NonlinearClientApplication \
	-ORBInitialHost ${TNAMEHOST:-localhost} -ORBInitialPort 1050
fi






