#!/bin/sh


raidiskdone=""
if [ -f noprompt ];then
rm noprompt
fi

if [ -f badblocksnoprompt ];then
rm badblocksnoprompt
fi

if [ -f partitionscript ];then
rm partitionscript
fi
if [ -f formatallscript ];then
rm formatallscript
fi
touch formatallscript

if [ -f added_disks ];then
rm added_disks
fi
touch added_disks
  if [ -f checkraidtemp ];then
  rm checkraidtemp
  fi
    touch checkraidtemp
      if [ -f added_disks ];then
      rm added_disks
      fi
touch added_disks
if [ -f fulldisks ];then
rm fulldisks
fi
if [ -f checkraidtemp ];then
rm checkraidtemp
fi
if [ -f raidisks ];then
rm raidisks
fi
touch raidisks
touch checkraidtemp
touch fulldisks

  if [ -d working ];then
  rm -r working
  fi
cp -r ../clients/$clientdir working

if [ -f nozen ]; then 
DIALOG=dialog
else
DIALOG=Xdialog
fi

chkdsk=""
done=1
notraid=1
#partprobe >>/dev/null 2>&1
#. ./restore2/checkdiffnew2
. ./restore2/get_serial_restore
. ./restore2/change_names_new
. ./restore2/warning
. ./restore2/choosedisk
. ./unmount-all
 

$DIALOG --title "AUTOMATIC ?" --yesno "do you want to automatically restore all disks ? \n (otherwise you will be prompted for each disk)" 30 60 

  if [ $? = 0 ];then
  echo "" > noprompt
  fi
$DIALOG --title "AUTOMATIC ?" --yesno "do you want to automatically disable \n the check for bad blocks on all disks ? \n (otherwise you will be prompted for each disk)" 30 60 

if [ $? = 0 ];then
echo "" > badblocksnoprompt
fi

####################################################check raid

		. ./restore2/checkraid

		
		



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

. ./restore2/check_lvm
fi

####################################################recheck raid


		. ./restore2/checkraid

#############################################check wether all raid disks have the right partition type

. ./restore2/all_raids_there
###########################################do all disks


cat disklist|while read line;do
	disk=`echo $line`
	. ./restore2/diskorpart

	disk=`echo $disk |cut -d"/" -f3`

   if [ "$partordisk" = "disk" ];then
  
  
   . ./restore2/full_disk

  	
   
   fi


	if [ "$partordisk" = "part" ];then
		di=${disk%?}
		d=`echo $di |tail -c2`
			echo $d | egrep '^[0-9]+$' >/dev/null 2>&1
				if [ "$?" -eq "0" ]; then
				di=${di%?}
				fi
		grep $di fulldisks>>/dev/null
					if [ ! $? = 0 ];then

					. ./restore2/part
					fi
	fi


done


if [ -f partitionscript ];then
chmod +x partitionscript
. ./partitionscript
fi


if [ -f checkraidtemp ];then
chmod +x checkraidtemp
ra=`cat checkraidtemp`
if [ ! "$ra" = "" ];then
. ./release_raids
. ./restore2/raid
fi
rm checkraidtemp
fi







if [ -f working/scripts/create-vg ];then
. ./restore2/do_lvm
fi
ra=`cat create-vg`
if [ ! "$ra" = "" ];then
chmod +x create-pv
chmod +x create-vg
chmod +x create-lv

. ./create-pv
sleep 5
. ./create-vg
sleep 5
. ./create-lv
fi
$DIALOG --title "Format ?" --yesno "Now we will format the choosen disks, Do you want to do this ?? \n Normally you would answer yes here, \n Only answer No if you want to try to restore them with bacula without formatting them first \n (this could be usefull if you know what's broken on your system \n and you just want bacula to replace certain files) " 600 600 

  if [ $? = 0 ];then
chmod +x formatallscript

. ./formatallscript
fi
# if [  -d clients/$clientdir.working.bak ];then
#mv clients/$clientdir.working.bak clients/$clientdir 
#fi
. ./chrootscript
