#!/bin/bash
# Copyrights (c) 2009-2012 Shoretel, Inc.
# set -x

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Function definitions
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function verbose() {
	logger "$*"
       	echo $*
}

# Set variables that reflect setting of the environment stored in EEPROM
function param() {
        if [ -e /etc/ubootenv/$1 ] ; then 
                export $1=`cat "/etc/ubootenv/$1"`
        else
                export "$1"="$2"
        fi
}
verbose "Starting add failed disk scprit" 
param disk1_status ""
param disk1_status1 ""
param disk1_status2 ""
param disk1_status3 ""
param disk2_status ""
param disk2_status1 ""
param disk2_status2 ""
param disk2_status3 ""

if [ -n "$(cat /proc/mdstat | grep recovery)" ] ; then
verbose "recovery already in progress. exiting"
exit 0
fi

if [ "$disk1_status1" == "" ] ; then
mdadm -r /dev/md1 /dev/sda1
nice -n  10 mdadm -a /dev/md1 /dev/sda1 &
fi


if [ "$disk2_status1" == "" ] ; then
mdadm -r /dev/md1 /dev/sdb1
nice -n  10 mdadm -a /dev/md1 /dev/sdb1 &
fi



if [ "$disk1_status2" == "" ] ; then
mdadm -r /dev/md2 /dev/sda2
nice -n  10 mdadm -a /dev/md2 /dev/sda2 &
fi

if [ "$disk2_status2" == "" ] ; then
mdadm -r /dev/md2 /dev/sdb2
nice -n  10 mdadm -a /dev/md2 /dev/sdb2 &
fi


if [ "$disk1_status3" == "" ] ; then
mdadm -r /dev/md3 /dev/sda3
nice -n  10 mdadm -a /dev/md3 /dev/sda3 &
fi

if [ "$disk2_status3" == "" ] ; then
mdadm -r /dev/md3 /dev/sdb3
nice -n  10 mdadm -a /dev/md3 /dev/sdb3 &
fi



if [ "$disk1_status" == "" ] ; then
mdadm -r /dev/md5 /dev/sda5
nice -n  10 mdadm -a /dev/md5 /dev/sda5 &
fi

if [ "$disk2_status" == "" ] ; then
mdadm -r /dev/md5 /dev/sdb5
nice -n  10 mdadm -a /dev/md5 /dev/sdb5 &
fi
verbose "Exiting add failed disk scprit" 
