#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software; you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by the
#     Free Software Foundation; either version 2 of the License, or (at your
#     option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM; if not, write to the Free Software Foundation,
#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script
#     runFoamXHB
#
# Description
#     Loader for FoamX host browser.
#
#------------------------------------------------------------------------------

#- Set some system specifics
case `uname -s` in
  HP-UX*)
    UNIX95=a; export UNIX95
    ;;
  IRIX*)
    _XPG=1; export _XPG
    ;;
esac
COLUMNS=200
export COLUMNS


# getValue file var
#   Prints value of 'var=value' in file.
getValue() {
    val=`grep "^[ \t]*$2[ \t]*=" $1 | sed -e "s/^[ \t]*$2[ \t]*=//" | head -1`
    if [ -z "$val" ]; then
        return 1
    fi
    echo "$val"
    return 0
}

# getPID processName
# Prints pid of named process (looks at users processes only)
getPID() {
    ps -U $LOGNAME -o 'pid,args' | fgrep " $1" | fgrep -v fgrep | head -1 | awk '{ print $1 }'
}

# cleanup
# removes all excess processes and files. Used in signal handler
cleanup() {
    echo "runFoamXHB : cleanup"

    #- reset traps
    trap 0 2 3 15

    NAMESERV_THREADS=`ps -U $LOGNAME -o 'pid,args' | fgrep " nsd" | fgrep "$myIOP" | fgrep -v fgrep | head -1 | awk '{ print $1 }'`
    # Kill the MICO nameserver if it is running
    if [ "$NAMESERV_THREADS" ]; then
	    echo "`basename $0`: Killing name server nsd(pid $NAMESERV_THREADS)."
        kill -9 $NAMESERV_THREADS
    else
	    echo "`basename $0`: Name server is not running."
    fi

    #- Clean up files
    rm -f $FOAMX_CONFIG/HostBrowserLog.xml

    #- nsd should already be done by nsd subshell but just to make sure.
    rm -f $FOAMX_CONFIG/ns.ref
}

#-------------------------
#
# Main.
#
#-------------------------

# Make sure that the FoamX configuration directory is available.
if [ ! -d "$FOAMX_CONFIG" ] ; then
    echo "Can't find FoamX configuration directory at $FOAMX_CONFIG."
    exit 1
fi

# Make sure that the FoamX client configuration file is available.
if [ ! -r "$FOAMX_CONFIG/FoamXClient.cfg" ] ; then
    echo "Can't find FoamX FoamXClient.cfg file in $FOAMX_CONFIG."
    exit 1
fi

# Get host and port from FoamX client client configuration file.
HOSTNAME=`getValue "$FOAMX_CONFIG/FoamXClient.cfg" 'org.omg.CORBA.ORBInitialHost'`
if [ $? -ne 0 ]; then
    HOSTNAME=`uname -n`
fi
HOSTPORT=`getValue "$FOAMX_CONFIG/FoamXClient.cfg" 'org.omg.CORBA.ORBInitialPort'`
if [ $? -ne 0 ]; then
    HOSTPORT=1234
fi
myIOP="inet:$HOSTNAME:$HOSTPORT"

#- Cleanup processes started by me
trap cleanup 0 2 3 15

rm -f $FOAMX_CONFIG/ns.ref
echo "Starting NameServer with $myIOP ..."
nsd -ORBIIOPAddr $myIOP --ior $FOAMX_CONFIG/ns.ref </dev/null &

while [ ! -f $FOAMX_CONFIG/ns.ref ]
do
    sleep 1
done

rm -f $FOAMX_CONFIG/HostBrowserLog.xml
echo "Starting FoamX Host Browser with $myIOP ..."
FoamXHostBrowser -ORBNamingAddr $myIOP

#- Alternatively use the corbaloc standard. Unfortunately that does not seem
#  to work on the C++ end (the nsd starts up fine)
#orbArgs="-ORBDefaultInitRef corbaloc://$HOSTNAME:$HOSTPORT"
#nsd $orbArgs --ior $FOAMX_CONFIG/ns.ref </dev/null &
#FoamXHostBrowser $orbArgs

# Implicit trap 0

#------------------------------------------------------------------------------
