#! /bin/sh
# chkconfig: - 91 9
# description: Atalk enables Linux to talk to Macintosh
#	       computers via the AppleTalk networking protocol
#	       and provides printer, file sharing, and AppleTalk
#	       routing services

### BEGIN INIT INFO
# Provides: atalk
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start:
# Default-Stop:
# Description: Atalk enables Linux to talk to Macintosh
#	       computers via the AppleTalk networking protocol
# Short-Description: start and stop atalk
### END INIT INFO

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

ATALK_CONF=/etc/netatalk/netatalk.conf
# test for and read in netatalk configuration
[ -f ${ATALK_CONF} ] || exit 0
. ${ATALK_CONF}

ATALKD=/usr/sbin/atalkd
AFPD=/usr/sbin/afpd
PAPD=/usr/sbin/papd
CNID_METAD=/usr/sbin/cnid_metad
NBPREG=/usr/bin/nbprgstr
NBPUNREG=/usr/bin/nbpunrgstr
LOCKFILE=/var/lock/subsys/atalk

[ -x ${ATALKD} -a -x ${AFPD} -a -x ${PAPD} -a -x ${CNID_METAD} -a -x ${NBPREG} -a -x ${NBPUNREG} ] || exit 0

# Quickly probe for appletalk and warn if we can't find it
if [ x"${ATALKD_RUN}" = x"yes" ]; then
	if [ ! -f /proc/net/appletalk ]; then
	    /sbin/modprobe appletalk || echo "[could not load appletalk module]"
	fi
fi

# Check for IP Encapsulation support
#/sbin/modprobe ipddp || echo "[could not load IP encapsulation]"

# initialize return values
RETVAL=1
RETVAL_ATALKD=0
RETVAL_PAPD=0
RETVAL_CNID_METAD=0
RETVAL_AFPD=0

# startup code for everything
atalk_startup() {
    if [ x"${ATALKD_RUN}" = x"yes" ]; then
	start_daemon ${ATALKD}
	RETVAL_ATALKD=$?

	action "Registering ${ATALK_NAME}:Workstation${ATALK_ZONE}:" ${NBPREG} -p 4 \"${ATALK_NAME}:Workstation${ATALK_ZONE}\"
	action "Registering ${ATALK_NAME}:netatalk${ATALK_ZONE}:" ${NBPREG} -p 4 \"${ATALK_NAME}:netatalk${ATALK_ZONE}\"

	if [ x"${PAPD_RUN}" = x"yes" ]; then
	    start_daemon ${PAPD}
	    RETVAL_PAPD=$?
	fi
    fi

    if [ x"${AFPD_RUN}" = x"yes" ] ; then
	    start_daemon ${AFPD} ${AFPD_UAMLIST} -g ${AFPD_GUEST} \
               -c ${AFPD_MAX_CLIENTS} #-n \"${ATALK_NAME}${ATALK_ZONE}\"
	    RETVAL_AFPD=$?
    fi

    if [ x"${CNID_METAD_RUN}" = x"yes" ] ; then
	    start_daemon ${CNID_METAD}
	    RETVAL_CNID_METAD=$?
    fi

    if [ $RETVAL_ATALKD -eq 0 -a $RETVAL_PAPD -eq 0 -a $RETVAL_AFPD -eq 0 -a $RETVAL_CNID_METAD -eq 0 ]; then
        RETVAL=0
	touch ${LOCKFILE} || RETVAL=1
    fi
}

case "$1" in
'start')
	echo -n 'Starting AppleTalk services: '
	if [ x"${ATALK_BGROUND}" = x"yes" ]; then 
	    echo -n "(backgrounded)"
	    atalk_startup >& /dev/null &
	    RETVAL=0
	else
	    echo
	    atalk_startup
	fi
	echo 
	;;
'stop')
	echo 'Shutting down AppleTalk services: '
	if [ x"${ATALKD_RUN}" = x"yes" ]; then
	    if [ x"${PAPD_RUN}" = x"yes" ]; then
		stop_daemon ${PAPD}
		RETVAL_PAPD=$?
	    fi

	    action "Unregistering ${ATALK_NAME}:Workstation${ATALK_ZONE}:" ${NBPUNREG} \"${ATALK_NAME}:Workstation${ATALK_ZONE}\"
	    action "Unregistering ${ATALK_NAME}:netatalk${ATALK_ZONE}:" ${NBPUNREG} \"${ATALK_NAME}:netatalk${ATALK_ZONE}\"

	    # kill atalkd last, since without it the plumbing goes away.
	    stop_daemon ${ATALKD}
	    RETVAL_ATALKD=$?
	fi

	# kill this separately as we also do AFP/tcp
	if [ x"${AFPD_RUN}" = x"yes" ]; then
	    stop_daemon ${AFPD}
	    RETVAL_AFPD=$?
	fi

	if [ x"${CNID_METAD_RUN}" = x"yes" ]; then
	    stop_daemon ${CNID_METAD}
	    RETVAL_CNID_METAD=$?
	fi

	if [ $RETVAL_ATALKD -eq 0 -a $RETVAL_PAPD -eq 0 -a $RETVAL_AFPD -eq 0 -a $RETVAL_CNID_METAD -eq 0 ] ; then
	    RETVAL=0
	    rm -f ${LOCKFILE} || RETVAL=1
	fi
	echo ""
	;;
'restart'|'reload')
	$0 stop
	$0 start
	RETVAL=$?
	;;
'condstop')
	if [ -e "$LOCKFILE" ]; then
		$0 stop
	fi
	;;
'condrestart'|'condreload')
	if [ -e "$LOCKFILE" ]; then
		$0 restart
	fi
	;;
'status')
	status atalkd
	RETVAL=$?
	;;
*)
	echo "Usage: atalk {start|stop|restart|condstop|condrestart|status}"
	exit 1
esac

exit $RETVAL
