#! /bin/bash

#
# SYNOPSIS:
#              ISOIMAGE=<image.iso> ./make_iso.sh
#
# DESCRIPTION:
#
#  Create the final iso image. Grub boot loader is used. 
#
#  by setting XEXCLUDES you can pass additional options to mkisofs  
#
#  example:
#            XEXCLUDES='-x *.inst -x Example -x LinuxSchulung -x Samsung*' ./make_iso.sh
#
#
#  You can choose the bootloader by setting the  BOOTLOADER environment variable
#  supported are grub and isolinux
#
#  !! if you use isolinux bootloader you need to configure 
#
#   master/$ISOLINUX_DIR/isolinux.cfg
#
#  and link (hardlink) the kernel to a simple short name, i.e. 
#
#       cd master/boot
#       ln vmlinux-2.6.12.4 linux
#
#
#
# Part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006 by Wolfgang Rohrmoser <info@rohrmoser-engineering.de>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

# get configuration
source /etc/LiveBackup/defaults.cfg

isodir=$(dirname $ISOIMAGE)
if [ ! -d $isodir ]; then
    echo "ERROR: directory $isodir not found"
    exit 1
fi

#
# remove backup files
#
rm -f $MASTER_DIR/boot/grub/*~
rm -f $MASTER_DIR/$ISOLINUX_DIR/*~


echo -e "creating ISO image: $ISOIMAGE"
echo "using bootloader: $BOOTLOADER"

#
# check and display disk space
#

echo -e "\n\nchecking disk space ...."
_du=$( get_used_disk_space $MASTER_DIR )
echo -e "  - you need $_du MB of free disk space"
_df=$( get_free_disk_space $(dirname $ISOIMAGE) )
echo -e "  - you have $_df MB of free disk space\n"
if [ $_df -le $_du ]; then
    echo "ERROR!! disk space is not sufficent to save the ISO file!!"
    exit 1
fi

if [ -f $ISOIMAGE ]; then
    echo -e "INFO: overwriting existing image:\n\t$(ls -lh $ISOIMAGE)\n"
fi

continueY "continue (Y/n): "


case "$BOOTLOADER" in

  grub)
    #
    # make an "El  Torito" bootable CD, use grub
    #
    if [ ! -d $MASTER_DIR/boot/grub/ ]; then
	echo "!!ERROR: grub is missing in master directory"
	exit 1
    fi
    mkisofs -J -R -iso-level 4 \
        -x ".svn" \
        -x "*~" \
        $XEXCLUDES \
        -T -b boot/grub/stage2_eltorito -no-emul-boot \
        -boot-load-size 4 -boot-info-table -o $ISOIMAGE $MASTER_DIR
    ;;


  isolinux)
    #
    # make an "El  Torito" bootable CD, use isolinux
    #
    if [ ! -d $MASTER_DIR/$ISOLINUX_DIR/ ]; then
	echo "!!ERROR: isolinux is missing in master directory"
	exit 1
    fi
    mkisofs -J -R \
        -x ".svn" \
        -x "*~" \
        $XEXCLUDES \
        -T  -b $ISOLINUX_DIR/isolinux.bin -c $ISOLINUX_DIR/boot.cat \
        -no-emul-boot \
        -boot-load-size 4 -boot-info-table -o $ISOIMAGE $MASTER_DIR
    ;;

esac


echo "created:"; ls -lh $ISOIMAGE


#
# HINT: you can mount the image by:
#
# > mount -o loop -t iso9660 $ISOIMAGE /mnt/
#
# or start the system with qemu:
#
# > qemu -m 256 -cdrom $ISOIMAGE
#

