#!/bin/sh
host=$clientdir
owd=/media/bs-rescue/bacula-rescue/clients						
usb=`blkid |grep bs-rescue|cut -d: -f1`
usb=${usb%?}
if [ -d working/diffdir ];then
rm -r  working/diffdir
fi

########################## here we get disks infos
mkdir -p working/diffdir
 blkid | awk '{ print $1 }'|grep -v loop|grep -v md|grep -v mapper|grep -v $usb|while read line;do
line=${line%?}
l=`echo $line |cut -d"/" -f3`
echo "/dev/"$l > working/diffdir/uuid.$l


a=`blkid $line |wc -w|xargs expr`
while [ $a -ne 0 ];do
blkid $line |awk "{print \$$a}"|grep UUID >/dev/null 2>&1
if [ $? = 0 ];then
blkid $line |awk "{print \$$a}"|grep UUID>>working/diffdir/uuid.$l
break
fi
if [ $a = 1 ];then
echo $line >> working/diffdir/uuid.disks.with.no.uuid

fi
a=$(( a - 1))
done

done

fdisk -l |grep /dev|grep -v $usb|grep :|cut -d: -f1|cut -d" " -f2|while read line;do
l=`echo $line |cut -d"/" -f3`

dd if=$line of=working/diffdir/mbr.$l bs=1 count=512 >/dev/null 2>&1
done

############################## here we start to diff with the backed up files
cd working/diffdir
for i in `ls `;do                ########## first by uuid
diff $i $owd/$host/diskinfo/$i >/dev/null 2>&1
if [ ! $? = 0 ];then
echo $i>>changedisks
#else
#i=${i%?}
#echo "/dev/"`echo $i|cut -d. -f2` " previously /dev/"`echo $i|cut -d. -f2`>>newdisk
fi
done

echo "">newdisk

if [ -f changedisks ];then
cat changedisks|while read line;do
lin=${line%?}
li=`echo $lin |cut -d. -f2`
check=`cat newdisk|awk '{print $1}'`
echo $check|grep $li
if [ ! $? = 0 ];then
echo $line|grep uuid>/dev/null 2>&1
  if [ $? = 0 ];then
  i=`grep UUID $line`
 i=`echo $i|cut -d'"' -f2`
 i=`echo $i|cut -d'"' -f1`
   find $owd/$host/diskinfo -type f -exec grep -l $i {} +>/dev/null 2>&1
    if [ $? = 0 ];then
       check=`cat newdisk|awk '{print $1}'`
       
       echo $check|grep $li
	  if [ ! $? = 0 ];then   
    a=`find $owd/$host/diskinfo/uuid.* -type f -exec grep -l $i {} +`
    a=`echo $a|cut -d/ -f8|cut -d. -f2`
    a=${a%?}
    
    echo "/dev/"`echo $lin|cut -d. -f2`" previously /dev/"$a>>newdisk
          fi
    fi

  fi
            if [ ! -f newdisk ];then
            echo "">newdisk
            fi  
#grep `echo $line|cut -d. -f2` `echo newdisk |awk '{print $1}'|cut -d/ -f3` > /dev/null 2>&1
          a=`cat newdisk|awk '{print $1}'`
       
          echo $a|grep $li
	  if [ ! $? = 0 ];then   
	   ########### then by mbr

       echo $line|grep mbr>/dev/null 2>&1
       if [ $? = 0 ];then
         for i in `ls $owd/$host/diskinfo|grep mbr`;do
         diff $line $owd/$host/diskinfo/$i >/dev/null 2>&1
		  if [ $? = 0 ];then
                  a=`cat newdisk|awk '{print $1}'`
                  c=`echo $lin|cut -d. -f2`
                  echo $a|grep $c
                      if [ ! $? = 0 ];then  
                    echo "/dev/"`echo $line|cut -d. -f2` " previously /dev/"`echo $i|cut -d. -f2` >>newdisk
                      fi
		  fi
	 done
       fi        

     fi
fi
done
fi

fdisk -l |grep /dev|grep -v $usb |grep -v /dev/md|grep :|cut -d: -f1|cut -d" " -f2|while read line ;do
b=1
  cat newdisk |grep $line|while read i ;do
  a=`echo $i|cut -d" " -f1`
    if [ $a = $line ];then
    cat newdisk |grep $a|awk '{print $1}'|grep $a
   	if [ ! $? = 0 ];then
         echo $i >> newdiskorder
         b=0
        fi
     fi
  done
if [ $b = 1 ];then
 cat newdisk |grep $line|awk '{print $1}'|grep $line
   	if [ ! $? = 0 ];then
echo $line "previously" $line>>newdiskorder
        fi
fi
done
cd ../../




