#!/bin/bash
# 
# mdadm	This starts, stops, and reloads the mdadm-based
#		software RAID monitoring and management facility
#
# chkconfig: 2345 98 01
# description: software RAID monitoring and management
# config: /etc/mdadm.conf
#

# Copyright 2002 Red Hat, Inc.

PATH=/sbin:/usr/sbin:$PATH
RETVAL=0

prog=mdadm

# Source function library.
. /etc/rc.d/init.d/functions

# 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
[ -f /etc/mdadm.conf ] || exit 0
grep '^\(MAILADDR\|PROGRAM\) .' /etc/mdadm.conf >/dev/null 2>&1 || exit 0


usage ()
{
    gprintf "Usage: %s {start|stop|status|restart|condrestart}\n" "$prog"
    RETVAL=1
}


start ()
{
    [ -e /var/lock/subsys/$prog ] && return 0
    [ -f /var/run/mdadm.pid ] && [ -d /proc/$(cat /var/run/mdadm.pid) ] && return 0

    ulimit -S -c 0 >/dev/null 2>&1
    gprintf "Starting %s monitoring: " "$prog"
    pid=`mdadm --monitor --scan --daemonise`
    if [ -n "$pid" ] ; then
	echo "$pid" > /var/run/mdadm.pid
	touch /var/lock/subsys/$prog
	success "mdadm"
	RETVAL=0
    else
	failure "mdadm"
	RETVAL=1
    fi
    echo
    return $RETVAL
}

stop ()
{
    gprintf "Stopping %s monitoring: " "$prog"
    killproc mdadm
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/run/mdadm.pid
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
    echo
    return $RETVAL
}

restart ()
{
    stop
    start
}

condrestart ()
{
    [ -e /var/lock/subsys/$prog ] && restart
}


case "$1" in
    start) start ;;
    stop) stop ;;
    status) status mdadm ;;
    restart|reload) restart ;;
    condrestart) condrestart ;;
    *) usage ;;
esac

exit $RETVAL
