#!/sbin/runscript
#
# Copyright (C) 2008 Mark Buechler <mark.buechler@gmail.com>
# Copyright (C) 2009 Bart Van Assche <bvanassche@acm.org>
# Copyright (C) 2010 Scott Bowe <scottb@sentania.net>
# This software is made available under the GPLv2 license.
#
# System startup script for the SCST core functionality.
#
# Customized for gentoo linux by Scott Bowe.
#
# 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 !

opts="${opts} try-restart reload force-reload"
depend() {
	use logger
}

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin

DEFAULTFILE="/etc/conf.d/scst"
SCST_CFG=/etc/scst.conf
MODPROBE="/sbin/modprobe"
RMMOD="/sbin/rmmod"

if [ -f $DEFAULTFILE ]; then
  . $DEFAULTFILE
fi

# 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"

# 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
#

checkconfig() {
	if [ ! -f $SCST_CFG ] ; then
		eerror "Please create $SCST_CFG"
		return 1
	fi
	return 0
}

checkinstall() {
	if [ ! -x `which scstadmin` ] ; then
		eerror "scstadmin is not intalled"
		return 1
	fi
	return 0
}

start() {
        ebegin "Loading and configuring the mid-level SCSI target SCST"
       	eindent
	checkconfig
	checkinstall || return $?
	ret=0
        for module in ${SCST_MODULES}; do
        	einfo "Loading SCST module ${module}..."
        	if ! modprobe "${module}"; then
                	eerror "${module} failed to load"
			eend $? "Failed to load SCST"
			return $?
		fi
        done
	einfo "Configuring SCST..."
	eindent
	if [ -f $SCST_CFG ]; then
                if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
        	        einfo "SCST config was loaded"
	        else
		        eend $? "SCST config could not be loaded"
		        return $?
                fi
        else
                einfo "SCST configuration file $SCST_CFG missing, skipping"
                return 0
        fi
        eoutdent
        eoutdent
        einfo "SCST loaded!!!"
        eend $?
}

stop() {
        ## Stop the service.
        ebegin "Stopping the mid-level SCSI target SCST"
		eindent
        reverse_list=""
        for module in ${SCST_MODULES}; do
        	reverse_list="${module} ${reverse_list}"
        done
        for module in ${reverse_list}; do
        	einfo "Unloading ${module}"
        	if [ -e "/sys/module/${module}" ] && ! rmmod "${module}"; then
            		eindent
			eend $? "Failed to unload ${module}"
			return $?
		fi
        done
        eoutdent
        einfo "SCST unloaded!!!"
        eend $?
}

restart() {
        ## Stop and restart the service if the service is already running,
        ## otherwise start the service.
        stop
        sleep 3
        start
}

try-restart() {
        ## Restart the service if the service is already running.
        status >/dev/null 2>&1 && restart
}

reload() {
        ## Cause the configuration of the service to be reloaded without
        ## actually stopping and restarting the service.
        einfo "Reloading SCST configuration"
        eindent
        if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
        	ret=0
        	ebegin "SCST config reloaded"
	else
		eend $? "SCST config could not be releaded"
		exit 1
        fi
}

force-reload() {
        ## Cause the configuration to be reloaded if the service supports this,
        ## otherwise restart the service if it is running.
        einfo "Reloading SCST configuration"
        eindent
        if scstadmin -config $SCST_CFG >/dev/null 2>&1; then
        	ret=0
        	ebegin "SCST config reloaded"
        else
        	restart
        fi
}

status() {
        ## Print the current status of the service.
        einfo "SCST status: "
	eindent

        for module in ${SCST_MODULES}; do
		if [ ! -e "/sys/module/${module}" ]; then
                	einfo "${module} not loaded"
			ret=3
			eend $ret
			return $ret
		fi
        done
        ret=0
        ebegin "SCST modules loaded OK"
        eend $ret
}
