#! /bin/sh
### BEGIN INIT INFO
# Provides:          cipux-rpcdr
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: SSL wrapper for XML-RPC server for CipUX.
# Description:       This stunnel4 start script provides an SSL wrapper for 
#                    the XML-RPC server for CipUX. You can plug in external
#                    applications to execute generic CipUX commands. You 
#                    should use this SSL wrapper to access the XML-RPC server 
#                    over a network connection.
### END INIT INFO

# Author: Jonas Smedegaard <dr@jones.dk>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="CipUX XML-RPC daemon"
NAME=cipux-rpcdr
DAEMON=/usr/sbin/cipux_rpcdr
DAEMON_ARGS=""
PIDFILE="/var/run/$NAME.pid"
SCRIPTNAME="/etc/init.d/$NAME"
NOSTARTFILE="/etc/cipux/${NAME}_not_to_be_run"
STUNNEL=/usr/bin/stunnel4
STUNNELFILE=/etc/cipux/stunnel-cert.conf

[ -x "$DAEMON" ] || exit 0

[ -r "/etc/default/$NAME" ] && . "/etc/default/$NAME"

. /lib/init/vars.sh

. /lib/lsb/init-functions

if [ ! -x "$DAEMON" ]; then
	log_error_msg "Could not find Binary $STUNNEL"
	exit 5
fi

check_for_no_start() {
	[ ! -e "$NOSTARTFILE" ] || return 1
}

check_config() {
	$DAEMON -t > /dev/null || return 1
}

do_start()
{
	start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" --test > /dev/null \
		|| return 1
	# FIXME: is --background and --make-pidfile needed? (not in skeleton)
	start-stop-daemon --background --make-pidfile --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" -- \
		$DAEMON_ARGS \
		|| return 2
	# FIXME: is sleep and pidfile update needed (not in skeleton)?
	sleep 1
        pid="$(pidof "$STUNNEL" "$STUNNELFILE")"
	if [ -z "$pid" ]; then 
	    echo ""
	else 
            if [ "$pid" -ne "$(cat "$PIDFILE")" ]; then
		     echo "$pid" > "$PIDFILE"
            fi
	fi
}

do_stop()
{
	# FIXME: Do we not trust existing pidfile?!?
        pid="$(pidof "$STUNNEL" "$STUNNELFILE")"
        if [ "$pid" -ne "$(cat "$PIDFILE")" ]; then
	        echo "$pid" > "$PIDFILE"
        fi
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile "$PIDFILE"
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	sleep 5
	# FIXME: is pidfile removal needed (suggested in skeleton)?
	#rm -f "$PIDFILE"
	return "$RETVAL"
}

do_reload() {
	start-stop-daemon --stop --signal 1 --quiet --pidfile "$PIDFILE" --exec "$DAEMON"
	# FIXME: is sleep needed (not in skeleton)?
	sleep 5
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	check_for_no_start
	case "$?" in
	  0)
		do_start
		case "$?" in
			0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
			2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
		esac
		;;
	  *)
		[ "$VERBOSE" != no ] && log_warning_msg "$DESC disabled ($NOSTARTFILE found)"
		[ "$VERBOSE" != no ] && log_end_msg 0
		;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  reload|force-reload)
	log_daemon_msg "Reloading $DESC" "$NAME"
	check_for_no_start
	case "$?" in
	  0)
		check_config
		case "$?" in
		  0)
			do_reload
			log_end_msg $?
			;;
		  *)
			log_warning_msg "configuration test failed for $NAME"
			log_end_msg 1
			;;
		esac
		;;
	  *)
		log_warning_msg "$DESC disabled ($NOSTARTFILE found)"
		log_end_msg 0
		;;
	esac
	;;
  restart)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		check_for_no_start
		case "$?" in
		  0)
			do_start
			case "$?" in
				0) log_end_msg 0 ;;
				1) log_end_msg 1 ;; # Old process is still running
				*) log_end_msg 1 ;; # Failed to start
			esac
			;;
		  *)
			log_warning_msg "$DESC stopped only ($NOSTARTFILE found)"
			log_end_msg 0
			;;
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	exit 3
	;;
esac

:
