#!/bin/sh
#
# bootcdbackup
#
#  bootcdbackup /dev/VolGroup00/LogVol00 otherdist /mnt/tmp/space [-nomount] [-2diskconf <file>] <$*>
#  1. mkdir /tmp/backup
#  2. [ -2diskconf ] || bootcdmk2diskconf -b -f /dev/VolGroup00/LogVol00 >/tmp/backup/bootcd2disk.conf
#  3. [ -nomount ] || bootcd2disk -onlymount -c /tmp/backup 
#  4. bootcd2disk -mkchroot /mnt/tmp/space <$*>
#  5. tar /mnt/tmp/space/chroot
#     chroot /mnt/tmp/space/chroot bootcdwrite
#     cp cdimage.iso 
#  6. [ -nomount ] || bootcd2disk -onlyunmount -c /tmp/backup 

CONFVARS="ERRLOG"
# directory created in builddir
BOOTCDDIR="bootcd"

TODOY="-y"
TODO=""
ENV_IMAGEURL=""
VERBOSE=""
SCRIPT=""
CONFDIR="/etc/bootcd"
NOMOUNT=""
DISKCONF2=""
BACKUPDIR=""
BACKUPNAME=""
COPYDEST="/mnt"

##################### nothing to configure behind this line ##################

cleanup()
{
  local TODO=""
  trap "" 2 EXIT
  [ "$CLEANTMP" ] && TODO="$TODO \"$CLEANTMP\""
  [ "$CLEANCHROOT" ] && TODO="$TODO \"$CLEANCHROOT\""
  [ "$CLEANUMNT" ] && TODO="$TODO \"$CLEANUMNT\""
  [ "$TODO" ] && eval "interactive $TODOY $TODO"
}

unset $CONFVARS

[ -f /etc/bootcd/bootcdbackup.conf ] && . /etc/bootcd/bootcdbackup.conf
[ -f ./bootcd-run.lib ] && . ./bootcd-run.lib || [ -f /usr/share/bootcd/bootcd-run.lib ] && . /usr/share/bootcd/bootcd-run.lib
for i in $CONFVARS; do
  [ "`set | grep ^$i=`" ] || err "$i is not set in $CONFDIR/bootcdbackup.conf"
done

# ignore this output from bootcdwrite
bootcd2disk_ignore()
{
  stdout "^--- copying CD and RAM to"
  stdout "^--- handling udev ---$"
  stdout "^--- deleting NOT_TO_RAM symlinks ---$"
  stdout "^--- Building fstab ---$"
  stdout "^--- Resetting mtab ---$"
  stdout "^--- Building grub/menu.lst ---$"
  stdout "^--- Enabling turned off stuff ---$"
  ignore "^Please Reboot now !$"
}  

# ignore this output of tar
tar_ignore()
{
  stdout "socket ignored"
}

# function: usage
# get: $1 -- error string
usage()
{
  [ "$1" ] && echo -e >&2 "\n$1"
  echo -e >&2 "[-i] [-v] [-s] [-c <confdir>] [-url <url] [-nomount] [-2diskconf <file>] <dev> <name> <builddir>\n"
  echo -e >&2 "\n $(basename $0) make an offline backup."
  echo -e >&2 " syntax:  -i           -- interactiv operation"
  echo -e >&2 "          -v           -- be noisy"
  echo -e >&2 "          -s           -- be silent and no interaction"
  echo -e >&2 "          -c <confdir> -- use another configuration directory"
  echo -e >&2 "          -url <url>   -- url from imageserver"
  echo -e >&2 "                          (overwrite IMAGEURL and USEIMAGESERVER)"
  echo -e >&2 "          -2diskconf <file> -- add your own bootcd2disk.conf for target system"
  echo -e >&2 "                          (may be created with \"bootcdmk2diskconf\" on target disk)"
  echo -e >&2 "          -nomount     -- don't mount target disk\n"
  echo -e >&2 "          <dev>        -- device where to find the file \"fstab\" for the target disk"
  echo -e >&2 "          <name>       -- name of the backup (no blanks!)"
  echo -e >&2 "                          (used as directory /<name> where to find the tarball of the target disk)"
  echo -e >&2 "                          (also used on bootcd2disk -c <name> to restore the backup)"
  echo -e >&2 "          <builddir>   -- build the backup CD/DVD on this directory (need lot of space!)"
  echo -e >&2 ""
  exit 1
}

############################## now the real things ###########################


# get options
while [ $# -gt 3 ]; do
  case "$1" in
     "-h"|"--help")
        usage
        ;;
     "-i")
        TODOY=""
	shift
        ;;
   "-url")
        ENV_IMAGEURL="$2"
        shift 2
        ;;
     "-v")
        VERBOSE="$1"
	shift
        ;;
     "-c")
        CONFDIR="$2"
        shift 2
        ;;
     "-s")
        SCRIPT="$1"
	shift
        ;;
     "-nomount")
        NOMOUNT="$1"
	shift
        ;;
     "-2diskconf")
        DISKCONF2="$2"
	shift 2
	;;
        *)
        usage "ERROR: option \"$1\" unknown!"
        ;;
  esac
done

if [ $# -ne 3 ]; then
  usage "Wrong number of parameters"
fi

BACKUPDEV="$1"
BACKUPNAME="$2"
BACKUPDIR="$3/$BOOTCDDIR"
COPYDEST="$BACKUPDIR/chroot"

### Main ###

A=""
while [ "$A" != "y" -a "$A" != "n" ]
do
  echo -n "Directory $BACKUPDIR will be removed and created again!(y|n) " | tee -a $ERRLOG >&2
  if [ "$SCRIPT" ]; then
    A="y"
  else
    read A
  fi
  echo "$A" >> $ERRLOG >&2
done
if [ "$A" = "n" ]; then
  exit 1
fi

CLEANTMP=""
CLEANUMNT=""
CLEANCHROOT=""
trap cleanup 2 EXIT

BOOTCD2DISK="bootcd2disk"
if [ -f ./bootcd2disk ]; then
  echo "using local ./bootcd2disk" | 
    if [ "$SCRIPT" ]; then cat >> $ERRLOG; else tee -a $ERRLOG >&2; fi
  BOOTCD2DISK="./bootcd2disk"
fi

BOOTCDMK2DISKCONF="bootcdmk2diskconf"
if [ -f ./bootcdmk2diskconf ]; then
  echo "using local ./bootcdmk2diskconf" | 
    if [ "$SCRIPT" ]; then cat >> $ERRLOG; else tee -a $ERRLOG >&2; fi
  BOOTCDMK2DISKCONF="./bootcdmk2diskconf"
fi

TODO=""

TODO="$TODO \"run \\\"mkdir /tmp/backup\\\"; CLEANTMP=\\\"rm -rf /tmp/backup\\\"\""

if [ "$DISKCONF2" ]; then
  TODO="$TODO \"run \\\"cp $DISKCONF2 /tmp/backup\\\"\""
else
  [ "$VERBOSE" ] && echo "--- Creating backup bootcd2disk.conf ---" | tee -a $ERRLOG >&2
  TODO="$TODO \"run \\\"$BOOTCDMK2DISKCONF -b -f $BACKUPDEV > /tmp/backup/bootcd2disk.conf\\\"\""
fi

if [ ! "$NOMOUNT" ]; then
  TODO="$TODO \"run \\\"$BOOTCD2DISK -onlymount -c /tmp/backup -s\\\";"
  TODO="$TODO CLEANUMNT=\\\"$BOOTCD2DISK -onlyunmount -c /tmp/backup -s\\\"\""
fi
  
TODO="$TODO \"bootcd2disk_ignore\""
TODO="$TODO \"run \\\"$BOOTCD2DISK"
[ "$CONFDIR" = "/etc/bootcd" ] || TODO="$TODO -c $CONFDIR"
[ "$ENV_IMAGEURL" ] && TODO="$TODO -url $ENV_IMAGEURL"
TODO="$TODO -mkchroot $BACKUPDIR -s\\\"; CLEANCHROOT=\\\"rm -rf $BACKUPDIR\\\"\""

TODO="$TODO \"run \\\"mkdir -p $COPYDEST/$BACKUPNAME\\\"\""

EXCLUDE=".$(echo "$COPYDEST" | sed "s|^/mnt||")"
TODO="$TODO \"tar_ignore\""
TODO="$TODO \"run \\\"(cd /mnt; tar c -z --exclude $EXCLUDE -f $COPYDEST/$BACKUPNAME/backup.tgz .)\\\"\""
TODO="$TODO \"run \\\"cp /tmp/backup/bootcd2disk.conf $COPYDEST/etc/bootcd/$BACKUPNAME\\\"\""

TODO="$TODO \"run \\\"chroot $COPYDEST bootcdwrite -s\\\"\""
TODO="$TODO \"run \\\"mv $COPYDEST/var/spool/bootcd/cdimage.iso $BACKUPDIR/cdimage.iso\\\"\""
TODO="$TODO \"run \\\"rm -r $COPYDEST\\\"\""

if [ ! "$NOMOUNT" ]; then
  TODO="$TODO \"run \\\"$BOOTCD2DISK -onlyunmount -c /tmp/backup -s\\\"\""
fi

eval "interactive $TODOY $TODO"
