#!/bin/sh
#
# Test server connection.
#
# See the file "license.txt" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# ----------------------------------------------------------------------
#

#
# Get home directory
#

cwd=`pwd`
cd `dirname $0`
LDIR=`pwd`; export LDIR
cd $cwd

#
# Do some platform-specific setup
#

case `uname -s` in
    Linux|SunOS)
        ps="ps -e"
    ;;
    Darwin|FreeBSD)
        ps="ps -ax"
    ;;
    *)
        echo "Unsupported platform `uname -s`" >&2
        exit 130
    ;;
esac

srv=lexxsrv
pid=`cat "$LDIR"/log/$srv.pid 2>/dev/null`
if test $? -gt 0; then
    echo "can't connect to server: not running?"
    exit 1
fi

new=`$ps | grep nsd | grep $pid | grep -v grep | awk '{print $1;}'`
if test -z "$pid" -o -z "$new"; then
    echo "can't connect to server $srv: no pid: $pid"
    exit 1
fi

#
# Use CLI to gather real server data
#

cd "$LDIR"
bin/nsdchat -c "srvinfo hostname" >/tmp/$srv.hnam 2>/dev/null
ret=$?
if test $ret -ne 0; then
    echo "can't connect to server: not running?"
else
    bin/nsdchat -c "srvinfo address" >/tmp/$srv.addr 2>/dev/null
    if test $? -eq 0; then
        addr=`cat /tmp/$srv.addr`
        if [ "$addr" =  "::1" ]; then
            addr=`cat /tmp/$srv.hnam`
        fi
    fi
    bin/nsdchat -c "srvinfo port" > /tmp/$srv.port 2>/dev/null
    bin/nsdchat -c "srvinfo lexxvers" > /tmp/$srv.vers 2>/dev/null
    urlh="http://$addr:`cat /tmp/$srv.port`/login"
    urls="https://$addr:`expr \`cat /tmp/$srv.port\` + 443`/login"
    echo "pid: $pid (version \"`cat /tmp/$srv.vers`\" running)"
    echo "url: $urlh, $urls (use web-browser to connect)"
    rm -rf /tmp/$srv.*
fi
cd $cwd

exit $ret

