#!/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
#     killFoamX
#
# Description
#     Kill script for the FoamX licence manager.
#
#------------------------------------------------------------------------------

PROGNAME=`basename $0`

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


#------------------------------------------------------------------------------
#
# Utilities
#
#------------------------------------------------------------------------------

# 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 grep | head -1 | awk '{ print $1 }'
}

# myKillAll processName
# Tries to kill all (first 10) occurrences of named process
myKillAll() {
    process=`getPID $1`
    if [ ! "$process" ]; then
        echo "$PROGNAME: Process $1 is not running."
    fi

    n=0
    while [ $n -lt 10 ]
    do
        # Kill the Host Browser if it is running
        if [ "$process" ]
        then
	    echo "$PROGNAME: Killing $1(pid $process)."
            kill -9 $process
        else
            break
        fi
        process=`getPID $1`
        n=`expr $n + 1`
    done
}

#------------------------------------------------------------------------------
#
# Main
#
#------------------------------------------------------------------------------

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

myKillAll FoamXCaseServer
myKillAll FoamXCasePostServer
myKillAll FoamXCaseBrowser
myKillAll FoamXHostBrowser

# 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"

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 "$PROGNAME: Killing name server nsd(pid $NAMESERV_THREADS)."
    kill -9 $NAMESERV_THREADS
else
    echo "$PROGNAME: Name server is not running."
fi

# Remove the name server reference file.
rm -f $FOAMX_CONFIG/ns.ref

# Remove the host browser log file
rm -f $FOAMX_CONFIG/HostBrowserLog.xml

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