#!/bin/bash
#
# fcauthd       This shell script takes care of starting and stopping
#               fcauthd (Fibre Channel Authentication daemon).
#
# 
# RedHat config info
#
# chkconfig: 2345 20 80
# description: fcauthd is the FC Authentication daemon
#
# Suse config info
#
#
### BEGIN INIT INFO
# Provides:       fcauthd
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    fcauthd is the FC Authentication daemon
### END INIT INFO

RETVAL=0
prog="fcauthd"
progpath="/usr/sbin/hbanyware/$prog"

# Source function library.
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    GO=daemon
    STATUS=status
    SHOW_STATUS=""
else
    . /etc/rc.status
    rc_reset
    GO="start_daemon -f"
    STATUS=checkproc
    SHOW_STATUS="rc_status -v"
fi

start() {
    # Do nothing if driver isn't loaded
    lpfc_loaded=$(lsmod | awk '{ print $1 }' | grep "lpfc")
    if [ -z "$lpfc_loaded" ]; then
	return 0
    fi

    # Start daemon.
    echo -n $"Starting $prog: "
    $GO $progpath $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/fcauthd
    $SHOW_STATUS
    return $RETVAL
}

stop() {
    # Stop daemon
    if [ -f /var/lock/subsys/fcauthd ];then
        running=`ps -ef | grep fcauthd | grep -v grep | grep -v stop`
        if [ -n "$running" ];then
            echo -n $"Shutting down $prog"
            killproc "$prog"
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/fcauthd
            $SHOW_STATUS
        else
            rm -f /var/lock/subsys/fcauthd
        fi
    fi
    return $RETVAL
}

# See how we were called.
case "$1" in
start)
    running=`ps -ef | grep fcauthd | grep -v grep | grep -v start`
    if [ -f /var/lock/subsys/fcauthd ]; then
        if [ -z "$running" ];then
            start
        else
            echo "$prog already running"
        fi
    else
        start
    fi
    ;;
stop)
    stop
    ;;
status)
    $STATUS $prog
    $SHOW_STATUS
    ;;
reload)
    PID=`pidof -s $prog`
    if [ "$PID" != "" ]; then
        echo "Reloading configuration of $prog ($PID)"
        kill -HUP $PID
    else
        start
    fi
    $SHOW_STATUS
    ;;
restart)
    if [ -f /var/lock/subsys/fcauthd ]; then
        stop
    fi
    start
    RETVAL=$?
    ;;
condrestart)
    if [ -f /var/lock/subsys/fcauthd ]; then
        stop
        start
        RETVAL=$?
    fi
    ;;
*)
    echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
    exit 1
esac

exit $RETVAL
