#!/bin/sh -e
#
#    raid: notify raid events, failures and syncing
#    Copyright (C) 2010 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that 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, see <http://www.gnu.org/licenses/>.

[ -r /proc/mdstat ] || exit 0

PKG="byobu"
color 2>/dev/null || color() { true; }

case $1 in
	--detail)
		cat /proc/mdstat
		exit 0
	;;
esac

if grep -qs " blocks .*\[.*_.*\]$" /proc/mdstat; then
	# Errors in your raid
	msg="RAID"
fi

if grep -qs \% /proc/mdstat; then
	p=$(grep -m1 \% /proc/mdstat | sed -e "s/\%.*/%/" -e "s/.* //")
	[ -z "$msg" ] && msg="RAID"
	msg="$msg,$p"
fi

if [ -n "$msg" ]; then
	printf "$(color B w r)%s$(color -) " "$msg"
fi
