#!/bin/sh
#
# Copyright (C) 2008 Mark Buechler <mark.buechler@gmail.com>
# Copyright (C) 2009 Bart Van Assche <bart.vanassche@gmail.com>
# This software is made available under the GPLv2 license.
#
# System startup script for the SCST core functionality.
#
# Note: on most Linux distributions /bin/sh is a soft link to /bin/bash, while
# on a default Ubuntu setup /bin/sh is a soft link to /bin/dash !
#
# See also:
# * http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
#
### BEGIN INIT INFO
# Provides:       scst
# Required-Start: $syslog $local_fs
# Required-Stop:  $syslog $local_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    SCST core
### END INIT INFO
### BEGIN CHKCONFIG INFO
# chkconfig:      2345 13 87
# description:    SCST core
### END CHKCONFIG IFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin
SCST_CFG=/etc/scst.conf

# Modules to load/unload.
#
# !!! DON'T ADD HERE TARGET DRIVERS, WHICH IMMEDIATELLY START ACCEPTING
# !!! NEW CONNECTIONS, BECAUSE AT THIS POINT ACCESS CONTROL HASN'T CONFIGURED
# !!! YET!
#
#SCST_MODULES="scst scst_disk scst_vdisk"
SCST_MODULES="scst qla2x00tgt scst_vdisk"

# Return values according 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
#

[ -x "$(which scstadmin)" ] || exit 5
[ -f $SCST_CFG ]            || exit 6

. /lib/lsb/init-functions

case "$1" in
    start)
        ## Start the service.
        echo -n "Loading and configuring the mid-level SCSI target SCST"

        for module in ${SCST_MODULES}; do
            if ! modprobe "${module}"; then
                log_failure_msg
                exit 5
            fi
        done

        if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
            log_success_msg
        else
            log_failure_msg
            exit 1
        fi
        ;;
    stop)
        ## Stop the service.
        echo -n "Stopping the mid-level SCSI target SCST"

        reverse_list=""
        for module in ${SCST_MODULES}; do
            reverse_list="${module} ${reverse_list}"
        done
        for module in ${reverse_list}; do
            if [ -e "/sys/module/${module}" ] && ! rmmod "${module}"; then
                log_failure_msg FAILED
                exit 1
            fi
        done

        log_success_msg
        ;;
    restart)
        ## Stop and restart the service if the service is already running,
        ## otherwise start the service.
        $0 stop
        $0 start
        ;;
    try-restart)
        ## Restart the service if the service is already running.
        $0 status >/dev/null 2>&1 && $0 restart
        ;;
    reload)
        ## Cause the configuration of the service to be reloaded without
        ## actually stopping and restarting the service.
        echo -n "Reloading SCST configuration"
        if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
            log_success_msg
        else
            log_failure_msg
            exit 1
        fi
        ;;
    force-reload)
        ## Cause the configuration to be reloaded if the service supports this,
        ## otherwise restart the service if it is running.
        echo -n "Reloading SCST configuration"
        if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
            log_success_msg
        else
            $0 restart
        fi
        ;;
    status)
        ## Print the current status of the service.
        echo -n "SCST status: "

        # Status has a slightly different meaning for the status command:
        # 0 - service running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running

        for module in ${SCST_MODULES}; do
            if [ ! -e "/sys/module/${module}" ]; then
                echo "Not loaded"
                exit 3
            fi
        done
        echo "OK"
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 2
        ;;
esac

exit 0
