#!/bin/sh
if [ -f nozen ]; then 
DIALOG=dialog
else
DIALOG=Xdialog
fi
set -x				
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" 60 60
				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 ?  (no)" 60 60
					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 " 60 60
				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)" 60 60
				exit 1
				fi
					else

				a=` sfdisk -n /dev/$part < working/diskinfo/sfdisk.$part.make.bsi 2>&1`
				echo $a > tmp
				grep Warning tmp
				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.$part.make.bsi 2>&1`
				echo $a > tmp
				grep Warning tmp
				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

						partprobe /dev/$part >/dev/null 2>&1
						. ./unmount-all
						sfdisk /dev/$part < working/diskinfo/sfdisk.$part.make.bsi
						fi
