#!/bin/sh
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)ss_boot.sh	1.11 99/11/17 Sun Microsystems, Inc.
#
# Boot the SunScreen system (load kernel modules, start daemons, activate a
# compiled configuration).  This is called in one of two situations:
# 1) The system is booting and there is a compiled SunScreen configuration
#    ready to run.
# 2) A configuration is being activated when the SunScreen system was not
#    already running.
#

SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
SS_ETCDIR=/etc/opt/SUNWicg/SunScreen
SS_VARDIR=/var/opt/SUNWicg/SunScreen
PROTO_MODULES=/usr/kernel/misc
ACTIVE=$SS_ETCDIR/.active
OLD=$SS_ETCDIR/.old
read SCREENNAME <$SS_ETCDIR/name

OSVERSION=`uname -v`

start_daemon()
{
    pidfile=${SS_VARDIR}/$1.pid
    /bin/sh -c 'echo $$'" >$pidfile; exec ${SS_LIBDIR}/$* >/dev/console 2>&1" &
}

set -e

#
# Start daemons from /var
#
cd /var

[ -d "${1-$ACTIVE}" ] ||
	error "There is no active configuration ${1-$ACTIVE}"

if [ -s /etc/skip/skipd.pid ] &&
   read pid 2>/dev/null </etc/skip/skipd.pid &&
   /bin/ps -p$pid >/dev/null 2>&1
then :
else sh /etc/init.d/skipkey start
fi


#
# Load any SunScreen protocol modules
#
if [ -d $PROTO_MODULES ]
then for module in ${PROTO_MODULES}/screen_*
     do 
	if [ "$OSVERSION" != "TS7" ] && [ "$module" = "/usr/kernel/misc/screen_ts7" ]; then
		continue
	fi
	/usr/sbin/modload $module
     done
fi

# Startup screen_skip support if SKIP is present on the system.
if [ -f /kernel/drv/skip_key ]
then /usr/sbin/modload -p drv/screen_skip
fi

#
# If HA is being used we must disable sending of packets
# until we know we're the master.
#
if $SS_LIBDIR/ss_ha ${1+-d} $1 CHECK >/dev/null 2>&1
then $SS_LIBDIR/ss_disable_send DISABLE
fi

#
# Activate the configuration.
#
$SS_LIBDIR/ss_activate $1

#
# See if we're supposed to install this temporary directory as the active.
#
if [ $# -gt 1 ]
then
	TEMP=$1
	ACTIVE=$2
	#
	# move temp config to .active
	#
	/usr/sbin/sync
	if [ -d $ACTIVE ]
	then /bin/rm -rf $OLD
	     /bin/mv $ACTIVE $OLD
	fi
	/bin/mv $TEMP $ACTIVE
	/usr/sbin/sync
fi

$SS_LIBDIR/ss_ha ${1+-d} $1 CHECK >/dev/null 2>&1 && start_daemon ss_had
start_daemon ss_logd
start_daemon ss_timed
/etc/init.d/proxy restart >/dev/null 2>&1
$SS_LIBDIR/ssadmserver start >/dev/console 2>&1

#
# record the boot time (sys_info uses this)
#
/bin/date >$SS_VARDIR/boottime
