#! /bin/sh
# chkconfig: 345 91 35
# description: This package enables Linux to talk to Macintosh
#              computers via the AppleTalk networking protocol and 
#              provides printer, file sharing, and AppleTalk routing 
#              services.   
#
# 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=/usr/hddapp/bin
ATALK_CONF_DIR=/etc/netatalk
ATALK_SBIN=/usr/hddapp/sbin
RETVAL_CNID_METAD=0
RETVAL_PAPD=0

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

test -x ${ATALK_SBIN}/atalkd || exit 0

test -f ${ATALK_CONF_DIR}/netatalk.conf || exit 0

# read in netatalk configuration
. ${ATALK_CONF_DIR}/netatalk.conf

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

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


# startup code for everything
atalk_startup() {
    if [ x"${ATALKD_RUN}" != x"no" ]; then 
	echo -n "  Starting atalkd:"
	${ATALK_SBIN}/atalkd
	RETVAL_ATALKD=$?
	echo

	if [ -x ${ATALK_BIN}/nbprgstr ]; then	
	    echo -n "  Registering ${ATALK_NAME}:Workstation${ATALK_ZONE}: "
	    ${ATALK_BIN}/nbprgstr -p 4 "${ATALK_NAME}:Workstation${ATALK_ZONE}"
	    RETVAL=$?
	    echo
	    echo -n "  Registering ${ATALK_NAME}:netatalk${ATALK_ZONE}: "
	    ${ATALK_BIN}/nbprgstr -p 4 "${ATALK_NAME}:netatalk${ATALK_ZONE}"
	    RETVAL=$?
	    echo
	fi	

        if [ $RETVAL_ATALKD -eq 0 ]; then
	    touch /var/lock/subsys/atalkd
        fi
  
        if [ x"${PAPD_RUN}" = x"yes"  -a -x ${ATALK_SBIN}/papd ]; then
            echo -n "  Starting papd:"
            daemon ${ATALK_SBIN}/papd
            RETVAL_PAPD=$?
            if [ $RETVAL_PAPD -eq 0 ]; then
                 touch /var/lock/subsys/papd
            fi
        fi

        # check for timelord in bin directory
        if [ x"${TIMELORD_RUN}" = x"yes"  -a -x ${ATALK_BIN}/timelord ]; then
            echo -n "  Starting timelord"
            daemon ${ATALK_BIN}/timelord
            echo
        fi

        # check for timelord in sbin directory
        if [ x"${TIMELORD_RUN}" = x"yes"  -a -x ${ATALK_SBIN}/timelord ]; then
            echo -n "  Starting timelord"
            daemon ${ATALK_SBIN}/timelord
            echo
        fi

        # check for a2boot in bin directory
        if [ x"${A2BOOT_RUN}" = x"yes"  -a -x ${ATALK_BIN}/a2boot ]; then
            echo -n "  Starting a2boot"
            daemon ${ATALK_BIN}/a2boot
            echo
        fi

        # check for a2boot in sbin directory
        if [ x"${A2BOOT_RUN}" = x"yes"  -a -x ${ATALK_SBIN}/a2boot ]; then
            echo -n "  Starting a2boot"
            daemon ${ATALK_SBIN}/a2boot
            echo
        fi


    fi

    if [ -x ${ATALK_SBIN}/cnid_metad ] ; then
            echo -n "  Starting cnid_metad:"
            ${ATALK_SBIN}/cnid_metad
            RETVAL_CNID_METAD=$?
          if [ $RETVAL_CNID_METAD -eq 0 ]; then
            touch /var/lock/subsys/cnid_metad
          fi

    fi


    if [ x"${AFPD_RUN}" = x"yes" -a -x ${ATALK_SBIN}/afpd -a -f /var/lock/subsys/afpd ] ; then
	    echo -n "  Starting afpd:"
	    ${ATALK_SBIN}/afpd ${AFPD_UAMLIST} -g ${AFPD_GUEST} \
               -c ${AFPD_MAX_CLIENTS} -n \"${ATALK_NAME}${ATALK_ZONE}\"
	    RETVAL_AFPD=$?
        if [ $RETVAL_AFPD -eq 0 ]; then
	    touch /var/lock/subsys/afpd
	else
	    rm -f /var/lock/subsys/afpd
        fi
    fi

    if [ $RETVAL_ATALKD -eq 0 -a $RETVAL_PAPD -eq 0 -a $RETVAL_CNID_METAD -eq 0 -a $RETVAL_AFPD -eq 0 ]; then
        RETVAL=0
    else 
        RETVAL=1
    fi
}

