#!/bin/sh
#
# 
. ./config
cwd=$PWD

if [ ! `whoami` = "root" ] ; then
  echo ""
  echo "You need to be root to run this shell script"
  echo ""
  exit 1
fi
usb=`blkid |grep bs-rescue|cut -d: -f1`
USB_DEV=${usb%?}
echo "Please make sure that only one casper-rw exists on the system"
echo "if you have other disks labeled casper-rw, unplug them now"
echo ""
echo "and make sure your usb key is mounted"
echo ""
echo "this will install bacula-rescue on " ${USB_DEV}
echo ""
echo "Continue?? (y/n)"
read a
if [ ! "$a" = "y" ];then
  exit 1
fi


if [  x{$USB_DEV} = x{} ];then
  echo "You must mount your usb key before running this script"
  exit 1
fi




dir=`df |grep ${USB_DEV}1 |awk '{print $NF}'`

umount ${USB_DEV}1 >/dev/null 2>&1
echo "we're about to erase the content of ${USB_DEV}1 , continue ? (y/n)"
read a
if [ ! "$a" = "y" ];then
  exit 1
fi

gunzip <files/rescue.gz | dd bs=1M of=${USB_DEV}1




dir=`df |grep ${USB_DEV}2 |awk '{print $NF}'`

if [ $dir = "/" ] ;then
  echo "ooops we were about to scrap your system , was the usb key mounted ??, guess not...."
  exit 1
fi

cd $dir
echo "we're about to erase the content of $dir , continue ? (y/n)"
read a
  if [ ! "$a" = "y" ];then
  exit 1
fi

tar -xvzf $cwd/files/rw.tar.gz 

dir=`df |grep ${USB_DEV}3 |awk '{print $NF}'`
if [ $dir = "/" ] ;then
  echo "ooops we were about to scrap your system , was the usb key mounted ??, guess not...."
  exit 1
fi
cd $cwd
echo "we're about to erase the content of $dir , continue ? (y/n)"
read a
  if [ ! "$a" = "y" ];then
  exit 1
fi
cp -r files/bacula-rescue $dir

#rm -r /tmp/bs-rescue
echo "syncing ..."
sync
sync
echo ""
echo ""
echo ""
echo ""
echo "Enjoy !!"
