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

#
# Try to find top-level directory
#
if test -f naming/src/com/ooc/CosNaming/NamingContext.java
then
    top_srcdir=naming
elif test -f src/com/ooc/CosNaming/NamingContext.java
then
    top_srcdir=.
elif test -f ../src/com/ooc/CosNaming/NamingContext.java
then
    top_srcdir=..
elif test -f ../../src/com/ooc/CosNaming/NamingContext.java
then
    top_srcdir=../..
elif test -f ../../../src/com/ooc/CosNaming/NamingContext.java
then
    top_srcdir=../../..
elif test -f ../../../../src/com/ooc/CosNaming/NamingContext.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/naming/bin/nameserv"
then
    have_cpp="yes"
fi
have_java="yes"

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

#
# Set name of database file
#
db="$top_srcdir/test/Naming.db"

#
# Set name of reference file
#
ref="$top_srcdir/test/Naming.ref"

#
# Print welcome message
#
$echo
$echo "****************************"
$echo "* Naming Server Test-Suite *"
$echo "****************************"

#
# 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++ NamingService, transient"
        $echo "2: C++ NamingService, persistent"
    else
        $echo "1: C++ NamingService, transient (not available)"
        $echo "2: C++ NamingService, persistent (not available)"
    fi
    $echo
    if test -n "$have_java"
    then
	$echo "3: Java server, transient"
	$echo "4: Java server, persistent"
    else
	$echo "3: Java server, transient (not available)"
	$echo "4: Java server, persistent (not available)"
    fi
    $echo
    $echo "---> \c"

    read num

    if test "$num" = "1" -a -n "$have_cpp"
    then
	server="$OB_CPP_DIST/naming/bin/nameserv${exe}"
    fi

    if test "$num" = "2" -a -n "$have_cpp"
    then
	server="$OB_CPP_DIST/naming/bin/nameserv${exe} --database $db --start"
    fi

    if test "$num" = "3"
    then
	server="$JAVA com.ooc.CosNaming.Server"
    fi

    if test "$num" = "4"
    then
	server="$JAVA com.ooc.CosNaming.Server --database $db --start"
    fi
done

#
# Display info
#
$echo
if test -n "$have_java"
then
    $echo Java CLASSPATH is \"CLASSPATH=$CLASSPATH\"
fi

client="$JAVA test.Client"
$echo Client command is \"$client\"
$echo Server command is \"$server\"

#
# 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 naming server
#
$echo
$echo "Waiting for naming server to start up... \c"
rm -f $ref $db
$server --ior > $ref 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 -ORBservice NameService `cat $ref` 2> client.log

#
# Deactivate server
#
deactivate