afp_startup() {
        if [ -x ${ATALK_SBIN}/cnid_metad ] ; then
	    echo -n "  Starting cnid_metad:"
	    ${ATALK_SBIN}/cnid_metad
            RETVAL_CNID_METAD=$?
 	    if [ $RETVAL_CNID_METAD -eq 0 ]; then
	        touch /var/lock/subsys/cnid_metad
	    fi
	fi
        if [ x"${AFPD_RUN}" = x"yes" -a -x ${ATALK_SBIN}/afpd ] ; then
	    echo -n "  Starting afpd:"
	    ${ATALK_SBIN}/afpd ${AFPD_UAMLIST} -g ${AFPD_GUEST} \
               -c ${AFPD_MAX_CLIENTS} -n \"${ATALK_NAME}${ATALK_ZONE}\"
	    RETVAL_AFPD=$?
	    echo
        fi
	
        if [ $RETVAL_ATALKD -eq 0 ]; then
            touch /var/lock/subsys/atalkd
        fi
        if [ $RETVAL_AFPD -eq 0 ]; then
            RETVAL=0
	    touch /var/lock/subsys/afpd || RETVAL=1
        fi
}

afpd_stop() {
	echo 'Shutting down AppleTalk services: '
	# kill this separately as we also do AFP/tcp
	if [ -x ${ATALK_SBIN}/cnid_metad ] ; then
	    echo -n "  Stopping cnid_metad:"
	    killall -9 cnid_metad
	    RETVAL_CNID_METAD=$?
	    rm -f /var/lock/subsys/cnid_metad
	fi
								
	if [ x"${AFPD_RUN}" = x"yes" -a -x ${ATALK_SBIN}/afpd ]; then
	    echo -n "  Stopping afpd:"
	    killall -9 afpd
	    RETVAL_AFPD=$?
	    rm -f /var/lock/subsys/afpd
	    echo
	fi
	
	if [ $RETVAL_AFPD -eq 0 ] ; then
	    RETVAL=0
	else
	    RETVAL=1
	fi
}

afpd_start() {
	echo -n 'Starting AppleTalk services: '
	if [ x"${ATALK_BGROUND}" = x"yes" ]; then 
	    echo -n "(backgrounded)"
	    afp_startup 1>/dev/null 2>/dev/null &
	else
	    echo
	    afp_startup
	fi

}
case "$1" in
'start')
	echo -n 'Starting AppleTalk services: '
	if [ x"${ATALK_BGROUND}" = x"yes" ]; then 
	    echo -n "(backgrounded)"
	    atalk_startup > /dev/null &
	else
	    echo
	    atalk_startup
	fi

	touch /var/lock/subsys/atalk

	echo 
	;;
'stop')
	echo 'Shutting down AppleTalk services: '
	if [ x"${ATALKD_RUN}" != x"no" ]; then
	    echo -n "  Unregistering ${ATALK_NAME}:Workstation${ATALK_ZONE}: "
	    ${ATALK_BIN}/nbpunrgstr "${ATALK_NAME}:Workstation${ATALK_ZONE}"
	    echo -n "  Unregistering ${ATALK_NAME}:netatalk${ATALK_ZONE}: "
	    ${ATALK_BIN}/nbpunrgstr "${ATALK_NAME}:netatalk${ATALK_ZONE}"

	    # kill atalkd last, since without it the plumbing goes away.
	    if [ -x ${ATALK_SBIN}/atalkd ]; then
		echo -n "  Stopping atalk:"
		killall -9 atalkd
		RETVAL_ATALKD=$?
		rm -f /var/lock/subsys/atalkd
		echo
	    fi
	fi

	# kill this separately as we also do AFP/tcp
	if [ x"${AFPD_RUN}" = x"yes" -a -x ${ATALK_SBIN}/afpd ]; then
	    if [ -f /var/lock/subsys/afpd ] ; then
	    	echo -n "  Stopping afpd:"
	    	killall -9 afpd 
	    	RETVAL_AFPD=$?
	    	rm -f /var/lock/subsys/afpd
	    fi
	    echo
	fi

        if [ -x ${ATALK_SBIN}/cnid_metad ]; then
            echo -n "  Stopping cnid_metad:"
            killall -9 cnid_metad
            RETVAL_CNID_METAD=$?
            rm -f /var/lock/subsys/cnid_metad
            echo
        fi


	if [ $RETVAL_ATALKD -eq 0 -a $RETVAL_PAPD -eq 0 -a $RETVAL_AFPD -eq 0 ] ; then
	    RETVAL=0
	else
	    RETVAL=1
	fi

	rm -f /var/lock/subsys/atalk

	echo ""
	;;
'afpd_stop')
	afpd_stop
	echo ""
	;;
'afpd_start')
	afpd_start
	RETVAL=$?
	echo ""
	;;
'afpd_restart')
	afpd_stop
	sleep 1
	afpd_start
	RETVAL=$?
	echo ""
	;;
'restart'|'reload')
	$0 stop
	$0 start
	RETVAL=0
	;;
*)
	echo "Usage: atalk {start|stop|restart|afpd_start|afpd_stop|afpd_restart}"
	exit 1
esac

exit $RETVAL
