#!/bin/sh
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)sunscreen.sh	3.14 02/29/00 Sun Microsystems
#
# Start/Stop the SunScreen system
#

SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
SS_ETCDIR=/etc/opt/SUNWicg/SunScreen
SS_VARDIR=/var/opt/SUNWicg/SunScreen

kill_daemon()
{
    pidfile=${SS_VARDIR}/$1.pid
    if [ -f $pidfile ]
    then read pid 2>/dev/null <$pidfile && kill "$pid"
	 /bin/rm $pidfile
    fi
}

kill_killproc()
{	# kill the named process with signal SIGKILL
	pid=`/bin/ps -e |
	     /bin/fgrep " $1" |
	     /usr/bin/awk '{ print $1 }'`
	[ "$pid" != "" ] && kill -9 $pid
}

case "$1" in

'start')
	/bin/rm -f $SS_VARDIR/*.pid $SS_VARDIR/boottime
	find $SS_ETCDIR/configs \( -name '*.lock' -o -name lock \) -print |
		/bin/xargs /bin/rm -f /tmp/xargs_is_stupid
	$SS_LIBDIR/run_httpd start efshttpd

	if [ ! -d $SS_ETCDIR/.active ]
	then echo "SunScreen: No active configuration."
	     exit
	fi
	$SS_LIBDIR/ss_boot

	;;

'stop')
	echo "Shutting down SunScreen processes."
	#
	# If HA is being used don't give routed a chance to send RIP with
	# all routing metrics set to "not reachable." This happens when
	# in.routed or in.rdisc receive SIGTERM, so use SIGKILL instead.
	#
	if $SS_LIBDIR/ss_ha CHECK >/dev/null 2>&1
	then kill_killproc in.route
	     kill_killproc in.rdisc
	fi
	kill_daemon ss_timed
	kill_daemon ss_had
	kill_daemon ss_logd
	$SS_LIBDIR/ssadmserver stop
	$SS_LIBDIR/run_httpd stop
	$SS_LIBDIR/ss_plumb_interface -u

# The following line is commented out to fix bugid 4307432
#	/usr/sbin/modunload -i 0

	/bin/rm -f $SS_VARDIR/*.pid $SS_VARDIR/boottime
	;;

*)	echo>&2 "Usage: $0 { start | stop }"
	;;
esac
