#!/bin/sh
#
# stunnel	stunnel (Universal SSL tunnel)
#
# chkconfig:	345 14 91
#
# description:	stunnel (Universal SSL tunnel)
#
# id:		$Id$
#

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/stunnel ] && . /etc/sysconfig/stunnel

# 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 stunnel
		exit 1
	fi
else
	exit 0
fi

# Daemon doesn't die if config doesn't exist
if [ ! -f "$STUNNEL_CONFIG" ]; then
	nls "%s config %s does not exist." Stunnel "$STUNNEL_CONFIG"
	exit 1
fi

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/stunnel ]; then
		msg_starting stunnel; busy
		daemon stunnel $STUNNEL_CONFIG
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/stunnel
	else
		msg_already_running stunnel
	fi
}

stop() {
	if [ -f /var/lock/subsys/stunnel ]; then
		msg_stopping stunnel
		killproc stunnel
		rm -f /var/run/stunnel/stunnel.pid /var/lock/subsys/stunnel >/dev/null 2>&1
	else
		msg_not_running stunnel
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/stunnel ]; then
		stop
		start
	else
		msg_not_running stunnel
		RETVAL=$1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status stunnel
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
