#! /bin/sh
#=================================================================
# Begin mdadm
#
# Description:	Start mdadm daemon
# chkconfig:	2345 30 60
#
#=================================================================

### BEGIN INIT INFO
# Provides:             $mdadm
# Should-Stop:
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description: 	Start and stop the MD software RAID monitor
# Description:		The mdmonitor service checks the status of all software
#			RAID arrays on the system.  In the event that any of the arrays
#			transition into a degraded state, it notifies the system
#			administrator.  Other options are available, see the mdadm.conf
#			and mdadm man pages for possible ways to configure this service.
### END INIT INFO

PIDFILE=/var/run/mdadm/mdadm.pid
PATH=/sbin:/usr/sbin:$PATH
RETVAL=0
OPTIONS="--monitor --scan -f --pid-file=$PIDFILE"
prog=mdmonitor

. /lib/lsb/init-functions

case "$1" in
  start)
# (Re)start mdmon to take over monitoring of mdmon started from the initrd
  for i in /dev/md/*.pid ;
    do
    if [ -r $i ];
      then
      origprog="$prog"; prog="/sbin/mdmon -a"
      log_info_msg "Starting $prog...."
      start_daemon -f $prog
      evaluate_retval
      prog="$origprog"
      fi
    done
# Make sure configuration file exists and has information we can use
# MAILADDR or PROGRAM or both must be set in order to run mdadm --monitor
    if [ -f /etc/mdadm.conf ] ;
      then    
      grep '^\(MAILADDR\|PROGRAM\) .' /etc/mdadm.conf >/dev/null 2>&1 || exit 6
      if [ -f "$PIDFILE" ]; then
	pidofproc $PIDFILE && return 0
        fi
      log_info_msg "Starting $prog...."
      start_daemon -f mdadm ${OPTIONS}
      ret=$?
      if [ $ret -eq "0" ] ; then
	touch /var/lock/subsys/$prog
        fi
      evaluate_retval
      fi
    ;;
# Make sure configuration file exists and has information we can use

    stop)
      if [ -f /var/lock/subsys/$prog ]; then
        log_info_msg "Stopping $prod daemon..."
        killproc -p $PID_FILE $prg
        evaluate_retval
        fi
      ;;

    restart)
      $0 stop
      sleep 1
      $0 start
      ;;

    reload)
      exit 3
      ;;

    condrestart)
      pid=`cat $PID_FILE 2>/dev/null`
      if [ -n "${pid}"  -a -d /proc/$pid ]; then
        $0 restart
	fi
      ;;

    *)
      echo "Usage: $0 {start|stop|restart|reload|condrestart}"
      exit 1
      ;;
  esac

