#!/bin/sh

PRG="$0"

# resolve symlinks
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
done

dir=`dirname "$PRG"`
dir=`cd "$dir" && pwd`

. "$dir"/../configuration.sh

case "`uname`" in
CYGWIN*)
    IBIS_ROOT=`cygpath --unix "$IBIS_ROOT"`
    CLASSPATH=`cygpath --unix --path "$CLASSPATH"`
    ;;
esac

LIBCLASSPATH=""

add_to_libclasspath () {
    DIRLIBS=`cd "$1"; ls *.jar`
    for i in ${DIRLIBS}
    do
	# if the directory is empty, then it will return the input string
	if [ "$i" != "${DIRLIBS}" ] ; then
	    if [ -z "$LIBCLASSPATH" ] ; then
		LIBCLASSPATH="$1/$i"
	    else
		LIBCLASSPATH="$1/$i":$LIBCLASSPATH
	    fi
	fi
    done
}

# Add the following dirs to the classpath 
add_to_libclasspath "${IBIS_ROOT}"/3rdparty

case "`uname`" in
CYGWIN*)
    JAVACLASSPATH="$CLASSPATH:$IBIS_ROOT/build/ibis.jar:$LIBCLASSPATH:"
    JAVACLASSPATH=`cygpath --path --windows "$JAVACLASSPATH"`
    ;;
*)
    JAVACLASSPATH="$CLASSPATH:$IBIS_ROOT/build/ibis.jar:$LIBCLASSPATH:"
    ;;
esac

has_enable=0
has_hubport=0
has_hubhost=0
Ddefines=

# Ibis Hub controller
if [ "x$IBIS_HUB" != "x" ]; then
  # decode Ibis control hub reference
  HUB_HOST=`echo $IBIS_HUB|cut -f 1 -d :`
  HUB_PORT=`echo $IBIS_HUB|cut -f 2 -d :`
  echo "Name service will connect to Ibis hub:"
  echo "  ${HUB_HOST}:${HUB_PORT}"
  Ddefines="-Dibis.connect.hub.host=$HUB_HOST -Dibis.connect.hub.port=$HUB_PORT"
  has_enable=1
  has_hubport=1
  has_hubhost=1
fi

args=
javaargs=

usage () {
    cat << 'EOF'
Usage:
  ibis-nameserver <nameserver parameters>

The nameserver parameters are:
-single
    make the nameserver only serve a single Ibis run. (In fact, it will
    server more than one run, but will exit as soon as no more runs are
    active).
-port <portno>
    make the nameserver listen to port number <portno>. The default
    port number is 9826.
-ns-port <portno>
    same as "-port".
-poolserver
    make the nameserver start a pool server (see ibis.util.PoolInfo in the
    Ibis API). This is the default.
-no-poolserver
    with this option, the nameserver does not start a pool server.
-poolport <poolportno>
    make the poolserver listen to port number <poolportno>. The default
    port number is the nameserver port number + 1.
-controlhub
    make the nameserver start a controlhub
    (see ibis/docs/notes/Crossing-firewalls.txt). Note that this
    option also makes the nameserver use communication that goes through
    this controlhub.
-no-controlhub
    do not make the nameserver start a controlhub. This is the default.
-hubport <hubportno>
    make the controlhub listen to port number <hubportno>. The default port
    number is the nameserver port number + 2. If the -controlhub flag is not
    given, it is assumed that one is already listening on port <hubportno>.
    Anyway, the nameserver will use communication that goes through a
    controlhub.
-hubhost <hubhostname>
    specifies the hostname with the controlhub that is to be used by the
    nameserver. All communication with the nameserver will go through this
    controlhub.
-v
-verbose
    make the nameserver verbose
-?
-h
-help
--help
    print this message

Unrecognized options are passed on to java.

EOF
    exit 1
}

while [ $# -gt 0 ]
do
    case "$1" in
    -\?|-h|-help|--help)
	usage
	;;
    -poolserver|-no-poolserver|-single|-no-controlhub)
	args="$args $1"
	;;
    -v|-verbose)
	args="$args -verbose"
	;;
    -controlhub)
	# Note: ibis.connect.data_links must be set or ibis.connect does not
	# work. However, the nameserver only uses control_links, so the
	# contents for data_links does not really matter, as long as the
	# socket type exists.
	if [ "x$IBIS_HUB" != "x" ]; then
		echo "You cannot use both -controlhub and the IBIS_HUB environment variable" 1>&2
		exit 1
	fi
	has_enable=1
	if [ $has_hubhost -eq 0 ]; then
	    has_hubhost=1
	    Ddefines="$Ddefines -Dibis.connect.hub.host=`hostname -f`"
	fi
	args="$args -controlhub"
	;;
    -port|-ns-port)
	shift
	Ddefines="$Ddefines -Dibis.name_server.port=$1"
	;;
    -poolport|-pool-port)
	shift
	Ddefines="$Ddefines -Dibis.pool.server.port=$1"
	;;
    -hubport|-hub-port)
	shift
	if [ "x$IBIS_HUB" != "x" ]; then
		echo "You cannot use both -hubport and the IBIS_HUB environment variable" 1>&2
		exit 1
	fi
	if [ $has_hubport -eq 0 ]; then
	    has_hubport=1
	    Ddefines="$Ddefines -Dibis.connect.hub.port=$1"
	fi
	has_enable=1
	;;
    -hubhost|-hub-host)
	shift
	if [ "x$IBIS_HUB" != "x" ]; then
		echo "You cannot use both -hubhost and the IBIS_HUB environment variable" 1>&2
		exit 1
	fi
	if [ $has_hubhost -eq 0 ]; then
	    has_hubhost=1
	    Ddefines="$Ddefines -Dibis.connect.hub.host=$1"
	fi
	has_enable=1
	;;
    *)
	javaargs="$javaargs $1"
	;;
    esac
    shift
done

if [ $has_enable -eq 1 ]; then
    Ddefines="$Ddefines -Dibis.connect.control_links=RoutedMessages -Dibis.connect.data_links=PlainTCP -Dibis.name_server.host=`hostname -f`"
fi

"$JAVA_ROOT"/bin/java -classpath "$JAVACLASSPATH" $Ddefines $javaargs ibis.impl.nameServer.tcp.NameServer $args
