#!/bin/sh
#iface=`ifconfig |cut -d" " -f1|head -n1`

swp=`blkid |grep swap|awk '{print $1}'|cut -d: -f1|head -n1`
if [ ! $swp = "" ];then
swapon $swp
fi

iface=`grep dev= working/scripts/start_network|cut -d= -f2`
. ./unmount-all
howmany=`ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo|wc -l`

tmp=""
echo "">tmp
ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo |while read line; do
#line=${line%?}

echo `echo $line $line'\'`>>tmp

tmp2=`cat tmp`
#tmp2=${tmp2%?}
echo $tmp2 >tmp
done
tmp=`cat tmp`

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

$DIALOG --menu "Which ethernet adapter connects to the director ??:" 30 60 20 $tmp 2>ans 
ifaceans=`cat ans`




sed -i 's/'$iface'/'$ifaceans'/g' working/scripts/start_network
. ./working/scripts/start_network
. ./unmount-all
if [ -f mount_drives ];then
rm mount_drives
fi

if [ -d /media/bs-rescue/chroot ];then
rm -r /media/bs-rescue/chroot
fi
mkdir /media/bs-rescue/chroot


touch mount_drives
cat disklist|while read line;do
cat working/scripts/mount_drives|grep $line>>/dev/null 2>&1
if [ $? = 0 ];then
cat working/scripts/mount_drives|grep $line|while read LINE;do
cat mount_drives|grep "$LINE"
if [ ! $? = 0 ];then
echo $LINE>>mount_drives
fi
done
fi
done

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

chmod +x mount_drives
. ./mount_drives
if [ ! -d /media/bs-rescue/chroot/sys ];then
mkdir /media/bs-rescue/chroot/sys
fi
if [ ! -d /media/bs-rescue/chroot/tmp ];then
mkdir /media/bs-rescue/chroot/tmp
chmod 777 /media/bs-rescue/chroot/tmp
fi
if [ ! -d /media/bs-rescue/chroot/dev ];then
mkdir /media/bs-rescue/chroot/dev
fi
if [ -f /media/bs-rescue/chroot/bacula-fdstatic ];then
rm /media/bs-rescue/chroot/bacula-fdstatic
fi
#cp bacula-fdstatic /media/bs-rescue/chroot
#cp batermined /media/bs-rescue/chroot
#cp working/diskinfo/bacula-fd.conf /media/bs-rescue/chroot #### doesn t work if you copy files
#cp working/diskinfo/bat.conf /media/bs-rescue/chroot		before mounting disks
if [ ! -d /media/bs-rescue/chroot/proc ];then
mkdir /media/bs-rescue/chroot/proc
fi
mount -o bind /proc /media/bs-rescue/chroot/proc






cp working/diskinfo/bacula-fd.conf /media/bs-rescue/chroot

if [ ! -d /media/bs-rescue/chroot/etc ];then
mkdir -p /media/bs-rescue/chroot/etc
fi
if [ ! -d /media/bs-rescue/chroot/lib ];then
mkdir -p /media/bs-rescue/chroot/lib
fi
cp /lib/libnss_dns.so.2 /media/bs-rescue/chroot/lib
cp /etc/nsswitch.conf /media/bs-rescue/chroot/etc
cp /etc/resolv.conf /media/bs-rescue/chroot/etc
cp ping.ermined /media/bs-rescue/chroot/ping

if [ ! -d /media/bs-rescue/chroot/var/bacula/working ];then
mkdir -p /media/bs-rescue/chroot/var/bacula/working
fi
if [ ! -d /media/bs-rescue/chroot/var/run/bacula ];then
mkdir -p /media/bs-rescue/chroot/var/run/bacula
fi

if [  -d /etc/bacula ];then
rm -r /etc/bacula
fi
mkdir /etc/bacula


if [ -f  working/diskinfo/bconsole.conf ];then

cp working/diskinfo/bconsole.conf /etc/bacula
../bin/bconsole.ermined < bcon > bconsole_output
	cat bconsole_output |grep Version|awk '{print $5}'|grep 3.0
	if [ $? = 0 ];then
	cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-3.0.3 /media/bs-rescue/chroot/bacula-fd-static
	fi
		cat bconsole_output |grep Version|awk '{print $5}'|grep 2.4
		if [ $? = 0 ];then
		cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-2.4.4 /media/bs-rescue/chroot/bacula-fd-static
		fi
else

$DIALOG --backtitle "If you want to connect to the director from this machine, \n you must provide a bconsole.conf , where is it ? \n (otherwise you can restore this client from another bconsole bat or bweb \n on another machine ) " --title "(spacebar to select)" --aspect 12 --begin 4 3 --fselect / 30 60 2>tmp
    if [ $? = 1 ];then
    $DIALOG --title "!!!!!!" --msgbox "No bconsole.conf, you will have to access your director \n from another machine and restore the directories \n that correspond to the drives you just formated" 30 60
