#!/bin/sh -f
#
# Start/Stop NCS daemons
#
# Copyright 1991 Silicon Graphics, Inc.
# All rights reserved.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
# the contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of Silicon Graphics, Inc.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
# rights reserved under the Copyright Laws of the United States.
#
# $Revision: 1.6 $

NCS=/usr/etc
GLBDB=/var/ncs/glb.e
SU="/bin/su daemon -c"

CONFIG=/etc/config
IS_ON=/etc/chkconfig

if $IS_ON verbose ; then
    ECHO=echo
else		# For a quiet startup and shutdown
    ECHO=:
fi

case "$1" in
'start')
	if $IS_ON llb || $IS_ON glb; then
	    killall -TERM glbd llbd

	    $ECHO "NCS daemons:\c"
	    if $IS_ON llb && test -x $NCS/llbd; then
		    $NCS/llbd `cat $CONFIG/llbd.options 2> /dev/null` &
		    $ECHO " llbd\c"
	    fi

	    if $IS_ON glb && test -x $NCS/glbd; then
		    if test -f $CONFIG/glbd.options; then
			    GOPTIONS="`cat $CONFIG/glbd.options`"

			# If no database, create the first one
			#
			# Note that if more than one glbd is to run on the same
			# network, or to create a replica of a glb on another
			# network, glbd must be started by hand the first time.
			# The first time glbd is started it creates its database
			# and does not need command line options for subsequent
			# invocations.
		    elif test ! -f $GLBDB; then
			    GOPTIONS="-first -create"
		    else
			    GOPTIONS=
		    fi
		    #
		    # Run as as the user "daemon" if possible
		    #
		    if $SU echo >/dev/null 2>&1; then
			    chown -R daemon /var/ncs
			    $SU "$NCS/glbd $GOPTIONS" &
		    else
			    $NCS/glbd $GOPTIONS &
		    fi
		    $ECHO " glbd\c"
	    fi
	    $ECHO "."
	fi
	;;

'stop')
	killall -TERM glbd llbd
	;;

*)
    echo "usage: $0 {start|stop}"
	;;
esac
