#!/bin/sh
#
# Script to make a Bacula "rescue" disk
#
device=/mnt/floppy
dev=/dev/fd0

# Print Usage message
usage () {
   cat <<END_OF_DATA
Usage: make_rescue_disk
  -h, --help             print this message
  --make-static-bacula   make static File daemon and add to diskette
  --copy-static-bacula   copy static File daemon to diskette
  --copy-etc-files       copy files in etc list to diskette
END_OF_DATA
}

# defaults
make_bacula=no
copy_bacula=no
copy_etc=no

#
# Process command line options
#
for option in "$@" ; do
   case "$option" in
   -h | --help)
      usage
      exit 0
      ;;
   --make-static-bacula)
      make_bacula=yes
      copy_bacula=yes
      ;;
   --copy-static-bacula)
      copy_bacula=yes
      ;;
   --copy-etc-files)
      copy_etc=yes
      ;;
   *)
      echo "Unknown option specified: $option"
      usage
      exit 1
      ;;
   esac
done

echo ""
echo "Place a blank diskette in your floppy drive."
echo "It will be formatted and all data will be lost."
echo ""
echo -n "Press return when ready: "
read a
umount $device 2>&1 >/dev/null
if [ $? = 0 ] ; then
   mounted=1
else
   mounted=0
fi
mke2fs $dev
mount $dev $device 2>&1 >/dev/null

if [ x$make_bacula = "xyes" ] ; then
   ./make_static_bacula
fi

if [ x$copy_bacula = "xyes" -a ! -e bacula-fd ] ; then
   echo " "
   echo "bacula-fd not found. If you want Bacula on this"                 
   echo "rescue disk please run \"make_static_bacula\" first."
   echo "Continuing anyway ..."
   echo " "
   exit 1
   have_bacula=no
else
  echo "Stripping and compressing Bacula"
  strip bacula-fd
  gzip bacula-fd
  have_bacula=yes
fi

echo "Copying files to $device" 
cp -a . $device

if [ x$copy_etc = "xyes" ] ; then
   echo "Tarring /etc files to $device"
   tar --create --gzip --file $device/etc.gz --files-from backup.etc.list
fi

if [ x$have_bacula = "xyes" ] ; then
   rm -f bacula-fd.gz bacula-fd.conf
fi
sync
if [ $mounted = 0 ] ; then
   umount $device
fi
