#!/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
echo "These are large files, so it will take some time ..."
echo " "

gunzip <files/rescue.gz | dd bs=1M of=${USB_DEV}1
if [ $? != 0 ]; then
  echo " "
  echo "Copy of boot setup failed. USB key creation failed!!!"
  echo " "
  exit 1
fi

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

if [ $dir = "/" ] ;then
  echo "Oops 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 "Oops 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 !!"
