#!/bin/bash

#
# Copyright (c) 2013 Mellanox Technologies. All rights reserved.
# Copyright (c) 2010 QLogic Corporation. 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.
#

### BEGIN INIT INFO
# Provides:       openibd
# Required-Start: $local_fs
# Required-Stop: opensmd
# X-Start-Before: networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:    Activates/Deactivates InfiniBand Driver to \
#                 start at boot time.
### END INIT INFO

MLNX_OFED_FUNCS_FILE="/usr/share/mlnx_ofed/mod_load_funcs"
if [ ! -r "$MLNX_OFED_FUNCS_FILE" ]; then
	echo "$0: Error: missing functions file $MLNX_OFED_FUNCS_FILE. Aborting"
	exit 1
fi
. "$MLNX_OFED_FUNCS_FILE"

global_init "$@"

RC=0
case "$ACTION" in
    start)
        do_start
        ;;
    stop)
        if [ $FORCE -eq 0 ]; then
            if [ "X${ALLOW_STOP}" != "Xyes" ]; then
                echo "ERROR: Option 'stop' is disabled!"
                log_msg "ERROR: Option 'stop' is disabled!"
                echo "Either use 'force-stop', or enable 'stop' by setting 'ALLOW_STOP=yes' in your ${CONFIG} file"
                exit 1
            fi
        fi
        do_stop
        ;;
    restart)
        do_stop
        do_start
        ;;
    status)
        status
        RC=$?
        ;;
    *)
        echo
        echo "Usage: `basename $0` {start|force-start|stop|force-stop|restart|force-restart|status}"
        echo
        RC=1
        ;;
esac

cleanup
exit $RC
