#!/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/CosNaming.h
then
    top_srcdir=ob
elif test -f include/OB/CosNaming.h
then
    top_srcdir=.
elif test -f ../include/OB/CosNaming.h
then
    top_srcdir=..
elif test -f ../../include/OB/CosNaming.h
then
    top_srcdir=../..
elif test -f ../../../include/OB/CosNaming.h
then
    top_srcdir=../../..
elif test -f ../../../../include/OB/CosNaming.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 the C++ distribution
#
have_cpp=
if test -x "../bin/nameserv"
then
    have_cpp="yes"
fi

#
# Check for the Java distribution
#
have_java=
if test -n "$OB_JAVA_DIST" -a \
        -x "$OB_JAVA_DIST/naming/src/com/ooc/CosNaming/Server"
then
    have_java="yes"
fi

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

#
# Print welcome message
#
$echo
$echo "******************************"
$echo "* ORBacus NamingService Demo *"
$echo "******************************"
 
#
# Ask for server
#
server=
while test -z "$server"
do
    $echo
    $echo "Select NamingService to use:"
    $echo
    if test -n "$have_cpp"
    then
	$echo "1: C++ NamingService"
    else
	$echo "1: C++ NamingService (not available)"
    fi
    if test -n "$have_java"
    then
	$echo "2: Java NamingService"
    else
	$echo "2: Java NamingService (not available)"
    fi
    $echo
    $echo "---> \c"
    
    read num
 
    if test "$num" = "1" -a -n "$have_cpp"
    then
        server="../bin/nameserv${exe}"
    fi
 
    if test "$num" = "2"
    then
        server="$JAVA com.ooc.CosNaming.Server"
    fi
done
 
#
# Set name of reference file
#
ref=Naming.ref

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

    if test $srvid -ne 0
    then
        kill $srvid
    fi

    exit
}

#
# Start NamingService
#
$echo
$echo "Waiting for NamingService to start up... \c"
rm -f $ref
$server --ior > $ref &
nsid=$!
trap deactivate 1 2 3 4 5 6 7 8 10 12 13 14 15
count=0
while test ! -s $ref -a $count -lt 6
do
    sleep 1
    count=`expr $count + 1`
done
if test ! -s $ref
then
    $echo "Failed!"
    $echo "(NamingService was not started)"
    exit
else
    $echo "OK!"
fi

#
# Start test server
#
./server${exe} -ORBservice NameService `cat $ref` &
srvid=$!
sleep 3
 
#
# Start test client
#
$top_srcdir/demo/client${exe} -ORBservice NameService `cat $ref`
 
#
# Deactivate naming service and server
#
deactivate
