#!/bin/sh 
#
# tdm2:     	Starts the 3ware daemon
#
# Author:       Michael Benz <linuxraid@lsi.com>
#
### BEGIN INIT INFO
# Required-Start:    $local_fs $syslog $network
# Required-Stop:     $local_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Provides:          3dm2
# Short-Description: Start and Stop 3ware Daemon
# Description:       Start the 3dm2 application which logs the current state
#                    of the 3ware RAID controller, then polls for state changes.
### END INIT INFO

# config: /etc/3dm2/3dm2.conf

# Source function library.
. /lib/lsb/init-functions

PROCNAME=3dm2
DAEMON=/usr/sbin/3dm2
PIDFILE=/var/lock/3dm2.pid
RETVAL=0

export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin"

[ -x $DAEMON ] || exit 0

set -e 

case "$1" in
  start)
        if [ ! -f $PIDFILE ]; then
		log_daemon_msg "Starting 3ware Daemon... "
		start-stop-daemon -S -q --make-pidfile -p $PIDFILE -x $DAEMON || RETVAL=1
		log_end_msg $RETVAL
        else
                echo "3dm2 must already be running. Found lock file $PIDFILE"
                echo "Try manuall running /etc/init.d/tdm2 restart"
	fi
	;;

  stop)
	log_daemon_msg "Stopping 3ware Daemon... "
	start-stop-daemon -K -q -s TERM -n $PROCNAME && rm -f $PIDFILE || RETVAL=1
	log_end_msg $RETVAL
	;;

  restart|force-reload)
	$0 stop
        sleep 1
	$0 start
	;;
  *)
	log_success_msg "Usage: $0 {start|stop|restart|force-reload}"
	exit 1
	;;
esac
exit $RETVAL
