#!/bin/sh
# **********************************************************************
#
# Copyright (c) 1999
# Object Oriented Concepts, Inc.
# Billerica, MA, USA
#
# All Rights Reserved
#
# **********************************************************************

#
# Try to find top-level directory
#
if test -f ob/src/com/ooc/CORBA/ORB.java
then
    top_srcdir=ob
elif test -f src/com/ooc/CORBA/ORB.java
then
    top_srcdir=.
elif test -f ../src/com/ooc/CORBA/ORB.java
then
    top_srcdir=..
elif test -f ../../src/com/ooc/CORBA/ORB.java
then
    top_srcdir=../..
elif test -f ../../../src/com/ooc/CORBA/ORB.java
then
    top_srcdir=../../..
elif test -f ../../../../src/com/ooc/CORBA/ORB.java
then
    top_srcdir=../../../..
else
    $echo "$0: can't find top-level directory"
    exit
fi

#
# Run standard init script
#
. $top_srcdir/../config/sh.init

#
# Check for the C++ distribution
#
have_cpp=
if test -n "$OB_CPP_DIST" -a \
        -x "$OB_CPP_DIST/ob/test/server" -a \
        -x "$OB_CPP_DIST/ob/test/client"
then
    have_cpp="yes"
fi

#
# Check for JTC
#
with_jtc=
if test -n "$have_cpp"
then
    if test "$windows" = "yes"
    then
	with_jtc=`grep "^WITH_JTC.*=.*yes" \
		  $OB_CPP_DIST/config/Make.rules.mak`
    else
	with_jtc=`grep "^#define HAVE_JTC 1" \
		  $OB_CPP_DIST/ob/include/OB/Config.h`
    fi
fi

#
# Set Java CLASSPATH
#
if test "$windows" = "yes"
then
    CLASSPATH="$top_srcdir/lib;$CLASSPATH"
else
    CLASSPATH="$top_srcdir/lib:$CLASSPATH"
fi
export CLASSPATH

#
# Print welcome message
#
$echo
$echo "**********************"
$echo "* ORBacus Testsuite  *"
$echo "**********************"

#
# Ask for client
#
client=
while test -z "$client"
do
    $echo
    $echo "Select test client to use:"
    $echo
    if test -n "$have_cpp"
    then
	$echo "1: C++ client, blocking"
	$echo "2: C++ client, reactive"
	if test -n "$with_jtc"
	then
	    $echo "3: C++ client, threaded"
	else
	    $echo "3: C++ client, threaded (not available)"
	fi
    else
	$echo "1: C++ client, blocking (not available)"
	$echo "2: C++ client, reactive (not available)"
        $echo "3: C++ client, threaded (not available)"
    fi
    $echo
    $echo "4: Java client, blocking"
    $echo "5: Java client, threaded"
    $echo
    $echo "---> \c"

    read num

    if test "$num" = "1" -a -n "$have_cpp"
    then
	client="$OB_CPP_DIST/ob/test/client -ORBblocking"
    fi

    if test "$num" = "2" -a -n "$have_cpp"
    then
	client="$OB_CPP_DIST/ob/test/client -ORBreactive"
    fi

    if test "$num" = "3" -a -n "$have_cpp" -a -n "$with_jtc"
    then
        client="$OB_CPP_DIST/ob/test/client -ORBthreaded"
    fi

    if test "$num" = "4"
    then
	client="$JAVA test.Client -ORBblocking"
    fi

    if test "$num" = "5"
    then
	client="$JAVA test.Client -ORBthreaded"
    fi
done

#
# Ask for server
#
server=
while test -z "$server"
do
    $echo
    $echo "Select test server to use:"
    $echo
    if test -n "$have_cpp"
    then
	$echo "1: C++ server, blocking"
	$echo "2: C++ server, reactive"
	if test -n "$with_jtc"
	then
	    $echo "3: C++ server, threaded"
	else
	    $echo "3: C++ server, threaded (not available)"
	fi
    else
	$echo "1: C++ server, blocking (not available)"
	$echo "2: C++ server, reactive (not available)"
        $echo "3: C++ server, threaded (not available)"
    fi
    $echo
    $echo "4: Java server, blocking"
    $echo "5: Java server, threaded"
    $echo
    $echo "---> \c"

    read num

    if test "$num" = "1" -a -n "$have_cpp"
    then
	server="$OB_CPP_DIST/ob/test/server -OAblocking"
    fi

    if test "$num" = "2" -a -n "$have_cpp"
    then
	server="$OB_CPP_DIST/ob/test/server -OAreactive"
    fi

    if test "$num" = "3" -a -n "$have_cpp" -a -n "$with_jtc"
    then
	server="$OB_CPP_DIST/ob/test/server -OAthreaded"
    fi

    if test "$num" = "4"
    then
	server="$JAVA test.Server -OAblocking"
    fi

    if test "$num" = "5"
    then
	server="$JAVA test.Server -OAthreaded"
    fi
done
server="$server -OAhost localhost"

#
# Display info
#
$echo
$echo Java CLASSPATH is \"CLASSPATH=$CLASSPATH\"
$echo Server command is \"$server\"
$echo Client command is \"$client\"

#
# Set name of reference file
#
ref=TestInterface.ref

#
# Function to deactivate the server
#
srvid=0
deactivate()
{
    if test $srvid -ne 0
    then
        kill $srvid
    fi

    exit
}
trap deactivate 1 2 3 4 5 6 7 8 10 12 13 14 15

#
# Start server
#
$echo
$echo "Waiting for server to start up... \c"
rm -f $ref
$server 2> server.log &
srvid=$!
count=0
while test ! -s $ref -a $count -lt 8
do
    sleep 1
    count=`expr $count + 1`
done
if test ! -s $ref
then
    $echo "Failed!"
    $echo "(Server was not started)"
    exit
else
    $echo "OK!"
fi

#
# Start client
#
$client 2> client.log

#
# Wait for server deactivation
#
$echo "Waiting for server to deactivate... \c"
count=0
while test -r $ref -a $count -lt 3
do
    sleep 1
    count=`expr $count + 1`
done

if test -r $ref
then
    $echo "Failed!"
    $echo "(Server was not deactivated by client - deactivating server now)"
    deactivate
else
    $echo "OK!"
fi
