#!/bin/sh

if [ -f disklistmp ];then
rm disklistmp
fi
if [ -f added_diskstmp ];then
rm added_diskstmp
fi
touch added_diskstmp

if [ -f working/scripts/create-vg ];then

		cat disklist|while read line;do
			if [ ! $line = "" ];then
			echo $line >>disklistmp
			fi
		done


if [ -f raidisks ];then
		cat raidisks|while read line;do
			if [ ! $line = "" ];then
			echo $line >>disklistmp
			fi
		done
fi

cat disklistmp|while read line;do
if [ ! $line = "" ];then
grep $line working/scripts/create-vg>>/dev/null 2>&1

	if [ $? = 0 ];then

			b=`grep $line working/scripts/create-vg`
			field=`echo $b|wc -w`
			while [ $field -ne 0 ];do
			a=`echo $b|awk "{ print \$ $field }"`
			echo $a|grep /dev >/dev/null 2>&1
				if [ $? = 0 ];then
				grep $a disklist>>/dev/null 2>&1
					if [ ! $? = 0 ];then
					echo $a >>added_disks
					
					fi
				fi
			field=$(( field - 1 ))
			done
	fi
fi
done

		   cat added_disks|while read line;do
			if [ ! $line = "" ];then
			echo $line|grep md>/dev/null 2>&1
				if [ $? = 0 ];then
				grep $line checkraidtemp>>/dev/null 2>&1
					if [ ! $? = 0 ];then
					a=`grep $line working/scripts/recreate_raid_arrays`
					field=`echo $a|wc -w`
						while [ $field -ne 0 ];do
						b=`echo $a|awk "{ print \$ $field }"`
						echo $b|grep /dev |grep -v md >/dev/null 2>&1
							if [ $? = 0 ];then
							grep $b disklist>>/dev/null 2>&1
								if [ ! $? = 0 ];then
								echo $b >>added_diskstmp
					
								fi
							fi
						field=$(( field - 1 ))
						done
					fi
				else

				cat disklist|grep $line>>/dev/null 2>&1
				  if [ ! $? = 0 ];then
				  echo $line >>disklist
				  fi
				fi



			fi
		done

if [ -f disklistmp ];then
rm disklistmp
fi
touch disklistmp
		cat disklist|while read line;do
			if [ ! $line = "" ];then
				grep $line disklistmp>>/dev/null 2>&1
				if [ ! $? = 0 ];then
				echo $line >>disklistmp
				fi
			fi
		done

		cat added_diskstmp|while read line;do
			if [ ! $line = "" ];then
				grep $line disklistmp>>/dev/null 2>&1
				if [ ! $? = 0 ];then
				echo $line >>disklistmp
				fi
			fi
		done

mv disklistmp disklist
echo "">added_disks
fi
if [ -f disklistmp ];then
rm disklistmp
fi
if [ -f added_diskstmp ];then
rm added_diskstmp
fi



