#!/bin/sh
#
# Startup script to start/stop the omniORB services
#
# chkconfig: 345 90 10
# description: CORBA Naming Service from omniORB
# processname: omniNames
# config: /etc/omniORB.cfg
#
# Author: Sander Steffann <sander@steffann.nl>
# Modified: Thomas Lockhart <lockhart@fourpalms.org>
# - Put omniNames files under /var/log/omniNames
# - Adjust start/stop levels
# Modified: Duncan Grisby <duncan@grisby.org>
# - Put omniNames files under /var/omniNames, since they are not log files

# Standard functions
. /etc/rc.d/init.d/functions

# Program options
GENIOR=/alma/ACS-8.2/Python/omni/bin/genior
OMNINAMES=/alma/ACS-8.2/Python/omni/bin/omniNames
OMNINAMES_DIR=/var/omniNames
OMNIMAPPER_DIR=/var/lib/omniMapper

# Make sure libraries are found if we're installed in a non-standard location
prefix=/alma/ACS-8.2/Python/omni
exec_prefix=${prefix}
export LD_LIBRARY_PATH=${exec_prefix}/lib:$LD_LIBRARY_PATH

# Registered port number for CORBA naming service is 2809.
# Should be preferred but may conflict with other ORBs running concurrently.
TCPPORT=2809

if [ -f /etc/sysconfig/omniNames ];then
	. /etc/sysconfig/omniNames
fi

case "$1" in
  start)
	# Make sure directories exist
	[ -d $OMNINAMES_DIR ] || mkdir -p $OMNINAMES_DIR
	[ -d $OMNIMAPPER_DIR ] || mkdir -p $OMNIMAPPER_DIR
  
	if [ -x $OMNINAMES ]; then
		echo -n "Starting omniNames"

		if [ ! -z "`pidofproc omniNames`" ]; then
			echo " impossible: already running (pid `pidofproc omniNames`)"
		else
			OMNINAMES_OPTIONS="-errlog $OMNINAMES_DIR/error.log -logdir $OMNINAMES_DIR"

			if [ ! -f $OMNINAMES_DIR/omninames-*.log ]; then
				OMNINAMES_OPTIONS="$OMNINAMES_OPTIONS -start $TCPPORT"

				echo -n " for the first time"
			fi

			# Run it
			su omni -c "$OMNINAMES $OMNINAMES_OPTIONS" &> /dev/null &
			OMNINAMES_PID=$!

			# Sleep a while, and then check if omniNames is still running
			sleep 2

			if [ ! -z "`pidofproc omniNames`" ]; then
				success "Starting omniNames"
				echo $OMNINAMES_PID > /var/run/omniNames.pid
				
				$GENIOR IDL:omg.org/CosNaming/NamingContextExt:1.0 `hostname` $TCPPORT NameService > $OMNIMAPPER_DIR/NameService
			else
				failure "Starting omniNames"
				
				rm -f $OMNIMAPPER_DIR/NameService
				rm -f /var/run/omniNames.pid
			fi

			echo
		fi
	fi

	if [ ! -z "`pidofproc omniNames`" ]; then
		touch /var/lock/subsys/omniNames
	fi
	;;

  stop)
	echo -n "Stopping omniNames"
	killproc omniNames
	echo

	rm -f $OMNIMAPPER_DIR/NameService

	while [ -n "`pidofproc omniNames`" ]; do
		sleep 1
	done

	rm -f /var/lock/subsys/omniNames
	;;

  restart)
	$0 stop
	$0 start
	;;

  status)
	status omniNames
	;;

  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit 0