$DIALOG --radiolist "Which director version do you use ?:" 10 40 3 \
        1 "  2.4.x" off \
        2 "  3.0.x" off 2>ans
	if [ $? = 1 ];then
	exit 1
	fi
   
			version=`cat ans`
			if [ $version = "1" ];then
			cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-2.4.4 /media/bs-rescue/chroot/bacula-fd-static
			fi
				if [ $version = "2" ];then
				cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-3.0.3 /media/bs-rescue/chroot/bacula-fd-static
				fi
	rm ans
   fi
		if [ -f tmp ];then
		bakonf=`cat tmp`
		rm tmp
		else
		bakonf=""
		fi

echo $bakonf |grep bconsole.conf
  if [ $? = 0 ];then

  cp $bakonf /etc/bacula
  else
 $DIALOG --radiolist "Which director version do you use ?:" 10 40 3 \
        1 "  2.4.x" off \
        2 "  3.0.x" off 2>ans
	if [ $? = 1 ];then
	exit 1
	fi
    fi
		version=`cat ans`
		if [ $version = "1" ];then
		cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-2.4.4 /media/bs-rescue/chroot/bacula-fd-static
		fi
			if [ $version = "2" ];then
			cp /media/bs-rescue/bacula-rescue/bin/bacula-fd-static-3.0.3 /media/bs-rescue/chroot/bacula-fd-static
			fi
			rm ans


  fi
chroot /media/bs-rescue/chroot /bacula-fd-static -c /bacula-fd.conf &
df |grep /media/bs-rescue/chroot |grep -v /media/bs-rescue/chroot/ >/dev/null 2>&1
if [ $? = 1 ];then
df |grep /media/bs-rescue/chroot >tmp 
place=`cat tmp|awk '{print $6}'|cut -c 24-1000`
if [ -f tmp2 ];then
rm tmp2
touch tmp2
fi
	df |grep /media/bs-rescue/chroot >tmp 
	place=""
		cat tmp|while read line;do
		b=`echo $line|awk '{print $6}'|cut -c 24-1000`
  			if [ ! $b = "" ];then
 	 		placet=`echo $b`
 	 		
 	 		else
			lv=`df |grep mapper`
 	 		placet=`df $lv|grep media|awk '{print $5}'|cut -c 24-1000`
 	 		
   				
    				
    	
			fi
                                if [ -f tmp2 ];then
				  cat tmp2|grep $placet
				  if [ ! $? = 0 ];then
 				  echo  $placet>>tmp2
				  fi
                                else
				echo  $placet>>tmp2
				fi

		done

place=`cat tmp2`
rm tmp2





$DIALOG --title "!!! WARNING !!!" --msgbox "Be carefull you didn't choose your root disk, so you must ONLY restore \n files that are situated on  \$ $place \n otherwise you would fill up this usb key instead of your disks \n \n \n But you still want to point to / (where to ?? >>>  / )" 30 60
fi
$DIALOG --title "!!! WARNING !!!" --msgbox "Now you must restore your files pointing to / \n We are faking that your disks are mounted on the root ( / ) \n \n \n If you restore your files anywhere else it wont work" 30 60
if [ -f /etc/bacula/bconsole.conf ];then
 ../bin/bconsole.ermined
fi
$DIALOG --title "PROCESSING" --msgbox "Please press ok when your job is done" 30 60
if [ -d /media/bs-rescue/chroot/boot/grub ];then
$DIALOG --title "Install GRUB ?" --yesno "You seem to have GRUB installed on this disk, \n Do you want to run install-grub (grub 1) on this disk ? \n (we will back up the grub dir in grub.bak in case something goes wrong)" 30 60 
if [ $? = 0 ];then
grubdisk=`df |grep /media/bs-rescue/chroot |grep -v /media/bs-rescue/chroot/ |awk '{print $1}'`
cp -r /media/bs-rescue/chroot/boot/grub /media/bs-rescue/chroot/boot/grub.bak
grub-install --root-directory=/media/bs-rescue/chroot/boot/grub $grubdisk
fi
fi
$DIALOG --title "THAT'S IT" --msgbox "That's it , we're all done \n \n \n Enjoy !!!" 30 60
if [ -f added_disks ];then
rm added_disks
fi
if [ -f alldisks ];then
rm alldisks
fi

if [ -f ans ];then
rm ans
fi

if [ -f badblocksnoprompt ];then
rm badblocksnoprompt
fi

if [ -f noprompt ];then
rm noprompt
fi

if [ -f clientmp ];then
rm clientmp
fi


if [ -f dialogtest ];then
rm dialogtest
fi

if [ -f disklist ];then
rm disklist
fi

if [ -f fulldisks ];then
rm fulldisks
fi

if [ -d working ];then
rm -r working
fi

if [ -f mount_drives ];then
rm mount_drives
fi

if [ -f tmp ];then
rm tmp
fi
 pid=`ps -ax|grep ermine|grep -v grep|awk '{print $1}'`
if [ ! $pid = "" ];then
kill $pid
fi
lsof |grep /media/bs-rescue/chroot|awk '{print $2}'|while read line ;do
if [ ! $line = "" ];then
kill $line
fi
done
