#! /bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 4Suite.org repository server
# Description:       4Suite.org repositiry server daemon start script
### END INIT INFO

# Author: Raphael Bossek <bossekr@debian.org>

# Do NOT "set -e"

PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="4Suite.org repository server"
NAME=4ssd
DAEMON=/usr/bin/4ss_manager
DAEMON_ARGS=
PIDFILE=/var/run/4Suite/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Set default settings for 4ssd.
DAEMON_STARTOPTIONS="start"
DAEMON_RESTARTOPTIONS="restart"
DAEMON_STOPOPTIONS="stop"
DAEMON_USER="ftss"
FTSS_CORE_ID="Core"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Update environment with current settings.
export FTSS_USERNAME
export FTSS_PASSWORD
export FTSS_CORE_ID
[ -n "$FTSS_USERNAME" -a -n "$FTSS_PASSWORD" -a -n "$FTSS_CORE_ID" ] && \
export FTSS_ENVIRONMENT=${FTSS_ENVIRONMENT:-"('$FTSS_USERNAME','$FTSS_PASSWORD','$FTSS_CORE_ID')"}

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Check if given pid and the coresponding application are up.
#
pidactive()
{
	if [ -s "$1" ]; then
		grep -q "$2" "/proc/"`cat "$1"`"/cmdline" \
		&& return 0
	fi
	return 1
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	if pidactive "$PIDFILE" "$NAME"; then
		retval=1
	elif [ \( -n "$FTSS_USERNAME" -a -n "$FTSS_PASSWORD" \) -o "$DAEMON_STARTOPTIONS" != "start" ]; then
		mkdir -p "$PIDDIR"
		chown -R "$DAEMON_USER:" "$PIDDIR"
		mkdir -p "$LOGDIR"
		chown -R "$DAEMON_USER:" "$LOGDIR"
		su "$DAEMON_USER" -c "$DAEMON $DAEMON_STARTOPTIONS >/dev/null 2>&1"
		local -i timeout=10
		while [ $timeout -gt 0 ] && ! pidactive "$PIDFILE" "$NAME"; do
			((timeout--))
			echo -n ". "
			sleep 1
		done
		pidactive "$PIDFILE" "$NAME" \
		&& retval=0 \
		|| retval=2
	else
		retval=2
	fi
	return $retval
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	if [ ! -s "$PIDFILE" ]; then
		retval=1
	elif [ \( -n "$FTSS_USERNAME" -a -n "$FTSS_PASSWORD" \) -o "$DAEMON_STOPOPTIONS" != "stop" ]; then
		su "$DAEMON_USER" -c "$DAEMON $DAEMON_STOPOPTIONS >/dev/null 2>&1"
		local -i timeout=10
		while [ $timeout -gt 0 ] && pidactive "$PIDFILE" "$NAME"; do
			((timeout--))
			sleep 1
		done
		pidactive "$PIDFILE" "$NAME" \
		&& retval=2 \
		|| retval=0
	fi
	return "$retval"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	if pidactive "$NAME" "$PIDFILE"; then
		su "$DAEMON_USER" -c "$DAEMON $DAEMON_RESTARTOPTIONS >/dev/null 2>&1"
	fi
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	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)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	log_daemon_msg "Reloading $DESC" "$NAME"
	do_reload
	log_end_msg $?
	;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		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
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

:
