#! /bin/sh
#
# Kiwi-ltsp-aoe  service setup management
#
# Copyright (c) 2009 IntelliAnt, Kolkata, India
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Author: Shrenik Bhura, 2009
#
# /etc/init.d/kla
#   and its symbolic link
# /usr/sbin/rckla
#
# Version       Date            Changes
# 0.1           2009-02-28      Initial release
#
#################################################################################
### BEGIN INIT INFO
# Provides:          kla
# Required-Start:    $network
# Required-Stop:     $null
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: kla, providing kiwi-ltsp-aoe services
# Description:       The kla is a script where you can control
#	the serving of custom and prebuilt kiwi-ltsp images in
#	a granular manner.  We want it to be active in runlevels 3
#	and 5, as these are the runlevels with the network
#	available.
### END INIT INFO

# Check for missing binaries
KLA_BIN=/usr/sbin/kiwi-ltsp-aoe-setup
test -x $KLA_BIN || { echo "$KLA_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Check for existence of needed config file and read it
KL_CONFIG=/etc/sysconfig/kiwi-ltsp
test -r $KL_CONFIG || { echo "$KL_CONFIG missing, please check if /
kiwi-ltsp is installed properly."
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config
. $KL_CONFIG

if [[ -z "`echo $IMAGETYPE | grep AOE`" ]] ; then
        echo "KIWI-LTSP is configured for non-AoE setup."
        exit 6
fi

# Load the rc.status script for this service.
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
        echo -n "Starting kiwi-ltsp with AoE (kla)"
        ## Start daemon with startproc(8). If this fails
        ## the return value is set appropriately by startproc.
	if ! checkproc $KLA_BIN; then
         	startproc $KLA_BIN start || true
        else
               	rc_reset
	fi
        # Remember status and be verbose
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down kiwi-ltsp with AoE (kla)"
        ## Stop daemon with killproc(8) and if this fails
        ## killproc sets the return value according to LSB.
	#$KLA_BIN stop
        killproc -g -TERM $KLA_BIN && $KLA_BIN stop > /dev/null 2>&1
        # Remember status and be verbose
        rc_status -v
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
		sleep 1 # wait for vblade shutdown 
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    status)
        echo -n "Checking for kiwi-ltsp with AoE (kla)"
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.
        # Return value is slightly different for the status command:
        # 0 - service up and running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running (unused)
        # 4 - service status unknown :-(
        # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
        # NOTE: checkproc returns LSB compliant status values.
        checkproc $KLA_BIN
        # NOTE: rc_status knows that we called this init script with
        # "status" option and adapts its messages accordingly.
        rc_status -v
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac

rc_exit
