#!/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/include/OB/CORBA.h
then
    top_srcdir=ob
elif test -f include/OB/CORBA.h
then
    top_srcdir=.
elif test -f ../include/OB/CORBA.h
then
    top_srcdir=..
elif test -f ../../include/OB/CORBA.h
then
    top_srcdir=../..
elif test -f ../../../include/OB/CORBA.h
then
    top_srcdir=../../..
elif test -f ../../../../include/OB/CORBA.h
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 JTC
#
with_jtc=
if test "$windows" = "yes"
then
    with_jtc=`grep "^WITH_JTC.*=.*yes" $top_srcdir/../config/Make.rules.mak`
else
    with_jtc=`grep "^#define HAVE_JTC 1" $top_srcdir/include/OB/Config.h`
fi

#
# Check for the Java distribution
#
have_java=
if test -n "$OB_JAVA_DIST" -a \
        -r "$OB_JAVA_DIST/ob/lib/test/Server.class" -a \
        -r "$OB_JAVA_DIST/ob/lib/test/Client.class"
then
    have_java="yes"
fi

#
# Set Java CLASSPATH
#
if test -n "$have_java"
then
    if test "$windows" = "yes"
    then
	CLASSPATH="$OB_JAVA_DIST/ob/lib;$CLASSPATH"
    else
	CLASSPATH="$OB_JAVA_DIST/ob/lib:$CLASSPATH"
    fi
fi
export CLASSPATH

#
# Print welcome message
#
$echo
$echo "**********************"
$echo "* ORBacus Test-Suite *"
$echo "**********************"

#
# Ask for client
#
client=
while test -z "$client"
do
    $echo
    $echo "Select test client to use:"
    $echo
    $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
    $echo
    if test -n "$have_java"
    then
	$echo "4: Java client, blocking"
	$echo "5: Java client, threaded"
    else
	$echo "4: Java client, blocking (not available)"
	$echo "5: Java client, threaded (not available)"
    fi
    $echo
    $echo "---> \c"

    read num

    if test "$num" = "1"
    then
	client="$top_srcdir/test/client${exe} -ORBblocking"
    fi

    if test "$num" = "2"
    then
	client="$top_srcdir/test/client${exe} -ORBreactive"
    fi

    if test "$num" = "3" -a -n "$with_jtc"
    then
        client="$top_srcdir/test/client${exe} -ORBthreaded"
    fi

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

    if test "$num" = "5" -a -n "$have_java"
    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
    $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
    $echo
    if test -n "$have_java"
    then
	$echo "4: Java server, blocking"
	$echo "5: Java server, threaded"
    else
	$echo "4: Java server, blocking (not available)"
	$echo "5: Java server, threaded (not available)"
    fi
    $echo
    $echo "---> \c"

    read num

    if test "$num" = "1"
    then
	server="$top_srcdir/test/server${exe} -OAblocking"
    fi

    if test "$num" = "2"
    then
	server="$top_srcdir/test/server${exe} -OAreactive"
    fi

    if test "$num" = "3" -a -n "$with_jtc"
    then
	server="$top_srcdir/test/server${exe} -OAthreaded"
    fi

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

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

#
# Display info
#
$echo
if test -n "$have_java"
then
    $echo Java CLASSPATH is \"CLASSPATH=$CLASSPATH\"
fi
$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
