#!/bin/bash

PROCESSNAME=OpenORBNS

# monitor
export AMSLOG="/opt/tandbergtv/watchpoint/log"

# open orb ins
export TCOO_HOME=/usr/local/OpenORB
CONFIG_FILE="/usr/local/n2bb/alerts/conf/OpenORB.xml"
PROCESS_ARGS="-ORBConfig=$CONFIG_FILE -ORBPort=4000 -p file -shutdown"
BOOTCLASSPATH=$TCOO_HOME/OpenORB/lib/endorsed/openorb_orb_omg-1.4.0.jar
CLASS=org.openorb.ins.Server
VM_ARGS="-Xbootclasspath/p:$BOOTCLASSPATH -Dopenorb.home.path=$TCOO_HOME -Diiop.hostname=AlertNameServer"
CMD_LINE="java $VM_ARGS -jar $TCOO_HOME/tools/lib/launcher.jar $CLASS $PROCESS_ARGS"
case "$1" in
'start')
    su nobody -c "$CMD_LINE >> $AMSLOG/$PROCESSNAME.log 2>&1 &"
    echo "OK"
    ;;
'stop')
    pid=$(ps axww | grep "[A]lertNameServer" | grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
    # Forcefully kill the process in case if it is still running0o-
    if [ ! -z "${pid}" ] ; then
        kill -9 $pid
    fi
    ;;
'status')
    pid=$(ps axww | grep "[A]lertNameServer"| grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
     if [ ! -z "${pid}" ] ; then
        echo "Alerts NameService (pid "$pid") is running..."
      else
        echo "Alerts NameService is stopped"
     fi
     ;;
     
'restart')
    $0 stop
    sleep 2
    $0 start
    ;;
*)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;

esac
exit 0
