#!/bin/bash
#
# Bring up/down ipoibha service
#
# chkconfig: - 18 82
# description: Activates/Deactivates InfiniBand ipoibha service
#
### BEGIN INIT INFO
# Provides:       ipoibha
### END INIT INFO
#
# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#
#  $Id: openib-1.1-ipoibha.init,v 1.2 2006/10/18 19:58:57 dledford Exp $
#

# config: /etc/sysconfig/openib.conf
CONFIG="/etc/ofed/openib.conf"
NETWORK_CONF_DIR=/etc/sysconfig/network-scripts

if [ ! -f $CONFIG ]; then
    exit 0
fi

. $CONFIG

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

if [ "X${IPOIBHA_ENABLE}" -ne "Xyes" ]; then
    exit 0
fi    

interfaces=`/sbin/ifconfig | grep "^ib[0-9]*" | cut -f 1 -d ' ' | sed -e 'y/\r/ /'`
if [ -z "$interfaces" ] && exit 0

start()
{
    ipoib_ha_log="/var/log/ipoib_ha.log"
    if [ ! -z "${PRIMARY_IPOIB_DEV}" ] && [ ! -z "${SECONDARY_IPOIB_DEV}" ]; then
        ipoib_ha.pl -p ${PRIMARY_IPOIB_DEV} -s ${SECONDARY_IPOIB_DEV} --with-arping --with-multicast -v > ${ipoib_ha_log} 2>&1 &
    else
        ipoib_ha.pl --with-arping --with-multicast -v > ${ipoib_ha_log} 2>&1 &
    fi
    ipoib_ha_pid=$!
    echo ${ipoib_ha_pid} > /var/run/ipoibha.pid
    touch /var/lock/subsys/ipoibha
    return 0   
}

stop()
{
    killproc ipoibha
    rm -f /var/lock/subsys/ipoibha
    rm -f /var/run/ipoibha.pid
    return 0
}

case $1 in
	start)
                action $"Loading IPoIB High Availability support: " start
		;;
	stop)
		action $"Stopping IPoIB High Availability support: " stop    
		;;
	restart)
		action $"Stopping IPoIB High Availability support: " stop    
                action $"Loading IPoIB High Availability support: " start
		;;
	status)
		status -p /var/run/ipoibha.pid ipoibha
		;;
	*)
		echo 
		echo "Usage: `basename $0` {start|stop|restart|status}"
		echo
		exit 1
		;;
esac

RC=$?
exit $RC
