#!/bin/sh
#
# telnetd		Start up the Telnet server daemon
#

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

prog="telnetd"
lockfile=/var/lock/$prog

# Some functions to make the below more readable
TELNETD=/sbin/telnetd
PID_FILE=/var/run/telnetd.pid
OPTIONS=-d

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

start()
{
	[ -x $TELNETD ] || exit 5

	echo -n "Starting $prog"
	daemon $TELNETD $OPTIONS > /dev/null 
	touch $PID_FILE
	touch $lockfile
	echo 
}

stop()
{
	echo -n "Shutting down $prog: "
	killall $prog
	# if we are in halt or reboot runlevel kill all running sessions
	# so the TCP connections are closed cleanly
	if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
		trap '' TERM
		killall $prog 2>/dev/null
		trap TERM
	fi
	rm -rf $PID_FILE
	rm -rf $lockfile
	echo 
}

restart() {
	stop
	start
}

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