#!/bin/sh
#
# Copyright (C) 2008 Mark Buechler <mark.buechler@gmail.com>
# Copyright (C) 2009 Bart Van Assche <bvanassche@acm.org>
# This software is made available under the GPLv2 license.
#
# System startup script for the QLogic 22xx/23xx card target driver.
#
# 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:       qla2x00t
# Required-Start: $syslog $local_fs scst
# Required-Stop:  $syslog $local_fs scst
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    QLogic 22xx/23xx card target driver
### END INIT INFO
### BEGIN CHKCONFIG INFO
# chkconfig:      2345 14 86
# description:    QLogic 22xx/23xx card target driver
### END CHKCONFIG IFO

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

# Modules to load/unload.
KERNEL_MODULES="qla2x00tgt"

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

. /lib/lsb/init-functions

case "$1" in
    start)
        ## Start the service.
        echo -n "Loading and configuring the QLogic target driver"

        for module in ${KERNEL_MODULES}; do
            if ! modprobe "${module}"; then
                log_failure_msg
                exit 5
            fi
        done
        ;;
    stop)
        ## Stop the service.
        echo -n "Stopping the QLogic target driver"

        reverse_list=""
        for module in ${KERNEL_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 && $0 restart
        ;;
    reload)
        ## Cause the configuration of the service to be reloaded without
        ## actually stopping and restarting the service.
        echo -n "Reloading QLogic target driver configuration"
        log_success_msg
        ;;
    force-reload)
        ## Cause the configuration to be reloaded if the service supports this,
        ## otherwise restart the service if it is running.
        echo -n "Reloading QLogic target driver configuration"
        log_success_msg
        ;;
    status)
        ## Print the current status of the service.
        echo -n "QLogic target driver 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 ${KERNEL_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
