#!/bin/bash
#
# opensm:		Manage OpenSM
#
# chkconfig: - 09 91
# description:  Manage OpenSM
#
### BEGIN INIT INFO
# Provides: opensmd
# Required-Start: $syslog $openibd
# Required-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:  Manage OpenSM
### END INIT INFO
#
# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
# Copyright 2006 PathScale, Inc.  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.

prefix=/usr
exec_prefix=/usr

. /lib/lsb/init-functions

CONFIG=/etc/default/opensm
if [[ -s $CONFIG ]]; then
    . $CONFIG
fi

start () {
    echo -n "Starting opensm: "
    /usr/sbin/opensm --daemon $OPTIONS > /dev/null
    if [[ $RETVAL -eq 0 ]]; then
	[ ! -d /var/lock/subsys ] && mkdir /var/lock/subsys
        touch /var/lock/subsys/opensm
        log_success_msg
    else
        log_failure_msg
    fi
    echo
}

stop () {
    echo -n "Shutting down opensm: "
    killall opensm
    if [[ $RETVAL -eq 0 ]]; then
        rm -f /var/lock/subsys/opensm
        log_success_msg
    else
        log_failure_msg
    fi
    echo
}

Xstatus () {
	pid="`pidof opensm`"
	ret=$?
	if [ $ret -eq 0 ] ; then
		echo "OpenSM is running... pid=$pid"
	else
		echo "OpenSM is not running."
	fi
}

restart() {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
        Xstatus
	;;
    restart | force-reload | reload)
	restart
	;;
    try-restart | condrestart)
	[ -e /var/lock/subsys/opensm ] && restart
	;;
    resweep)
	killall -HUP opensm
        RETVAL=$?
	;;
    rotatelog)
	killall -USR1 opensm
        RETVAL=$?
	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|reload|condrestart|resweep|rotatelog}"
	RETVAL=1
	;;
esac

exit
