#!/bin/bash

# Manfred.Kehr@siemens-enterprise.com
# Version
# V 1.0 released;
# V 1.1 released; shift touch cmd

VER='1.1'

MDADM=/sbin/mdadm
BASENAME=`basename $0`
LOGGER="/bin/logger -t $BASENAME"
GREP=/usr/bin/grep
SED=/usr/bin/sed
AWK=/usr/bin/awk
SORT=/usr/bin/sort
HEAD=/usr/bin/head
UDEVADM=/sbin/udevadm
TOUCH=/usr/bin/touch
CUT=/usr/bin/cut
ECHO=/bin/echo
BIN_DATE="/bin/date"
FDISK=/sbin/fdisk
WC=/usr/bin/wc
RAIDSTAT=/var/run/raidstat
STATUS_FILE="/proc/mdstat"
HEADER=Softraid:
SLEEP=2
HOTREMOVE=true
LOGHIST="/var/log/softraid/raid.history"
LOGFILEDIR=/var/log/softraid
SLEEPWATCH=10


if [ -n "$HOTREMOVE" ]; then
    if [ "$2" == "delayedremove" ]; then
      sleep $SLEEP
      FAULTYDEVS=$(grep -s md $STATUS_FILE | grep -cs "(F)")
		DEGDEVS=$(grep -s blocks $STATUS_FILE | grep -cs _)
		LDATE=$($BIN_DATE)
		if [ $FAULTYDEVS -ne 0 ] || [ $DEGDEVS -ne 2 ]; then
				$LOGGER "$HEADER ($LINENO) BAD: Hot remove of /dev/$1 without remove from raid before"
				LOGLINE="$LDATE: ($LINENO) critical: Hot remove of /dev/$1 without remove from raid before"
				echo $LOGLINE >> $LOGHIST
		else
					$LOGGER "$HEADER ($LINENO) OK: Hot remove of /dev/$1 with remove from raid before"
					LOGLINE="$LDATE: ($LINENO) info: Hot remove of /dev/$1 with remove from raid before"
					echo $LOGLINE >> $LOGHIST
		fi
		mdline=`grep $1 /proc/mdstat`  #mdline contains something like "md0 : active raid1 sda1[0] sdb1[1]"
		mddev=${mdline% :*}            #delete everything from " :" till the end of line and return the result as mddev
		if [ -n "$mddev" ]; then
            #
            isautomaticremoveallowed=1
            while [ 1 ] ; do
                [ -e /dev/shm/add2raid-block-remove.pid ] || break
                add2raidpid=$(cat /dev/shm/add2raid-block-remove.pid)
                [ "x$add2raidpid" = "x" ] && break
                [ $add2raidpid -eq $add2raidpid ] || break
                [ -e /proc/$add2raidpid ] || break
                grep -qi add2raid /proc/$add2raidpid/cmdline || break
                isautomaticremoveallowed=0
                break
            done
            if [ $isautomaticremoveallowed -eq 1 ] ; then
                $LOGGER "$HEADER Trying to remove /dev/$1 from $mddev"
                log=`$MDADM /dev/$mddev --fail detached --remove detached 2>&1`
                $LOGGER "$HEADER $log"
                # touch raidstat to trigger portal
            else
                $LOGGER "$HEADER Removal of /dev/$1 from $mddev is NOT allowed because add2raid (pid $add2raidpid) is in progress"
            fi
		fi
		$TOUCH -a $RAIDSTAT
    else
		if [[ "$1" != d* ]]; then
            #
            isautomaticremoveallowed=1
            while [ 1 ] ; do
                [ -e /dev/shm/add2raid-block-remove.pid ] || break
                add2raidpid=$(cat /dev/shm/add2raid-block-remove.pid)
                [ "x$add2raidpid" = "x" ] && break
                [ $add2raidpid -eq $add2raidpid ] || break
                [ -e /proc/$add2raidpid ] || break
                grep -qi add2raid /proc/$add2raidpid/cmdline || break
                isautomaticremoveallowed=0
                break
            done
            if [ $isautomaticremoveallowed -eq 1 ] ; then
                $LOGGER "$HEADER ($LINENO) Called to remove /dev/$1. Will be delayed for $SLEEP seconds"
                $0 $1 delayedremove &
            else
                $LOGGER "$HEADER ($LINENO) Called to remove /dev/$1 is NOT allowed because add2raid (pid $add2raidpid) is in progress"
            fi
		fi
		exit 0
    fi
else
    if [ "$2" == "delayedremove" ]; then
      sleep $SLEEP
			FAULTYDEVS=$(grep -s md $STATUS_FILE | grep -cs "(F)")
			LDATE=$($BIN_DATE)
			if [ $FAULTYDEVS -ne 0 ]; then
				$LOGGER "$HEADER ($LINENO) BAD: Hot remove of /dev/$1 without remove from raid before"
				LOGLINE="$LDATE: ($LINENO) critical: Hot remove of /dev/$1 without remove from raid before"
				echo $LOGLINE >> $LOGHIST		
			else
					$LOGGER "$HEADER ($LINENO) OK: Hot remove of /dev/$1 with remove from raid before"
					LOGLINE="$LDATE: ($LINENO) info: Hot remove of /dev/$1 with remove from raid before"
					echo $LOGLINE >> $LOGHIST
			fi
			# touch raidstat to trigger portal
			$TOUCH -a $RAIDSTAT
    else
	  if [[ "$1" != d* ]]; then
		  $LOGGER "$HEADER ($LINENO) Called to remove /dev/$1. Will be delayed for $SLEEP seconds"
		  $0 $1 delayedremove &
	  fi
      exit 0
    fi			
fi

exit 0