#!/bin/sh
#
# @(#)$Id: BBoot.sh,v 1.1.2.14.2.3 1998/09/10 13:47:57 cvsusr Exp $
#
# Copyright 1996 Dynamic Software AB
# This is unpublished proprietary source code.
#
# Description: Start or stop BoKS
#
# Usage:
#	Boot		to restart BoKS, as currently configured
# 	Boot -k 	to kill running processes then exit
# 	Boot -b 	("boot") at boot time, to clean up btmp etc in
#			addition to starting BoKS daemons.
#	Boot -i		initial boot at install (empty database)
#	Boot -v [opt]	be verbose
#
cd /
umask 022
KILL=false
BOOT=false
INSTALLED=true
LICENSE=yes; export LICENSE; #Temporary license
PATH=/sbin:/usr/sbin:/etc:/usr/bin
# The following line added to support japanese kanji.
LANG=C; LC_ALL=C; export LANG LC_ALL

for f in /etc/opt/SUNWssmi/ENV /etc/boksenv; do
    if [ -f $f ]; then
	eval `grep '^BOKS_lib=' $f`
	PATH="$BOKS_lib:$PATH"
	break
    fi
done
test -z "$BOKS_lib" && exit 1
# Get BoKS locations
for d in DIR etc sbin lib tmp var data; do
    eval BOKS_$d="`boksdir $d`"
    export BOKS_$d
done
test -z "$BOKS_lib" && { echo "Must be run in a BoKS environment" >&2; exit 1; }
PATH="$BOKS_lib:$BOKS_sbin:$BOKS_bin:$PATH"; export PATH


VERBOSE=false
if [ X"$1" = X-v ] ; then
    VERBOSE=true
    shift
fi
case "$1" in
    -b) BOOT=true ;;
    -k) KILL=true ;;
    -i) BOOT=true; INSTALLED=false ;;
esac

if $BOOT; then
    $VERBOSE && echo "cleaning out locks and tempfiles" >&2
    if [ -n "$BOKS_var" ]; then
	rm -f $BOKS_var/LCK/boks_init	
	find $BOKS_var/LCK -type f -exec rm -f {} \;
	> $BOKS_var/btmp
    fi
    test -n "$BOKS_tmp" && rm -f $BOKS_tmp/* 2>/dev/null
elif [ -f "$BOKS_var/LCK/boks_init" ]; then
    # unloading streams encryption module
    ID=`/usr/sbin/modinfo 2>/dev/null | awk '$6=="scrypt" { print $1; }'`
    if [ X"$ID" != X ]; then
	/usr/sbin/modunload -i "$ID" >/dev/null 2>&1
    fi
    $VERBOSE && echo "stopping boks_init..." >&2
    kill `cat $BOKS_var/LCK/boks_init`
    i=0
    while [ -f $BOKS_var/LCK/boks_init ]; do
	i=`expr $i + 1`
	sleep 1
	test $i -gt 10 && break
    done
    if $VERBOSE; then
	test -f $BOKS_var/LCK/boks_init && echo "boks_init still around?" >&2
	echo "killing any remaining processes..." >&2
    fi
    for f in $BOKS_var/LCK/*; do
	test -f $f && kill `cat $f` 2>/dev/null
    done > /dev/null 2>&1
    rm -f $BOKS_var/LCK/boks_init
    find $BOKS_var/LCK -type f -exec rm -f {} \;
fi
$VERBOSE && echo "removing message queues" >&2
boksdiag -f rmqueues $BOKS_etc/*_queue
if $KILL; then
    $VERBOSE && echo "removing shared memory and semaphores" >&2
    boksdiag -f rmsems 1984 $BOKS_etc/bridge_cache
    boksdiag -f rmshms 1984 1985 $BOKS_etc/bridge_cache
    $VERBOSE && echo "removing lockfiles" >&2
    test -n "$BOKS_var" && rm -f $BOKS_var/LCK/boks_init && \
        find $BOKS_var/LCK -type f -exec rm -f {} \;
    exit 0
fi

eval `grep '^BOKSINIT=' $BOKS_etc/ENV`
BOKSTYPE="${BOKSINIT:-standalone}"
BOKSINIT="${BOKSINIT:+-f $BOKS_etc/boksinit.$BOKSINIT}"

if [ $BOKSTYPE = master ]; then
    master=`showmaster`
    if [ X"$master" != X ]; then
        echo "a master is already installed at: $master" >&2
        exit 1
    fi
fi


if $INSTALLED; then
    if [ "$BOKSTYPE" = master -o "$BOKSTYPE" = standalone ]; then
	$VERBOSE && echo "look for existing db" >&2
	missing="`boksdiag -f db missing`"
	if [ -n "$missing" ]; then
	    echo
	    echo "**** BoKS: fatal error"
	    echo "Missing database files: $missing"
	    echo "Database should be restored before BoKS can start"
	    echo "Until then only root may log in"
	    echo
	    exit 1
	fi
    fi
fi

$VERBOSE && echo "modload of stream crypt module"
/usr/sbin/modload $BOKS_lib/scrypt

$VERBOSE && echo "starting boks_init..."
rm -f $BOKS_var/.init $BOKS_data/.notready
boks_init $BOKSINIT || exit 1
while [ -f $BOKS_data/.notready -o ! -f $BOKS_var/.init ]; do
    sleep 1
done
rm -f $BOKS_var/.init
$VERBOSE && echo "boks_init started" >&2

if $BOOT && $INSTALLED; then
    boksdiag -f clean_logout
fi
exit 0
