#!/bin/sh

[ -e /etc/rc.defaults ] && . /etc/rc.defaults
[ -e /etc/rc.conf ] && . /etc/rc.conf

DAEMON=inetd

case "$1" in
	start)
		if [ ! -f "/var/run/$DAEMON.pid" ]
		then
			echo "Starting $DAEMON"
				INETD_CONF=$inetd_config
				SSH_HOST_KEY=$ssh_host_key

			# Check for existence of config file
			if [ ! -f $INETD_CONF ]
			then
				echo "$INETD_CONF not found!"
				echo "Launching configuration menu"
				/usr/bin/cfg_inetd
			fi
	
			# Check for existence of dropbear key
			# Generate if not found and sshd enabled
	
			if grep ssh $INETD_CONF | grep -v "^#" >/dev/null \
				&& [ ! -e $SSH_HOST_KEY ]
			then
				echo "You have enabled dropbear in $INETD_CONF"
				echo "but there is no SSH host key yet."
				/usr/bin/dropbearkey -t rsa -f $SSH_HOST_KEY
			fi		
			# Check for existence of samba password
			# Generate if not found and smbd enabled

			if grep netbios-ssn $INETD_CONF | grep -v "^#" >/dev/null \
				&& [ ! -e /etc/smbpasswd ]
			then
				echo "Generating default smbpasswd"
				echo "You are advised to change this with smbpasswd"
				(echo uClinux ; echo uClinux) | /usr/bin/smbpasswd -a root -s
			fi
			# Start Daemon
			$DAEMON $INETD_CONF &
			# Don't need to create pid as inetd does this for us
			# Check Daemon launched OK
			if ps -fp$! > /dev/null 2>&1
			then
				echo "  >> OK"
			else
				echo "  ** FAILED"
			fi
		else
			# Daemon already running, show status
			$0 status
			exit 1
		fi
		;;
	stop)
		# Make sure we don't nuke this script 
		# in the process of stopping daemon.
		# killall will kill this script as well
		# as it shares the same ps name
		if [ ! -f "/var/run/$DAEMON.pid" ]
                then                                   
			# Daemon not running, show status
                        $0 status
			exit 1
		fi
		echo "Stopping $DAEMON"
		# kill daemon
		kill `cat /var/run/$DAEMON.pid`
		# Remove pid
		rm /var/run/$DAEMON.pid
		echo "  >> OK"
		;;
	restart)
		# If daemon not running, start
		# Otherwise start and stop daemon
		# Ugly hack as ps id's change when using
		# $0 start and $0 stop

		if [ ! -f "/var/run/$DAEMON.pid" ]
		then
			echo "$DAEMON not running, starting"
			exec /etc/rc.d/$DAEMON start
		else
			echo "Stopping $DAEMON"
			# kill daemon
			kill `cat /var/run/$DAEMON.pid`
			# Remove pid
			rm /var/run/$DAEMON.pid
			echo "  >> OK"
			exec /etc/rc.d/$DAEMON start
		fi
		;;

	status)
		
		if [ ! -f "/var/run/$DAEMON.pid" ]
		then
			echo "$DAEMON is not running or pidfile does not exist"
		else
			DAEMONPID=`cat /var/run/$DAEMON.pid`
			echo "$DAEMON is running with pid $DAEMONPID"
		fi
		;;
	*)
		echo "Usage: $0 <start|stop|restart|status>"
		exit 1
		;;
esac
# vim:ts=4:sw=4:ic:ai:nows:
