#!/bin/sh

# Bring up/down tftpd

. /etc/sysconfig/rc.conf
. /bin/network-functions

SETSID=/usr/bin/setsid
PIDFILE=/var/run/tftpd.pid
SERVICE=opentftpd
PROCESS_NAME=opentftpd
TFTP_PATH=/bin

do_start ()
{
	if ! ServiceCheckRunning1 ; then
		return $?
	fi

	if [ -e /usr/bin/$PROCESS_NAME ] ; then
		TFTP_PATH=/usr/bin
	fi

	/etc/init.d/softdogd wait
	$SETSID $TFTP_PATH/$PROCESS_NAME -i /etc/opentftpd.ini > /dev/null 2>&1 &

	ServiceCheckRunning2

	return $?
}

do_stop ()
{
	/etc/init.d/softdogd wait
	ServiceStop

	sleep 2
	/bin/killall -0 $PROCESS_NAME > /dev/null 2>&1
	if [ $? = 0 ] ; then
		/bin/killall $PROCESS_NAME
	fi

	sleep 1
	if [ $? = 0 ] ; then
		/bin/killall $PROCESS_NAME
	fi
	return $?
}

do_status ()
{
	ServiceStatus

	return $?
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	status)
		do_status
		;;
	restart)
		do_stop
		do_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit $?
