#!/bin/sh
### BEGIN INIT INFO
# Provides:          scached
# Required-Start:    $network $local_fs
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start scached daemon
# Description:       Start up scached, session caching daemon
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=scached
NAME=scached
DAEMON=/usr/sbin/scached
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
CONFIGSDIR=/etc/scache

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

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

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

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

#
# Function that starts the daemon/service
#
do_start() {
    
    if [ "$ENABLE_SCACHED" != "yes" ]; then
	log_end_msg "disabled in /etc/default/$NAME";
	return 2
    fi
    
    for _cfg in $CONFIGSDIR/*.conf; do
	log_progress_msg $(basename "$_cfg");
	$DAEMON --config="$_cfg" || return 2
    done
    log_progress_msg "";

    return 0;
}

#
# Function that stops the daemon/service
#
do_stop() {

    if [ "$ENABLE_SCACHED" != "yes" ]; then
	return 1
    fi
    
    for _cfg in $CONFIGSDIR/*.conf; do
	[ "$VERBOSE" != no ] || log_action_cont_msg $(basename "$_cfg");
	$DAEMON --config="$_cfg" --quit || return 2
    done

    return 0;
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] || log_action_begin_msg "Starting $DESC "
    do_start
    case "$?" in
		0|1) [ "$VERBOSE" != no ] || log_action_end_msg 0;;
		2) [ "$VERBOSE" != no ] || log_action_end_msg 1;;
	esac
  ;;
  stop)
	[ "$VERBOSE" != no ] || log_action_begin_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] || log_action_end_msg 0;;
		2) [ "$VERBOSE" != no ] || log_action_end_msg 1;;
	esac
	;;
  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  restart|force-reload)
	[ "$VERBOSE" != no ] || log_action_begin_msg "Restarting $DESC"
	do_stop
        sleep 1
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_action_end_msg 0;;
			1) log_action_end_msg 1;; # Old process is still running
			*) log_action_end_msg 1;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_action_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
