#!/bin/sh
#
# Copyright(c) 2011 Ritesh Raj Sarraf
# Copyright(c) 2010 Intel Corporation. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Derived from the Upstream Script

### BEGIN INIT INFO
# Provides: fcoe-utils
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs sendsigs
# Default-Start: S
# Default-Stop: 0 1 6
# Short-Description: Starts and Stops the Open-FCoE Initiator
# Description: Open FCoE Initiator
### END INIT INFO

CONFIG_DIR=/etc/fcoe
PID_FILE="/var/run/fcoemon.pid"
LOG_FILE="/var/log/fcoemon.log"
FCOEMON=/usr/sbin/fcoemon
FCOEADM=/usr/sbin/fcoeadm
FCOEMON_OPTS=

. /lib/lsb/init-functions


. $CONFIG_DIR/config

if [ "$USE_SYSLOG" = "yes" ] || [ "$USE_SYSLOG" = "YES" ]; then
    FCOEMON_OPTS="$FCOEMON_OPTS --syslog"
fi

if [ "$DEBUG" = "yes" ] || [ "$DEBUG" = "YES" ]; then
    FCOEMON_OPTS="$FCOEMON_OPTS --debug"
fi

test -x $FCOEADM || {
	if [ "$1" = "stop" ]; then exit 0;
	else
		log_failure_msg "fcoeadm tool not installed."
		exit 1;
	fi
}

test -x $FCOEMON || {
	echo "$FCOEMON not installed";
	if [ "$1" = "stop" ]; then exit 0;
	else
		log_failure_msg "fcoemon tool not installed."
		exit 1;
	fi
}

start()
{
	log_daemon_msg "Starting FCoE initiator service"

	modprobe -q libfc 2>/dev/null || :
	modprobe -q fcoe 2>/dev/null || :

	start-stop-daemon --start --quiet --pidfile $PID_FILE --exec ${FCOEMON} -- ${FCOEMON_OPTS}

	touch /var/lock/fcoe
	log_end_msg 0;
}

stop()
{
	local force=$1

	pid=$(pidof "$FCOEMON")
	if [ "$force" = "force" ]
	then
		log_warning_msg "Destroying any active fcoe interface/s"
		[ "$pid" ] && kill -HUP $pid
		log_end_msg 0;
	else
		[ "$pid" ] && kill -TERM $pid
	fi

	log_daemon_msg "Stopping FCoE initiator service"

	rm -f /var/run/fcoemon.*
	rm -f /tmp/fcoemon.dcbd.*
	rm -f /var/lock/fcoe
	log_end_msg 0;
}

status()
{
	status=0
	pidof $FCOEMON
	if [ $? -eq 0 ]; then
		log_daemon_msg "$FCOEMON -- RUNNING, pid=`cat $PID_FILE`"
	else
		log_daemon_msg "$FCOEMON -- UNUSED"
		status=3
	fi
	log_end_msg 0;

	interfaces=`$FCOEADM -i 2>&1 | \
		    awk '/Symbolic Name:/{print $6}' | \
		    sort | awk '{printf("%s ", $1)}'`

	if [ -z "$interfaces" ]; then
		log_daemon_msg "No interfaces created."
	else
		log_daemon_msg "Created interfaces: $interfaces"
		status=0
	fi
	if [ -f /var/lock/fcoe -a $status -eq 3 ]; then
		status=2
	fi
	if [ -f /var/run/fcoe.pid -a $status -eq 3 ]; then
		status=1
	fi
	return $status
}

case "$1" in
	start)
		start
		;;

	stop)
		stop $2
		;;

	restart)
		stop $2
		start
		;;

	force-reload)
		stop force
		start
		;;

	status)
		status
		exit $?
		;;
	condrestart|try-restart)
		status || exit 0
		$0 restart
		;;
	*)
		echo -n "Usage: $0 {start|stop [force]|status|restart [force]|"
		echo "force-reload|condrestart|try-restart}"
		exit 1
		;;
esac
