#!/bin/sh

# SKIP Source Code License Statement:
# ------------------------------------------------------------------
#   Copyright
#   Sun Microsystems, Inc.
# 
# 
#   Copyright (C) 1994, 1995, 1996 Sun Microsystems, Inc.  All Rights
#   Reserved.
# 
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software or derivatives of the Software, and to 
#   permit persons to whom the Software or its derivatives is furnished 
#   to do so, subject to the following conditions:
# 
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
# 
#   The Software must not be transferred to persons who are not US
#   citizens or permanent residents of the US or exported outside
#   the US (except Canada) in any form (including by electronic
#   transmission) without prior written approval from the US
#   Government. Non-compliance with these restrictions constitutes
#   a violation of the U.S. Export Control Laws.
# 
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT.  IN NO EVENT SHALL SUN MICROSYSTEMS, INC., BE LIABLE
#   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#   CONNECTION WITH THE SOFTWARE OR DERIVATES OF THIS SOFTWARE OR 
#   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
#   Except as contained in this notice, the name of Sun Microsystems, Inc.
#   shall not be used in advertising or otherwise to promote
#   the sale, use or other dealings in this Software or its derivatives 
#   without prior written authorization from Sun Microsystems, Inc.
 
#pragma ident "@(#)rc	1.5 96/10/08 Sun Microsystems"
#
PATH=/bin:/usr/bin:/sbin:/usr/ucb:/usr/etc
SKIP_PATH=${SKIP_PATH-/usr/skip}
SKIPD=/usr/skip/bin/skipd
SKIP_HOST=/usr/skip/bin/skiphost
SKIP_VAR_CMD=/usr/skip/bin/skipvar
SKIP_SECURE_HOSTS=/etc/skip/hosts
SKIP_ETC=/etc/skip
SKIP_VAR=/var/skip
VAR_SIZE=830
SKIP_REQ_FILES="$SKIP_PATH/drv/skip.o $SKIP_ETC/skip.mkdev"
PSEUDO_DRVS="skip"

verify_owner()
{
	me=`whoami`
	if [ $me != "root" ]; then
		echo "skip: this script must be run as root ... fatal error"
		exit 1
	fi
}

pre_checks() {
	for FILE in $SKIP_REQ_FILES; do
	if [ ! -f $FILE ]; then
		echo "skip: installation problem detected"
		echo "skip: $FILE not found"
		echo "skip: please refer to installation guide"
		exit 1
	fi
	done

	if [ ! -w $SKIP_VAR ]; then
		echo "skip: can not write to $SKIP_VAR"
		exit 1
	fi

	# free up old workspace in $SKIP_VAR
	rm -f $SKIP_VAR/skip

	sync

	DISK_AVAIL=`df $SKIP_VAR | awk 'NR==2 {x=$4}
		NR==3 {x=$3}
		END  {print x}'`

	if [ $DISK_AVAIL -lt $VAR_SIZE ]; then
		echo "skip: not enough space available in $SKIP_VAR"
		echo "skip: $VAR_SIZE kbytes will be required to run the product"
	exit 1
	fi
}

load_driver()
{
	modstat > $SKIP_VAR/modules

	# load pseudo-drivers
	for driver in $PSEUDO_DRVS; do

		awk '{ print $8 }' $SKIP_VAR/modules | grep $driver > /dev/null 2>&1

		if [ $? -eq 0 ]; then
			echo "skip: driver already loaded"
		else
			echo "skip: loading driver"
			modload -o $SKIP_VAR/$driver \
				-p $SKIP_ETC/$driver.mkdev \
				$SKIP_PATH/drv/$driver.o> /dev/null


			if [ $? -ne 0 ]; then
				echo "skip: failed to load driver"
				echo "skip: perhaps too many drivers are loaded?"
				exit 1
			fi

		fi
	done
}

start_daemons() {
	if [ -x $SKIPD ]; then
		echo -n "starting skip key manager daemon"
		$SKIPD > /var/log/skipd.log &
		echo "."
	fi

	if [ -x $SKIP_SECURE_HOSTS -a -x $SKIP_HOST ]; then
		$SKIP_SECURE_HOSTS >> /var/log/skipd.log 2>&1 &
	fi
}

# main
verify_owner
pre_checks
load_driver
start_daemons

exit 0
