#!/bin/sh

set -x
z=0
bar="/"
one="1"
two="2"
three="3"
cwd=`pwd`
host=`hostname`
goodanswer=1000
doit=0

if [ ! `whoami` = "root" ] ; then
  echo ""
  echo "You need to be root to run this script ..."
  echo ""
  exit 1
fi



if [ -f ./nozen ]; then 
rm ./nozen
fi

if [ -f ./differenthostname ]; then 
rm ./differenthostname
fi




  Xdialog --title " Welcome" --msgbox "Welcome to the bacula usb rescue" 10 30|| { echo "";echo "";echo "";echo "";echo "you don't seem to have an X server running, defaulting to console mode" ; echo "nozen" >> nozen ; }
echo ""
echo ""
echo ""

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

if [ -f nozen ]; then
$DIALOG --title " Welcome" --msgbox "Welcome to the bacula usb rescue" 10 30
fi
$DIALOG --radiolist "Do you want to:" 10 40 3 \
        1 "Collect info for a client" off \
        2 "Restore a client" off 2>ans
	if [ $? = 1 ];then
	exit 1
	fi
wannado=`cat ans|cut -d'"' -f2 |cut -d'"' -f1`
     
#usbdev=`mount -l |grep xubuntu9 |cut -d" " -f3`
root=`df |grep rootfs|awk '{print $6}'`

if [ x{$wannado} = x{$two} ] ; then




	if [ ! x{$root} = "x{/}" ]; then

 		

	$DIALOG --msgbox "Please boot on  the usb key to restore a client" 30 80

exit 1
	


	else
		. ./restore2/restoredebut
exit 0
	fi

fi


if [ x{$wannado} = x{$one} ] ; then

	if [ x{$root} = x{$bar} ]; then

 	


	
	$DIALOG --msgbox "Please boot on your system (not on the usb key) \n       to gather client info" 20 60
	
	exit 0


	fi
if [ -f unmounted ];then
rm unmounted
fi

mount -l |awk '{print $1}'|grep /dev/ >mounted
blkid |awk '{print $1}'|grep -v /dev/md |while read line;do
line=${line%?}
grep $line mounted >/dev/null 2>&1

if [ ! $? = 0 ];then
echo $line >> unmounted
fi
done
echo "Mounted disks are:                                                             Unmounted disks are:">mntinfo 
       
unmounted=`cat unmounted|wc -l`

mounted=`cat mounted|wc -l`
if [ $mounted -gt $unmounted ];then
lines=$mounted
else
lines=$unmounted
fi

if [ $mounted -eq $unmounted ];then
lines=$mounted
fi


for i in $(seq 1 $lines) ;do
a=`cat  mounted| awk "NR==$i"`
b=`cat  unmounted| awk "NR==$i"`
long1=`echo $a|wc -c`
long2=`echo $b|wc -c`
if [ $long2 -lt 11 ];then
if [ $long1 -lt 11 ];then
echo "$a                                                                              $b">>mntinfo
else
echo "$a                                                      $b">>mntinfo
fi
else
echo "$a                                                      $b">>mntinfo
fi
done
mounted=`cat mntinfo`

rm unmounted
#rm mntinfo
rm mounted


 	$DIALOG --title " Warning " --yesno "Only mounted disks will get picked up, \n Please mount all the disks you want to be able \n to restore before running this script \n \n \n $mounted \n \n  \n Note that raid devices will show up as unmounted \n \n Do you want to continue ?"  600 600
 
	if [ $? = 1 ];then
	exit 1
	fi

	while [ $goodanswer -gt 600 ]; do

  	 if [ -f ../clients/$host/diskinfo/df.bsi ] ; then

	
	$DIALOG --radiolist "Please note that the informations are stored by hostname ,\n we seem to already have gattered information for that hostname. Do you want to :" 20 80 3 \
        1 "continue , moving previous data to .bak (erasing old .bak if any) ?" off \
        2 "choose a name for this backup (you will have to remember it for restoring) ? " off 2>ans 
	if [ $? = 1 ];then
	exit 1
	fi
	changename=`cat ans|cut -d'"' -f2 |cut -d'"' -f1`
	
	else

	$DIALOG --radiolist "Please note that the informations are stored by hostname ,\n The hostname for this machine is $host \n  Do you want to :" 20 80 3 \
        1 "Use the hostname as a reference for this client ?" off \
        2 "choose a name for this backup (you will have to remember it for restoring) ? " off 2>ans 
	if [ $? = 1 ];then
	exit 1
	fi
	changename=`cat ans|cut -d'"' -f2 |cut -d'"' -f1`
	fi
	

		if [ $changename = $one ] ;then

		newhostname=$host
		goodanswer=500
		doit=1
		fi




			if [ $changename = $two ] ;then


	

			$DIALOG --inputbox "Please enter a name under which you want to save this client" 30 80 2>ans
       				if [ $? = 1 ];then
				exit 1
				fi

			newhostname=`cat ans|cut -d'"' -f2 |cut -d'"' -f1`

				if [ -f ./differenthostname ]; then 
				rm ./differenthostname
				fi
	
			echo $newhostname >> differenthostname
			goodanswer=500
			 fi

				if [ $changename -ne $two ] ; then
				goodanswer=1000
				fi


 					if [ -f ../clients/$newhostname/diskinfo/df.bsi ] ; then
						if [ ${doit} -gt 0 ] ; then
						goodanswer=500

						else

						goodanswer=1000

						fi


							if [ x{$wannado} = x{$two} ] ; then

								if [ ! x{$usbdev} = x{$bar} ]; then

 	

								$DIALOG --msgbox  "Please boot on  the usb key to restore a client" 30 30

								exit 0
								fi

							fi
					else 
  					  goodanswer=500
					fi
	done

  fi


./getdiskinfo


