#! /bin/sh
#
# Start/stop the Netatalk 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.
#

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

# netatalk.conf expects hostname in $HOSTNAME by default
HOSTNAME=`hostname`

. :ETCDIR:/netatalk.conf


#
# Start the appletalk server processes.
#

atalk_startup() {
	echo 'starting appletalk daemons: \c'
	if [ x"${ATALKD_RUN}" != x"no" ]; then
		if [ -x :SBINDIR:/atalkd ]; then
			:SBINDIR:/atalkd;		echo ' atalkd\c'
		fi

		if [ -x :BINDIR:/nbprgstr ]; then
			:BINDIR:/nbprgstr -p 4 "${ATALK_NAME}:Workstation${ATALK_ZONE}";
			:BINDIR:/nbprgstr -p 4 "${ATALK_NAME}:netatalk${ATALK_ZONE}";
							echo ' nbprgstr\c'
		fi

		if [ x"${PAPD_RUN}" = x"yes"  -a -x :SBINDIR:/papd ]; then
			:SBINDIR:/papd;			echo ' papd\c'
		fi

		if [ x"${TIMELORD_RUN}" = x"yes"  -a -x :SBINDIR:/timelord ]; then
			:SBINDIR:/timelord;		echo ' timelord\c'
		fi
	fi

	if [ x"${CNID_METAD_RUN}" = x"yes" -a -x :SBINDIR:/cnid_metad ]; then
		:SBINDIR:/cnid_metad;			echo ' cnid_metad\c'
	fi

	if [  x"${AFPD_RUN}" = x"yes" -a -x :SBINDIR:/afpd ]; then
		:SBINDIR:/afpd  ${AFPD_UAMLIST} -g ${AFPD_GUEST} \
               -c ${AFPD_MAX_CLIENTS} -n "${ATALK_NAME}${ATALK_ZONE}";	echo ' afpd\c'
	fi

	echo '.'
}


case "$1" in

'start')
        if [ x"${ATALK_BGROUND}" = x"yes" ]; then
            echo "Starting netatalk in the background ... "
            atalk_startup > /dev/null &
        else
            atalk_startup
        fi
        ;;

#
# Stop the appletalk server processes.
#
'stop')

	echo 'stopping appletalk daemons:\c'

	if [ -x :SBINDIR:/papd ]; then
		killproc papd;			echo ' papd\c'
	fi

	if [ -x :SBINDIR:/afpd ]; then
		killproc afpd;			echo ' afpd\c'
	fi

	if [ -x :SBINDIR:/cnid_metad ]; then
		killproc cnid_met;		echo ' cnid_metad\c'
	fi

	if [ -x :SBINDIR:/timelord ]; then
		killproc timelord;		echo ' timelord\c'
	fi

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

	echo '.'
	;;

#
# Usage statement.
#

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