#!/bin/sh
#
# skeleton      Example file to build /etc/init.d scripts.
#
# Version:      @(#) /etc/init.d/skeleton 1.01 26-Oct-1993
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#
# Modified:     Henry M. Pierce <hmpierce@cs.cmu.edu> for *BSD only...

prefix=/usr/local
vicedir=
if [ -f ${prefix}/etc/coda/server.conf ] ; then
    . ${prefix}/etc/coda/server.conf
fi

if [ x${vicedir} = x ]; then
    vicedir=/vice
fi

if [ x${numservers} = x ]; then
    numservers=1
fi

VICE=${vicedir}
VICESRV=${VICE}/srv

OSTYPE=`uname -s`
if [ x$OSTYPE = xNetBSD ] ; then
  KILLALL="pkill"
else
  KILLALL=killall
fi

CODABASE=/usr/local
PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH
export PATH

AUTH2=${CODABASE}/sbin/auth2

if [ -x ${CODABASE}/sbin/kauth2 ] ; then
    AUTH2=${CODABASE}/sbin/kauth2
# should be set in the server.conf file.
    FLAGS="-realm CS.CMU.EDU"
fi

if [ ! -x ${AUTH2} \
	-o ! -x ${CODABASE}/sbin/updatesrv \
	-o ! -x ${CODABASE}/sbin/updateclnt \
	-o ! -x ${CODABASE}/sbin/codasrv \
	-o ! -x ${CODABASE}/sbin/startserver \
	-o ! -x ${CODABASE}/sbin/volutil ]; then
    echo $0: Incomplete Coda server installation.  Server start aborted.
    exit 1
fi

echon() {
    if [ "`echo -n`" ] ; then
        echo "$@"\c
    else
        echo -n "$@"
    fi
}

# See how we were called.
case "${1:-start}" in
  start)
    # start auth2 & update stuff: first the scm case
    echon "Starting Coda Services: "
    if [ "`cat ${VICE}/hostname`" = "`cat ${VICE}/db/scm`" ]; then
 	${AUTH2} ${FLAGS} &
	echon " auth2"
		
    else 	# for non-scm machines.
	${AUTH2} ${FLAGS} -chk &
	echon " auth2(ro)"
    fi

    # update srv and updatemon run on all servers
    if [ "`cat ${VICE}/hostname`" = "`cat ${VICE}/db/scm`" ]; then
            updatesrv &
            echon " updatesrv"
    fi

    updateclnt -h `cat ${VICE}/db/scm` &
    echon " updateclnt"

    echo '.'	# Done With Coda Serivces

    # now the server
    echon "Starting Coda:"
    if [ $numservers -eq 1 ]; then 
	if [  -f ${VICESRV}/STARTFROMBOOT -a ! -f ${VICESRV}/CRASH ]; then
	    rm -f $VICESRV/pid
	    startserver
	    echo " codasrv."
	else
	    echo "`date`: srv not started." >> ${VICESRV}/SrvErr
	    echo "missing STARTFROMBOOT or CRASH." >> ${VICESRV}/SrvErr
	fi
    else
	n=1
	while [ $n -le $numservers ]; do
	    VICESRV=${vicedir}/server_$n/srv
	    if [  -f ${VICESRV}/STARTFROMBOOT -a ! -f ${VICESRV}/CRASH ]; then
		rm -f $VICESRV/pid
		startserver -n $n 
		echon " codasrv(-n $n)"
	    else
		echo "`date`: srv not started." >> ${VICESRV}/SrvErr
		echo "missing STARTFROMBOOT or CRASH." >> ${VICESRV}/SrvErr
	    fi
	    n=`expr $n + 1`
	done
	echo "."
    fi
    ;;

  stop)
    # Normal Coda FS shutdown:
    # if pid file doesn't exist, it isn't likely that srv is running.
    echon "Shutting down Coda:"

    $KILLALL -9 auth2
    echon " auth2"
    $KILLALL -9 updateclnt
    echon " updateclnt"

    if [ "`cat ${VICE}/hostname`" = "`cat ${VICE}/db/scm`" ]; then
        $KILLALL -9 updatesrv
        echon " updatesrv"
    fi

    if [ -f ${VICESRV}/pid ]; then
	if [ $numservers -eq 1 ]; then 
	    volutil shutdown
	    echo " codasrv"
	    echon "$0: sleeping 30 secs to let srv shutdown..."
	else
	    n=1
	    while [ $n -le $numserver ] ; do
		if [ -f ${prefix}/etc/coda/server_$n.conf ] ; then
		    . ${prefix}/etc/coda/server_$n.conf
		fi
		volutil -h $hostname shutdown
		echo " codasrv ($hostname)"
		n=`expr $n + 1`
	    done
	    echon "$0: sleeping 30 secs to let the servers shutdown..."
	fi
	sleep 30
	echo "..done"
    fi
    echo '.'
    ;;

  *)
    echo "Usage: $0 {start|stop}"
    echo "       no parameters mean start"
    exit 1

esac

exit 0
