#!/bin/sh
# inet		Start TCP/IP networking services. This script is wrapper for
#		any kind inetd daemon like inetd, rlinetd and others.
#
# Author:	Tomasz Koczko <kloczek@pld.org.pl>
#
# chkconfig:	345 50 50
# description:	The internet superserver daemon starts a variety of other \
#		internet services as needed. It is responsible for starting \
#		many services, including telnet, ftp, rsh, and rlogin. \
#		Disabling inetd disables all of the services it is responsible for.

. /etc/rc.d/init.d/functions

AUTO_CONFIG=yes

# sanity check for /etc/sysconfig/rc-inetd.conf
# Note: defaults must also be read for each service, because they will be replaced
#	by config of each service processed.
if [ -f /etc/sysconfig/rc-inetd.conf ]; then
	. /etc/sysconfig/rc-inetd.conf
else
	nls "Error: %s not found" /etc/sysconfig/rc-inetd.conf
	nls " Inet server can't be run."
	exit 6
fi

# sanity check for /etc/sysconfig/rc-inet.script
if [ -f /etc/sysconfig/rc-inet.script ]; then
	. /etc/sysconfig/rc-inet.script
else
	nls "Error: %s not found" /etc/sysconfig/rc-inet.script
	nls " Inet server can't be run."
	exit 5
fi

SERVICES=$(ls -d /etc/sysconfig/rc-inetd/* 2>/dev/null | egrep -v '.*(\.rpm(save|new|orig)|~|CVS)')

gen_config_file()
{
	# initialize empty config file
	rm -f $CONFIG_FILE
	CURRENT_UMASK=`umask`
	if [ ${CONFIG_FILE_UMASK}x != x ]; then
		umask $CONFIG_FILE_UMASK
	fi

	echo $PREAMBLE >$CONFIG_FILE
	echo >> $CONFIG_FILE
	umask $CURRENT_UMASK

	DID_PARSE=0
	for i in $SERVICES; do
		# unset everything
		unset SERVICE_NAME SOCK_TYPE PROTOCOL PORT USER
		unset DAEMON DAEMONARGS MAX_CONNECTIONS GROUP NICE
		unset FAMILY INTERFACE CHROOT RPCNAME RPCVERSION
		unset INITGROUPS BANNER ECHO FILTER ENV FLAGS
		unset SERVER MAX_CONNECTIONS_PER_SOURCE
		unset CONNECTIONS_PER_SECOND RPCNUMBER
		unset BANNER_SUCCESS BANNER_FAILURE PASSENV
		unset SERVICE_TYPE ACCESS_TIMES LOG_TYPE
		unset LOG_SUCCESS LOG_FAILURE REDIRECT MAX_LOAD
		# Read defaults...
		. /etc/sysconfig/rc-inetd.conf
		# ...and then config of *this* service.
		. $i
		CURRENT_SERVICE=`basename $i`
		DONT_PARSE=0
		# check if service is in deny list ?
		for i in $DENY_SERVICES ; do
			if [ $i = $CURRENT_SERVICE ] ; then
				DONT_PARSE=1
			fi
		done

		if [ $DONT_PARSE -eq 0 ] ; then
			DID_PARSE=1
			parse_one_service >> $CONFIG_FILE
		fi
	done
	# unset everything
	unset SERVICE_NAME SOCK_TYPE PROTOCOL PORT USER
	unset DAEMON DAEMONARGS MAX_CONNECTIONS GROUP NICE
	unset FAMILY INTERFACE CHROOT RPCNAME RPCVERSION
	unset INITGROUPS BANNER ECHO FILTER ENV FLAGS
	unset SERVER MAX_CONNECTIONS_PER_SOURCE
	unset CONNECTIONS_PER_SECOND RPCNUMBER
	unset BANNER_SUCCESS BANNER_FAILURE PASSENV
	unset SERVICE_TYPE ACCESS_TIMES LOG_TYPE
	unset LOG_SUCCESS LOG_FAILURE REDIRECT MAX_LOAD

	return $DID_PARSE
}

pre_start_services()
{
	DID_PARSE=0
	for i in $SERVICES; do
		pre_start_service()
		{
			:
		}
		. $i
		CURRENT_SERVICE=`basename $i`
		DONT_PARSE=0
		# check if service is in deny list ?
		for i in $DENY_SERVICES ; do
			if [ $i = $CURRENT_SERVICE ] ; then
				DONT_PARSE=1
			fi
		done

		if [ $DONT_PARSE -eq 0 ] ; then
#			if [ $( set | grep "^pre_start_service\(\)" ) ] ; then
				pre_start_service
#			fi
		fi
	done
}

pre_stop_services()
{
	DID_PARSE=0
	for i in $SERVICES; do
		pre_stop_service ()
		{
			:
		}
		. $i
		CURRENT_SERVICE=`basename $i`
		DONT_PARSE=0
		# check if service is in deny list ?
		for i in $DENY_SERVICES ; do
			if [ $i = $CURRENT_SERVICE ] ; then
				DONT_PARSE=1
			fi
		done

		if [ $DONT_PARSE -eq 0 ] ; then
#			if [ $(set | grep "^pre_stop_service" ) ] ; then
			pre_stop_service
#			fi
		fi
	done
}

start() {
	if [ -f /var/lock/subsys/rc-inetd ]; then
		msg_already_running Inet
	fi

	if [ "$AUTO_CONFIG" != "no" ]; then
		show "$(nls "Generating %s for %s" "$CONFIG_FILE" "$PROCESS_NAME")"
		busy
		pre_start_services
		gen_config_file
		DIDPARSE=$?
		deltext;ok
	else
		DIDPARSE=1
	fi

	if [ "$DIDPARSE" -gt 0 ]; then
		msg_starting "$PROCESS_NAME"
		daemon $INETDAEMON $INETDAEMON_ARGS
		touch /var/lock/subsys/rc-inetd
	fi
}

stop() {
	if [ -f /var/lock/subsys/rc-inetd ]; then
		msg_stopping "$PROCESS_NAME"
		killproc $INETDAEMON
		pre_stop_services
		rm -f $PID_FILE
		rm -f /var/lock/subsys/rc-inetd
	else
		msg_not_running Inet
	fi
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  reload|force-reload)
	if [ ! -f /var/lock/subsys/rc-inetd ]; then
		msg_not_running Inet >&2
		exit 7
	fi

	if [ "$AUTO_CONFIG" != "no" ]; then
		show "$(nls "Generating %s for %s" "$CONFIG_FILE" "$PROCESS_NAME")"
		busy
		gen_config_file
		deltext; ok
	fi

	show "$(nls "Reload %s service configuration" "$PROCESS_NAME")"
	busy
	reload_config
	deltext; ok
	;;
  status)
	status rc-inetd $(basename $INETDAEMON)
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit 0
