#!/bin/sh
				
if [ -f nozen ]; then 
DIALOG=dialog
else
DIALOG=Xdialog
fi
				
MB=MB				part=`echo $disk|cut -d"/" -f3`
				fdisk -l /dev/$part |grep bytes|grep -v Units|awk '{print $5}'>samesize
				cat working/diskinfo/fdisk.$part |grep bytes|grep -v Units|awk '{print $5}'>samesize2
				diff samesize samesize2 >/dev/null 2>&1
			if [ ! $? = 0 ] ;then
				nowdisk=`cat samesize`
				oldisk=`cat samesize2`
				$DIALOG --title "different disk" --msgbox "The size of the disk you are trying to restore \n and the one we have backed up differ for /dev/$part  \n \n The old disk size was  $oldisk  \n And the actual disk size is  $nowdisk \n \n We'll have to do things manually" 600 600
				oldiskparted=`cat working/diskinfo/parted.$part`
				howmanyparts=`cat working/diskinfo/parted.$part|wc -l`
				howmanyparts=$(( $howmanyparts - 7 ))
					$DIALOG --title "different disk" --yesno "Do you want to do it manually \n ( we will launch gparted) (yes) \n Or only adjust the LAST partition size to fit the disk ? \n (We only shrink the las partition, it will not claim empty space on the drive) (no)" 600 600
					if [ $? = 0 ];then
				          if [ $DIALOG = Xdialog ];then
				          echo $$oldiskparted
				          $DIALOG --title "different disk" --msgbox "the old partition sizes for this disk was \n \n $oldiskparted \n \n We will now launch gparted so you can adjust you disk partitions " 600 600
				gparted /dev/$part
				          else
				$DIALOG --title "different disk" --msgbox "the old partition sizes for this disk was \n \n $oldiskparted \n \n please use fdisk or cfdisk to repartition your drive \n \n and the re-run this script pointing to the partitions of the new disks  \n (like /dev/sda1 ) instead of the whole disk (/dev/sda)" 600 600
				          exit 1
				          fi
					else

					a=` sfdisk -n /dev/$part < working/diskinfo/sfdisk.make.$part.bsi 2>&1`
				        echo $a > tmp
				        grep exceed tmp >/dev/null 2>&1
				               if [ $? = 0 ];then
				               tr ' ' '\n' < tmp >tmp2
				               cat tmp2|awk /given/,/sfdisk/ >tmp3
				               currentsize=`cat tmp3|grep -m1 "("|cut -d"(" -f2 |cut -d")" -f1`
				               maxsize=`cat tmp3|grep -m2 "("|cut -d"(" -f2 |cut -d")" -f1|tail -n1`
				               sed -i "s/$currentsize/$maxsize/g" working/diskinfo/sfdisk.make.$part.bsi
				               nowarn=1
				                  while [ $nowarn = 1 ];do
				                  a=` sfdisk -n /dev/$part < working/diskinfo/sfdisk.make.$part.bsi 2>&1`
				                  echo $a > tmp
				                 grep exceed tmp >/dev/null 2>&1
   							if [ $? = 0 ];then
							tr ' ' '\n' < tmp >tmp2
							cat tmp2|awk /given/,/sfdisk/ >tmp3
							currentsize=`cat tmp3|grep -m1 "("|cut -d"(" -f2 |cut -d")" -f1`
							maxsize=`cat tmp3|grep -m2 "("|cut -d"(" -f2 |cut -d")" -f1|tail -n1`
							sed -i "s/$currentsize/$maxsize/g" working/diskinfo/sfdisk.make.$part.bsi
							else
							nowarn=0
							fi
						  done
						partprobe /dev/$part >/dev/null 2>&1
						. ./unmount-all
						sfdisk /dev/$part < working/diskinfo/sfdisk.make.$part.bsi
						rm tmp
						rm tmp2
						rm tmp3
						fi
					fi
		else						
				echo ". ./unmount-all">>partitionscript
				#cat working/scripts/partition.$part>>fullrestorescript
				#echo "partprobe >/dev/null 2>&1">>fullrestorescript
				#echo "umount /dev/"$part"* >/dev/null 2>&1">>fullrestorescript
 				echo "dd if=working/diskinfo/mbr."$part" of=/dev/"$part>>partitionscript
				echo "partprobe /dev/"$part" >/dev/null 2>&1">>partitionscript
				echo ". ./unmount-all">>partitionscript
		   fi

			if [ -f formatscript ]; then
			rm formatscript
			fi
cat >formatscript <<END_OF_DATA
. ./unmount-all
#
#  Format all partitions on disk $disk -- created by getdiskinfo
#
echo ""
echo "This script will format all partitions on disk $disk."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $disk !!!!"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi 
fi
if [ ! -f badblocksnoprompt ];then
echo "Do you want to do a disk check for bad blocks?"
echo -n "It is recommended, but takes time. yes/no: "
read a
if [ x\$a = xyes ] ; then
  check="-c"
else
   check=
fi
fi
END_OF_DATA

formatscripto=`ls working/scripts |grep $disk|grep format`
cat working/scripts/$formatscripto |grep $disk|while read line;do
			echo $line >>formatscript

			done
		cat formatscript>>formatallscript
		rm formatscript
