#!/bin/sh
#
# httptunnel	httptunnel short service description
#
# chkconfig:	345  90 36
#
# description:	httptunnel long service description
#
# $Id$


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

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down httptunnel
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/httptunnel ]; then
		msg_starting httptunnel
		awk '/^[^#]/ { print $0; }' \
			< /etc/sysconfig/httptunnel \
			| while read LISTEN_PORT FWD_HOST_PORT; do
			daemon hts.respawn -F "$FWD_HOST_PORT" "$LISTEN_PORT"
		done
		# FIXME: this probably doesn't work
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/httptunnel
	else
		msg_already_running httptunnel
	fi
	;;
  stop)
	# Stop daemons.
	if [ -f /var/lock/subsys/httptunnel ]; then
		msg_stopping httptunnel
		killproc hts.respawn
		rm -f /var/lock/subsys/httptunnel
	else
		msg_not_running httptunnel
	fi
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  status)
	status httptunnel
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL

# This must be last line !
# vi:syntax=sh:tw=78:ts=8:sw=4
