#!/bin/bash
#
### BEGIN INIT INFO
# Provides: hts
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Short-Description: httptunnel server daemon
# Description:  An HTTP tunneling server designed to tunnel other protocols,
#               such as SSH, over the HTTP protocol.
### END INIT INFO

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

# Source httptunnel server configuration.
if [ -f /etc/sysconfig/hts ] ; then
	. /etc/sysconfig/hts
else
	HTS_OPTS="-S -F localhost:22"
	HTS_SERVER="localhost"
	HTS_PORT="7777"
fi

test -x /usr/bin/hts || exit 0

RETVAL=0
prog="hts"

start() {
	# Check if hts is already running
	if [ ! -f /var/lock/subsys/hts ]; then
	    gprintf "Starting %s: " "$prog"
	    daemon --user nobody /usr/bin/hts -p /var/run/hts.pid \
					$HTS_OPTS $HTS_SERVER:$HTS_PORT
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/hts
	    echo
	fi
}

stop() {
	gprintf "Stopping %s: " "$prog"
	killproc /usr/bin/hts
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/hts
	echo
}

#
#	See how we were called.
#
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload|restart)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/hts ]; then
	    stop
	    start
	fi
	;;
  status)
	status /usr/bin/hts
	RETVAL=$?
	;;
  *)
	gprintf "Usage: %s {condrestart|start|stop|restart|reload|status}\n" "$0"
	exit 1
esac

exit $RETVAL
