#!/bin/sh
#
# tftpd		This shell script takes care of starting and stopping
#               TFTP server.
#
# chkconfig: - 90 25
# description: The tftp server serves files using the trivial file transfer \
#              protocol. The tftp protocol is often used to boot diskless \
#              workstations, download configuration files to network-aware printers, \
#              and to start the installation process for some operating systems.
# processname: in.tftpd
# config: /etc/sysconfig/tftpd
# pidfile: /var/run/tftpd.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Source TFTPD configuration.
SourceIfNotEmpty /etc/sysconfig/tftpd

PIDFILE=/var/run/tftpd.pid
LOCKFILE=/var/lock/subsys/tftpd
RETVAL=0

start()
{
	local secure proto create

	is_yes "$CREATE" && create="-c" || create=
	if is_no "$SECURE"; then
	    secure=
	else
	    secure="-s"
	    DIR="${DIR:-/var/lib/tftpboot}"
	fi
	if is_yes "$IPv6_ONLY"; then
	    proto="-6"
	elif is_yes "$IPv4_ONLY"; then
	    proto="-4"
	else
	    proto=
	fi

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --name tftpd --expect-user ${USER:-root} -- \
	    in.tftpd -P -l $secure $proto $create \
		${USER:+-u $USER} \
		${ADDRESS:+-a $ADDRESS} \
		${UMASK:+-U $UMASK} \
		${BLOCKSIZE:+-B $BLOCKSIZE} \
		$OPTS \
		"${DIR}"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- tftpd
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root -- tftpd
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
