#!/bin/bash
#
# srpd		Bring up/down the SRP client daemon
#
# chkconfig: - 24 76
# description: Starts/Stops InfiniBand SRP client service
# config:	/etc/srp_daemon.conf
#
### BEGIN INIT INFO
# Provides:       srpd
# Default-Stop: 0 1 2 3 4 5 6
# Required-Start: rdma
# Required-Stop: rdma
# Should-Start: $network NetworkManager opensm
# Should-Stop: $network MetworkManager opensm
# Short-Description: Starts and stops the InfiniBand SRP client service
# Description: The InfiniBand SRP client service attaches to SRP devices
#	on the InfiniBand fabric and makes them appear as local disks to
#	to the system.  This service starts the client daemon that's
#	responsible for initiating and maintaining the connections to
#	remote devices.
### END INIT INFO

RDMA_CONFIG=/etc/rdma/rdma.conf
pidfile=/var/run/srp_daemon.sh.pid
subsys=/var/lock/subsys/srpd
prog=/usr/sbin/srp_daemon.sh

. /etc/rc.d/init.d/functions

SRP_LOADED=no
if [ -f $RDMA_CONFIG ]; then
    . $RDMA_CONFIG
    if [ "${SRP_LOAD}" == "yes" ]; then
	SRP_LOADED=yes
    fi
fi

start()
{
    if [ "$SRP_LOADED" == "no" ]; then
    	echo "SRP kernel services not configured, unable to start SRP daemon"
	return 6
    fi

    echo -n "Starting SRP daemon service:"
    srp_daemon -o -e 2>&1 1>/dev/null

    daemon $prog >/dev/null 2>&1 &
    sleep 1
    checkpid `cat $pidfile`
    RC=$?
    [ $RC -eq 0 ] && (touch $subsys; echo_success) || echo_failure
    echo
    return $RC
}

stop()
{
    echo -n "Stopping SRP daemon service:"

    killproc -p $pidfile -d 2 srp_daemon.sh
    RC=$?
    rm -f $subsys
    echo
    return $RC
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

restart ()
{
    stop
    start
}

case "$1" in
    start)
        rh_status_q && exit 0
        start
    ;;
    stop)
        rh_status_q || exit 0
        stop
    ;;
    restart)
        restart
    ;;
    condrestart | try-restart)
        rh_status_q || exit 0
        restart
    ;;
    force-reload)
        restart
    ;;
    status)
        rh_status
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|try-restart|force-reload|status}"
        exit 2
esac

exit $?
