#!/bin/bash
#
# MOLMAKE-ISO -- This program makes iso image for Make One Linux
# Copyright (C) 2007 Keicho Kondo <dgel@users.sourceforge.jp>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

## === Setup Environment === ##
CONFIG_ROOT="/etc/mol"

# load libmolmake
if which libmolmake >/dev/null; then
	. libmolmake
else
	echo "$(basename $0): Can't find libmolmake." >/dev/stderr
	exit 1
fi

# load config files
load_molmake_config $CONFIG_ROOT

# allow only root account
check_user_account

# check variables whether it is filled or not
check_variable BUILD_ROOT $BUILD_ROOT
check_variable ISO_ROOTDIR $ISO_ROOTDIR
check_variable ISODIR $ISODIR
check_variable MOL_VERSION $MOL_VERSION
check_variable LIVECDSGN $LIVECDSGN
check_variable INSTALLER_DATA $INSTALLER_DATA
check_variable ISOLINUX_DATA $ISOLINUX_DATA
check_variable ISOLINUXBIN $ISOLINUXBIN
check_variable ISOLINUXCFG $ISOLINUXCFG
check_variable KERNEL $KERNEL
check_variable INITRAM $INITRAM

# set cmd output option
NUM=0
case $* in *--line*) NUM=$(($NUM + 2));; esac
case $* in *--log*) NUM=$(($NUM + 1));; esac
CMD="cmd $NUM"

# check options
case "$*" in
	*--help*)
	echo "MOLMAKE-ISO $MOL_VERSION"
	echo " (C)2007 Keicho Kondo <dgel@users.sourceforge.jp>"
	echo
	echo "This program makes iso image for Make One Linux."
	echo "Usage: molmake-iso [option]"
	echo
	echo "   --clean         : Clean up files made by this script"
	echo "   --help          : Show this help"
	echo "   --version       : Show version and notice"
	echo
	echo "   --line          : Show running command line"
	echo "   --log           : Output log in $BUILD_ROOT/log"
	echo
	exit 0 ;;

	*--version*)
	echo "MOLMAKE-ISO $MOL_VERSION"
	echo "Written by Keicho Kondo."
	echo
	echo "Copyright (C) 2007 Keicho Kondo."
	echo "This is free software; see the source for copying conditions.  There is NO"
	echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
	echo
	exit 0 ;;

	*--clean*)
	echo "Cleaning iso image..."
	[ -d "${ISO_ROOTDIR}/mol" ] && $CMD mv ${ISO_ROOTDIR}/mol $BUILD_ROOT
	$CMD rm -rf $ISO_ROOTDIR
	$CMD rm -f ${ISODIR}/MOL-${MOL_VERSION}${ISO_SUFFIX}.iso
	$CMD rm -f ${BUILD_ROOT}/molmake-iso.flag
	exit 0 ;;
esac

# check flag
if [ ! -f "${BUILD_ROOT}/molmake-initramfs.flag" ]; then
	die "<< First, please execute molmake-initramfs script. >>"
elif [ ! -f "${BUILD_ROOT}/molmake-modules.flag" ]; then
	die "<< First, please execute molmake-modules script. >>"
elif [ -f "${BUILD_ROOT}/molmake-iso.flag" ]; then
	die "<< First, please execute this script with --clean option. >>"
fi


## === Start === ##
echo "MOLMAKE-ISO Starting"

# creating iso9660 root directory
echo "Creating iso data directory..."
$CMD mkdir -p ${ISO_ROOTDIR}
$CMD mkdir -p ${ISO_ROOTDIR}/boot
$CMD mv ${BUILD_ROOT}/mol ${ISO_ROOTDIR}
$CMD echo "MOL_VERSION=${MOL_VERSION}" >>${ISO_ROOTDIR}/boot/${LIVECDSGN}

# copying harddisk installer
echo "Copying harddisk installer..."
[ "${INSTALL2WIN}" == "yes" ] && $CMD copy_install2win ${INSTALLER_DATA} ${ISO_ROOTDIR}
[ "${INSTALL2LIN}" == "yes" ] && $CMD copy_install2lin ${INSTALLER_DATA} ${ISO_ROOTDIR}

# copying isolinux, kernel image, initrd.img
echo "Copying isolinux, kernel image, initrd..."
$CMD cp -a ${ISOLINUX_DATA} ${ISO_ROOTDIR}/boot/isolinux
$CMD cp -a ${ISOLINUXBIN} ${ISO_ROOTDIR}/boot/isolinux/isolinux.bin
$CMD cp -a ${ISOLINUXCFG} ${ISO_ROOTDIR}/boot/isolinux/isolinux.cfg
$CMD cp -a ${KERNEL} ${ISO_ROOTDIR}/boot/isolinux/vmlinuz
$CMD cp -a ${INITRAM} ${ISO_ROOTDIR}/boot/isolinux/initrd.img

# creating iso9660 image
echo "Creating iso9660 image file..."
$CMD mkisofs -quiet -l -r -J -R \
	-V "MOL-${MOL_VERSION}${ISO_SUFFIX}" \
	-b boot/isolinux/isolinux.bin \
	-c boot/isolinux/isolinux.cat \
	-no-emul-boot -boot-load-size 4 -boot-info-table -hide-rr-moved \
	-o ${ISODIR}/MOL-${MOL_VERSION}${ISO_SUFFIX}.iso \
	${ISO_ROOTDIR} 2>/dev/null


## === fin. === ##
$CMD touch ${BUILD_ROOT}/molmake-iso.flag
echo "MOLMAKE-ISO Finished"
exit 0


