#! /bin/sh
#
# Start/stop the AppleTalk daemons.
#
# AppleTalk daemons. Make sure not to start atalkd in the background:
# its data structures must have time to stablize before running the
# other processes.
#

ATALK_BIN=:BINDIR:
ATALK_CONF_DIR=:ETCDIR:
ATALK_SBIN=:SBINDIR:

ATALK_START_ATALKD=0
ATALK_START_NBPRGSTR=0
ATALK_START_PAPD=0
ATALK_START_CNID_METAD=1
ATALK_START_AFPD=1
ATALK_START_TIMELORD=0

#
# kill the named process(es)
#
killproc() {
	pid=`/usr/bin/ps -e |
	     /usr/bin/grep $1 |
	     /usr/bin/grep -v grep |
	     /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	[ "$pid" != "" ] && kill $pid
}

case "$1" in

#
# Start the appletalk server processes.
#

'start')

	echo 'starting appletalk daemons: \c'
	if [ ${ATALK_START_ATALKD} -eq 1 -a -x ${ATALK_SBIN}/atalkd ]; then
		${ATALK_SBIN}/atalkd;		echo ' atalkd\c'
	fi

	if [ ${ATALK_START_NBPRGSTR} -eq 1 -a -x ${ATALK_BIN}/nbprgstr ]; then
		${ATALK_BIN}/nbprgstr -p 4 `hostname|sed 's/\..*$//'`:Workstation
		${ATALK_BIN}/nbprgstr -p 4 `hostname|sed 's/\..*$//'`:netatalk
						echo ' nbprgstr\c'
	fi

	if [ ${ATALK_START_PAPD} -eq 1 -a -x ${ATALK_SBIN}/papd ]; then
		${ATALK_SBIN}/papd;			echo ' papd\c'
	fi

	if [ ${ATALK_START_CNID_METAD} -eq 1 -a -x ${ATALK_SBIN}/cnid_metad ]; then
		${ATALK_SBIN}/cnid_metad;			echo ' cnid_metad\c'
	fi

	if [ ${ATALK_START_AFPD} -eq 1 -a -x ${ATALK_SBIN}/afpd ]; then
		${ATALK_SBIN}/afpd;			echo ' afpd\c'
	fi

	if [ ${ATALK_START_TIMELORD} -eq 1 -a -x ${ATALK_SBIN}/timelord ]; then
		${ATALK_SBIN}/timelord;		echo ' timelord\c'
	fi

	echo '.'

	;;

#
# Stop the appletalk server processes.
#

'stop')

	echo 'stopping appletalk daemons:\c'

	if [ -x ${ATALK_SBIN}/papd ]; then
		killproc papd;			echo ' papd\c'
	fi

	if [ -x ${ATALK_SBIN}/afpd ]; then
		killproc afpd;			echo ' afpd\c'
	fi

	if [ -x ${ATALK_SBIN}/cnid_metad ]; then
		killproc cnid_metad;			echo ' cnid_metad\c'
	fi

	if [ -x ${ATALK_SBIN}/timelord ]; then
		killproc timelord;		echo ' timelord\c'
	fi

	# kill atalkd last, since without it the plumbing goes away.
	if [ -x ${ATALK_SBIN}/atalkd ]; then
		killproc atalkd;		echo ' atalkd\c'
	fi

	echo '.'
	;;

#
# Usage statement.
#

*)
	echo "usage: $0 {start|stop}"
	exit 1
	;;
esac
