#!/bin/sh
#
# Copyright (c) 1993 by TransSys, Inc.
# All rights reserved.
#
# $Header: /local/SRCS/slip2/util/pnirun,v 1.16 1994/10/03 03:14:14 louie Exp $

# next few lines must match entry added to /etc/syslog.conf and 
# other configuration information
PNILOGFAC=local5
PNILOG=/usr/adm/pni.log
PNIDIR=/etc/pni

#----------------------------------------------------------------------------

runpni() {

    CFG="$1"
    PNIUNIT=
    
    
    if PNIUNIT=`expr $CFG : '.*/\(pni[0-9a-f]*\)\.*'` ; then
	PNITAG="-n ${PNIUNIT}"
    else
	echo Can\'t grok unit from configuration file name
	exit 1
    fi

    export CFG PNIDIR

    cd ${PNIDIR}
    if [ ! -f ${PNIDIR}/runtime/${PNIUNIT}.LOCK ]; then
	/etc/pni/pnid ${COREOPT+"-C"} -c ${PNITAG} -f ${CFG} ${debugopts}
    fi
}

#----------------------------------------------------------------------------

forever() {
    C="$1"
    #
    #  This is a bit of a kludge at the moment, as the retry mechanisms are
    #  not yet completely in place in the pnid process.  That's why we've got
    #  this loop here; should the pnid process exit, we wait 5 minutes and
    #  start it up again
    #
    while [ -f "${C}" ]; do
	runpni $*
        sleep 300
    done
}

#----------------------------------------------------------------------------

allopt=0
boot=0
debugopts=0
downopt=0

while [ $# != 0 ]
do
    case "$1" in
    -x|-v)
	set $1
	;;

    -all)
	allopt=1
	;;

    -boot)
	boot=1
	if [ -d ${PNIDIR}/runtime ]; then 
	    rm -f ${PNIDIR}/runtime/*
	fi

	if [ ! -f ${PNILOG} ]; then
	    touch ${PNILOG}
	else
	    mv ${PNILOG} ${PNILOG}.old
	    tail -100 ${PNILOG}.old > ${PNILOG}
	    chmod 644 ${PNILOG}
	fi
	kill -1 `cat /etc/syslog.pid`
	: if running from /etc/rc.local, then dup stderr to stdout
	exec 2>&1
	;;

    -core)
	COREOPT=yes
	;;

    -debug|--debug)
	debugopts="-t -d"
	;;

    -down|--down)
	if [ $boot != 0 -o $allopt != 0 ]; then
	    echo "Can't specify $1 with -boot or -all" 1>&2
	    exit 1
        fi
	downopt=1
	;;

    -*)
	echo "$0: Unrecognized option $1 ignored" 1>&2
	;;

    *)
	C="$1"
	if [ ! -f "${C}" -a -f "${PNIDIR}/config/${C}.config" ]; then
	    C="${PNIDIR}/config/${C}.config"
	fi
	CONFS="${CONFS-} $C"
	;;

    esac

    shift
done

if [ $allopt != 0 ]; then 
    debugopts=
    if [ -r ${PNIDIR}/config/autostart ]; then
	: configuration files are listed in the /etc/pni/autostart file
	CONFS=`cat ${PNIDIR}/config/autostart`

    elif [ -d ${PNIDIR}/config ]; then
	: conf files are all those in /etc/pni/config ending in .config-auto

	CONFS=`find ${PNIDIR}/config -depth -type d -prune -o \
			-type f -name '*.config-auto' -print`

	if [ $? -ne 0 ]; then
	    CONFS=
	fi
    else
	: Else, we do not know what is going on.  Imagine that..
	echo -n "No auto-start configs"
    fi
else
    if [ -z "${CONFS}" ]; then
	echo "You have not specified any PNI configurations to start"  1>&2
	exit 1
    fi
fi

for cfg in ${CONFS-blurfl}; do
    if [ $cfg != blurfl -a -f $cfg ]; then
	if [ $allopt != 0 ]; then
	    logger -p local5.info "Starting $cfg"
	    forever $cfg &
	    : pause a bit to reduce disk thrashing and kernel driver loading
	    : race condition
	    sleep 30
	else
	    if [ $downopt -eq 0 ]; then
		runpni $cfg
	    else
		${PNIDIR}/bin/pnistat -c down $cfg
	    fi
	fi
    fi
done

exit 0

