#!/bin/bash
#
# LiveCD iso build script
# Copyright (C) 2002-2003, Jaco Greeff <jaco@linuxminicd.org>
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Adapted from the MiniCD (http://www.linuxminicd.org) and mkinitrd build scripts
#
# $Id: mklivecd.in,v 1.41 2003/10/06 14:50:35 jaco Exp $
#

PATH=/sbin:/usr/sbin:$PATH
export PATH
VERSION="0.5.6"
PROGNAME="mklivecd"

rootdir=/
target=""
kernel=""
verbose=""
MODULES=""
img_vers=""
builtins=""
modulefile=/etc/modules.conf
tmpdir=""
rc=0
kernel26=""
modules="cdrom ide-cd ext3 isofs jbd nls_iso8859-13 nls_iso8859-14 nls_iso8859-15 nls_iso8859-1 nls_iso8859-2 nls_iso8859-3 nls_iso8859-4 nls_iso8859-5 nls_iso8859-6 nls_iso8859-7 nls_iso8859-8 nls_iso8859-9 nls_utf8"
excludedirs="^/[.].* ^/dev$ ^/initrd$ ^/lost+found$ ^/mnt$ ^/proc$ ^/tmp$ ^/var/lock/subsys$ ^/var/run$ ^/var/tmp$"
excludefiles="^/[.].* ^/fastboot$ /core[.][0-9][0-9]*$ .*[.]rpmnew$ .*[.]rpmsave$ /[.]bash_history$ /[.]fonts[.]cache-[0-9]$ /[.]xauth.* /[.]xsession-errors$"
cloopimg=""
LIVECDISOCLP="livecd.iso.clp"

# compression options
loopcmp="create_compressed_fs"
loopmod="cloop"
blocksize=64

# boot options
isokeyboard=""
isobootmsg=""
isoimage=""
isobootkeymsg=""
resolution="1024x768"
vgamode="791"
bootsplash=1

usage () {
	echo "Usage:" >&2
	echo "    `basename $0` [options] <livecd-image>" >&2
	echo "" >&2
	echo "General Options:" >&2
	echo "    --help                     Display this message" >&2
	echo "    --version                  Display version information" >&2
	echo "" >&2
	echo "Image generation:" >&2
	echo "    --verbose                  Be verbose in output" >&2
	echo "    --rootdir=<rootdir>        Root directory of the live filesystem" >&2
	echo "    --tmpdir=<tmpdir>          Temp directory for images" >&2
	echo "    --cloop=<image>            Name of the saved compressed image (1)" >&2
	echo "    --exclude-dir=<excludes>   Excludes directories from the final image (2)" >&2
	echo "    --exclude-file=<excludes>  Excludes files from the final image (2)" >&2
	echo "    --kernel=<kernel>          Kernel to use as boot image" >&2
	echo "    --lowmem                   Use disk as temporary storage for iso images" >&2
	echo "    --final                    Strip extra lines from the scripts on the initrd" >&2
	echo "" >&2
	echo "    1. This option can have unexpected side-effects. When an image by this name " >&2
	echo "       is found, it will not be re-created/overwritten, rather the existing image" >&2
	echo "       will be re-used, i.e. the compressed image is not re-built." >&2
	echo "    2. Patterns passed to these options is to be valid in a grep(1) search, " >&2
	echo "       and space delimited, e.g. --exclude-dir=\"^/home/jaco ^/root/\.mozilla\"" >&2
	echo "       will exclude both the /home/jaco and /root/.mozilla directories" >&2
	echo "" >&2
	echo "Compression options:" >&2
	echo "    --blocksize=<size>         Compression blocksize in K (default: 64)" >&2
	echo "    --looptype=<cloop|bzloop>  Create a cloop (default) or bzloop image (3)" >&2
	echo "" >&2
	echo "    3. The bzloop option is currently utilising the experimental bzloop kernel" >&2
	echo "       module. It should not be considdered as stable and might have adverse" >&2
	echo "       (unintended) side-effects." >&2
	echo "" >&2
	echo "Boot options:" >&2
	echo "    --iso-bootmsg=<msg>        Use 'msg' as the isolinux message display" >&2
	echo "    --iso-bootkey=<key>,<msg>  Display 'msg' on key 'key' from isolinux" >&2
	echo "    --iso-graphic              Use the Mandrake boot image extension" >&2
	echo "    --iso-bgimage=<image.lss>  Use 'image.lss' as the isolinux display image" >&2
	echo "    --keyboard=<mapping>       Specify a different keyboard layout (default: us)" >&2
	echo "    --resolution=<res>         Specify the resolution for the framebuffer (default: 1024x768)" >&2
	echo "    --splash={yes|no}          Create with bootsplash if available on image (default: yes)" >&2
	echo "    --theme=<theme>            Use 'theme' as the bootsplash theme" >&2
	echo "" >&2
	echo "Examples:" >&2
	echo "    `basename $0` --exclude-dir=\"^/usr/src/RPM ^/root/tmp\" livecd.iso" >&2
	echo "    `basename $0` --blocksize=224 livecd.iso" >&2
	echo "" >&2
	exit 1
}

### do a shell command
do_cmd() {
	stripped=`echo $* | tr -d "\t"`
	[ -n "$verbose" ] && echo "+ $stripped"
	if [ -n "$verbose" ]; then
		eval "$*" || exit
	else
		(eval "$*") 2>&1 >/dev/null || exit
	fi
}

do_cp() {
	do_cmd "cp $1 $2"
}

do_cp_strip() {
	if [ -n "$final" ]; then
		do_cmd "cat $1 | \
			sed -e 's/^#[^!].*//;s/^#$//;s/^[ ].*//' | \
			tr -d \"\t\" | grep -v ^$ >$2"
	else
		do_cp $@
	fi
}

find_module() {
	fmPath=`(cd /lib/modules/$kernel; find -type f -name $modName.o.gz | grep -v build)`
	if [ -z "$fmPath" ]; then
		fmPath=`(cd /lib/modules/$kernel; find -type f -name $modName.o | grep -v build)`
	fi
}

check_kernel_26() {
	local -i major minor

	major=$(expr "$1" : '\([^.]\+\)\..*')
	minor=$(expr "$1" : '[^.]\+\.\([^.]\+\)\..*')

	if [ $major -ge 3 -o $major -eq 2 -a $minor -ge 5 ]; then
		kernel26=yes
		modulefile=/etc/modprobe.conf
		splash=""
	fi
}

is_good_fs() {
	local parttype= tmpname=
	local dir=$1
	[[ -d $dir ]] || return 1
	[[ -w $dir ]] || return 1
	[[ $dir == */ ]] && dir=${dir%/}
	parttype=$(awk "{if (\$2 == \""$dir"\") print \$3 }" /proc/mounts)

	while tmpname=${dir%/*} && [[ -z $parttype ]];do
		[[ -z $tmpname ]] && tmpname=/
		parttype=$(awk "{if (\$2 == \""$tmpname"\") print \$3 }" /proc/mounts)
		dir=$tmpname
	done

	case $parttype in
		nfs|tmpfs) return 1;;
		*) return 0;
	esac
}

is_dietlibc_arch() {
	if [ -n "`uname -m | grep '\(i.86\|x86_64\)'`" ]; then return 0; fi
	return 1
}

### generate actual initrd image
create_initrdimg() {
	### create initrd
	echo "* Creating initrd structure..." >&2
	do_cmd "mkdir -p $INITRDDIR/{bin,dev,etc/livecd/hwdetect,lib/modules,proc,sbin,usr/{bin,sbin}}"
	do_cmd "mkdir -p $INITRDDIR/{cdrom,loopfs,ramfs}"
	if is_dietlibc_arch; then
		if [[ -n "$kernel26" ]]; then
			do_cp /sbin/insmod-25-DIET $INITRDDIR/sbin/insmod
		else
			do_cp /sbin/insmod-DIET $INITRDDIR/sbin/insmod
		fi
		else
		if [[ -n "$kernel26" ]]; then
			do_cp /sbin/insmod.static-25 $INITRDDIR/sbin/insmod
		else
			do_cp /sbin/insmod.static $INITRDDIR/sbin/insmod
		fi
	fi
	do_cp /usr/bin/busybox $INITRDDIR/bin/busybox
	do_cp_strip /usr/share/mklivecd/linuxrc $INITRDDIR/linuxrc
	do_cmd "chmod 755 $INITRDDIR/linuxrc"
	do_cp_strip /usr/sbin/hwdetect $INITRDDIR/usr/sbin/hwdetect
	do_cmd "chmod 755 $INITRDDIR/usr/sbin/hwdetect"

	### modules
	echo "* Copying initrd kernel modules..." >&2
	do_cmd "mkdir -p $INITRDDIR$MODULESDIR/kernel"
	modules="$loopmod $modules"
	for m in $modules; do
		modpath=`(cd ${rootdir}lib/modules/$kernel; find -type f -name $m.o.gz | grep -v build)`
		if [ -n "$modpath" ]; then
			do_cmd "gzip -dc ${rootdir}lib/modules/$kernel/$modpath >$INITRDDIR$MODULESDIR/kernel/$m.o"
		else
			modpath=`(cd ${rootdir}lib/modules/$kernel; find -type f -name $m.o | grep -v build)`
			if [ -z "$modpath" ]; then
				echo "ERROR: unable to find kernel module $m.o" >&2
				exit 1
			fi
			do_cp ${rootdir}lib/modules/$kernel/$modpath $INITRDDIR/$MODULESDIR/kernel/$m.o
		fi
	done
	do_cmd ":> $INITRDDIR$modulefile"
	if [ `uname -r` == "$kernel" ]; then
		do_cmd "depmod -a --basedir $INITRDDIR --config $INITRDDIR$modulefile"
	else
		do_cp ${rootdir}lib/modules/$kernel/modules.* $INITRDDIR/$MODULESDIR/
	fi

	### devices
	do_cmd "mknod $INITRDDIR/dev/initrd b 1 250"
	do_cmd "mknod $INITRDDIR/dev/null c 1 3"
	do_cmd "mknod $INITRDDIR/dev/systty c 4 0"
	for i in 1 2 3 4; do
		do_cmd "mknod $INITRDDIR/dev/tty$i c 4 $i"
	done

	### compressed image
	echo "* Creating compressed initrd image..." >&2
	IMAGESIZE=`du -ks $INITRDDIR | awk '{print $1}'`
	INODES=250
	for i in `find $INITRDDIR`; do
		INODES=$[INODES + 1];
	done
	for i in `find /dev`; do
		INODES=$[INODES + 1];
	done
	IMAGESIZE=$[IMAGESIZE + INODES / 10]  # 10 inodes needs 1k
	[ -n "$verbose" ] && echo "+ Creating filesystem with size ${IMAGESIZE}KB and $INODES inodes"
	do_cmd "dd if=/dev/zero of=$INITRD bs=1k count=$IMAGESIZE 2> /dev/null"
	do_cmd "mke2fs -q -m 0 -F -N $INODES -s 1 $INITRD"
	do_cmd "mount -o loop -t ext2 $INITRD $INITRDMNT"
	do_cmd "rm -rf $INITRDMNT/lost+found"
	do_cmd "(cd $INITRDDIR; tar cf - .) | (cd $INITRDMNT; tar xf -)"
	do_cmd "umount $INITRDMNT"
	do_cmd  "(cd $ISOLINUXDIR ; gzip -9 initrd)"
	if [ -n "$bootsplash" ]; then
		echo "* Creating bootsplash image..." >&2
		if [ -x ${rootdir}usr/share/bootsplash/scripts/make-boot-splash ]; then
			do_cmd "(mv -f $ISOLINUXDIR/initrd.gz ${rootdir}tmp/; \
		        	chroot ${rootdir} /usr/share/bootsplash/scripts/make-boot-splash /tmp/initrd.gz ${resolution}; \
				mv -f ${rootdir}tmp/initrd.gz $ISOLINUXDIR)"
		else
			echo "WARNING: '${rootdir}usr/share/bootsplash/scripts/make-boot-splash' does not exist. Creating initrd image without bootsplash" >&2
		fi
	fi
}

### create the compressed iso image
create_cloop() {
	echo "* Creating compressed loop image..." >&2
	if [ -f "$cloopimg" ]; then
		do_cmd "ln $cloopimg $LIVECDCLOOPDIR/$LIVECDISOCLP"
	else
		# handle excluded dirs
		do_cmd ":> $EXCLUDELIST"
		if [ ! -z "$excludedirs" ]; then
			PATTERNS=""
			for e in $excludedirs; do
				if [ -z "$PATTERNS" ]; then
					PATTERNS="$e"
				else
					PATTERNS="$PATTERNS
$e"
				fi
			done
			do_cmd "( find $rootdir -type d 2>/dev/null |sed -e 's,^$rootdir,/,g'| grep '$PATTERNS' | sed 's,^/,$rootdir,' >>$EXCLUDELIST)"
		fi

		# handle excluded files
		if [ ! -z "$excludefiles" ]; then
			PATTERNS=""
			for e in $excludefiles; do
				if [ -z "$PATTERNS" ]; then
					PATTERNS="$e"
				else
					PATTERNS="$PATTERNS
$e"
				fi
			done
			do_cmd "(find $rootdir -type f 2>/dev/null |sed -e 's,^$rootdir,/,g'| grep '$PATTERNS' | sed 's,^/,$rootdir,' >>$EXCLUDELIST)"
		fi

		# create the cloop image
		if [ -z `which $loopcmp` ]; then
			echo "ERROR: '$loopcmp' does not exist on your machine. You need to install an updated version of the cloop-utils package." >&2
			exit 1
		fi
		if [ -z `which mkisofs` ]; then
			echo "ERROR: 'mkisofs' does not exist on your machine. You need to install the mkisofs package." >&2
			exit 1
		fi
		REALSIZE=$[$blocksize*1024]
		MKISOCMD="nice -n 19 mkisofs -R -exclude-list $EXCLUDELIST -hide-rr-moved -cache-inodes -no-bak -pad $verbose $verbose $rootdir"
		[ -z "$verbose" ] && MKISOCMD="$MKISOCMD 2>/dev/null"
		if [ -n "$lowmem" ]; then
			do_cmd "$MKISOCMD >$TMPISO"
			CMD="$loopcmp $TMPISO $REALSIZE >$LIVECDCLOOPDIR/$LIVECDISOCLP"
			[ -z "$verbose" ] && CMD="$CMD 2>/dev/null"
			do_cmd "$CMD"
			do_cmd "rm -rf $TMPISO"
		else
			CMD="$MKISOCMD | $loopcmp - $REALSIZE >$LIVECDCLOOPDIR/$LIVECDISOCLP"
			[ -z "$verbose" ] && CMD="$CMD 2>/dev/null"
			do_cmd "$CMD"
		fi
		[ -n "$cloopimg" ] && do_cmd "ln $LIVECDCLOOPDIR/$LIVECDISOCLP $cloopimg"
	fi
}

### install isolinux
create_isolinux() {
	echo "* Creating isolinux image..." >&2
	isolinuxbin="/usr/lib/syslinux/isolinux.bin"
	[ -n "$isographic" ] && isolinuxbin="/usr/lib/syslinux/isolinux-graphic.bin"
	if [ ! -f $isolinuxbin ]; then
		echo "ERROR: '$isolinuxbin' does not exist on your machine. You need to install the syslinux package." >&2
		exit 1
	else
		do_cp $isolinuxbin $ISOLINUXDIR/isolinux.bin
	fi
	if [ ! -f ${rootdir}boot/vmlinuz-$kernel ]; then
		echo "ERROR: The kernel '${rootdir}boot/vmlinuz-$kernel' does not exist on your machine." >&2
		exit 1
	else
		do_cp ${rootdir}boot/vmlinuz-$kernel $ISOLINUXDIR/vmlinuz
	fi
	[ -n "$isobootmsg" ] && do_cp $isobootmsg $ISOLINUXDIR/livecd.msg
	if [ -n "$isoimage" ]; then
		[ ! -f $ISOLINUXDIR/livecd.msg ] && echo -n "" >$ISOLINUXDIR/livecd.msg
		do_cmd "echo -e '\\030livecd.lss' >>$ISOLINUXDIR/livecd.msg"
		do_cp $isoimage $ISOLINUXDIR/livecd.lss
	fi
	# everything generated via echo since we put in some of our variables on the
	# various command-lines
	do_cmd "echo 'default livecd' >$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo 'prompt  1' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo 'timeout 950' >>$ISOLINUXDIR/isolinux.cfg"
	[ -f $ISOLINUXDIR/livecd.msg ] && do_cmd "echo 'display livecd.msg' >>$ISOLINUXDIR/isolinux.cfg"
	keymsg=""
	if [ -n "$isobootkeymsg" ]; then
		for l in $isobootkeymsg; do
			key=`echo $l | awk -F',' '{ print $1 }'`
			msg=`echo $l | awk -F',' '{ print $2 }'`
			do_cp $msg $ISOLINUXDIR/livecd_$key.msg
			do_cmd "echo '$key livecd_$key.msg' >>$ISOLINUXDIR/isolinux.cfg"
		done
	fi
	do_cmd "echo '' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo 'label livecd' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo '    kernel vmlinuz' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo '    append livecd=livecd initrd=initrd.gz devfs=mount fastboot=yes ramdisk_size=32000 root=/dev/rd/3 vga=$vgamode automatic=method:cdrom $isokeyboard' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo '' >>$ISOLINUXDIR/isolinux.cfg"
	if [ -z "$final" ]; then
		do_cmd "echo 'label initrd' >>$ISOLINUXDIR/isolinux.cfg"
		do_cmd "echo '    kernel vmlinuz' >>$ISOLINUXDIR/isolinux.cfg"
		do_cmd "echo '    append livecd=initrd initrd=initrd.gz devfs=mount fastboot=yes ramdisk_size=32000 root=/dev/rd/3 vga=$vgamode automatic=method:cdrom $isokeyboard' >>$ISOLINUXDIR/isolinux.cfg"
		do_cmd "echo '' >>$ISOLINUXDIR/isolinux.cfg"
	fi
	do_cmd "echo 'label memtest' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo '    kernel memtest' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "echo '' >>$ISOLINUXDIR/isolinux.cfg"
	do_cmd "dd if=/dev/zero of=$ISOLINUXDIR/boot.cat bs=1k count=2 2> /dev/null"
}

### create the final iso image
create_liveiso() {
	echo "* Creating final iso image..." >&2
	DISTRO=`gawk -F' ' '{ print $1 " " $2 }' $rootdir/etc/mandrake-release`
	VERSION=`gawk -F' ' '{ print $4 " " $5 }' $rootdir/etc/mandrake-release`
	VERSION_DATE=`date --utc`
	CMD="nice -n 19 mkisofs -pad -l -R -J $verbose \
		-V '$DISTRO LiveCD' -A '$VERSION $VERSION_DATE' \
		-b isolinux/isolinux.bin -c isolinux/boot.cat \
		-hide-rr-moved -no-emul-boot -boot-load-size 4 -boot-info-table \
		-o $target $LIVECDDIR"
	[ -z "$verbose" ] && CMD="$CMD 2>/dev/null"
	do_cmd "$CMD"
}

check_theme() {
	[ -z "$theme" ] && return 0
	if [ -x ${rootdir}usr/share/bootsplash/scripts/switch-themes ]; then
		current_theme="`chroot ${rootdir} /usr/share/bootsplash/scripts/switch-themes -c`"
		current_theme="${current_theme#*: }"
		if [ "$theme" != "$current_theme" ]
		then
			# Is it available?
			have_requested_theme=
			for i in `chroot ${rootdir} /usr/share/bootsplash/scripts/switch-themes -l|awk '{if ($1!="Available") print $1}'`
			do
				[ "$i" == "$theme" ] && have_requested_theme=1 || :
			done
			if [ -z "$have_requested_theme" ]
			then
				echo "ERROR: The specified bootsplash theme, '$theme', is not available" >&2
				exit 1
			fi

			#We have the theme, so change it
			chroot ${rootdir} /usr/share/bootsplash/scripts/switch-themes "$theme"
		fi
	else
		echo "ERROR: '${rootdir}usr/share/bootsplash/scripts/switch-themes' does not exist. Cannot adhere to '--theme' option." >&2
		exit 1
	fi
}

restore_theme() {
	[ -z "$theme" ] && return 0
	if [ "$theme" != "$current_theme" ]
	then
		chroot ${rootdir} /usr/share/bootsplash/scripts/switch-themes "$current_theme"
	fi
}


echo "$PROGNAME, version $VERSION, http://www.linuxminicd.org/mklivecd/" >&2
echo "Copyright (C) 2002-2003, Jaco Greeff <jaco@linuxminicd.org>" >&2
echo "" >&2
while [ $# -gt 0 ]; do
	case $1 in
		--cloop*)
			if echo $1 | grep '=' >/dev/null ; then
				cloopimg="${1#--cloop=}"
			else
				cloopimg=$2
				shift
			fi
			;;

		--exclude-dir*)
			if echo $1 | grep '=' >/dev/null ; then
				excludes="${1#--exclude-dir=}"
			else
				excludes=$2
				shift
			fi
			for e in $excludes; do
				excludedirs="$excludedirs $e"
			done
			;;

		--exclude-file*)
			if echo $1 | grep '=' >/dev/null ; then
				excludes="${1#--exclude-file=}"
			else
				excludes=$2
				shift
			fi
			for e in $excludes; do
				excludefiles="$excludefiles $e"
			done
			;;

		--blocksize*)
			if echo $1 | grep '=' >/dev/null ; then
				blocksize="${1#--blocksize=}"
			else
				blocksize=$2
				shift
			fi
			;;

		--kernel*)
			if echo $1 | grep '=' >/dev/null ; then
				kernel="${1#--kernel=}"
			else
				kernel=$2
				shift
			fi
			;;

		--tmpdir*)
			if echo $1 | grep '=' >/dev/null ; then
				tmpdir="${1#--tmpdir=}"
			else
				tmpdir=$2
				shift
			fi
			;;

		--rootdir*)
			if echo $1 | grep '=' >/dev/null ; then
				rootdir="${1#--rootdir=}"
			else
				rootdir=$2
				shift
			fi
			;;

		--looptype*)
			if echo $1 | grep '=' >/dev/null ; then
				loopmod="${1#--looptype=}"
			else
				loopmod=$2
				shift
			fi
			if [ "$loopmod" == "cloop" ]; then
				LIVECDISOCLP="livecd.iso.clp"
				loopcmp="create_compressed_fs"
				loopmod="cloop"
			else
				if [ "$loopmod" == "bzloop" ]; then
					LIVECDISOCLP="livecd.iso.bzlp"
					loopcmp="create_bzfs"
					loopmod="bzloop"
				else
					echo "ERROR: You need to specify either bzloop or cloop with the '--looptype' option" >&2
					exit 1
				fi
			fi
			;;

		--lowmem)
			lowmem=1
			;;

		--final)
			final=1
			;;

		--splash*)
			if echo $1 | grep '=' >/dev/null ; then
				yn="${1#--splash=}"
			else
				yn=$2
				shift
			fi
			if [ "$yn" == "yes" ]; then
				bootsplash=1
			else
				bootsplash=
			fi
			;;

		--theme*)
			if echo $1 | grep '=' >/dev/null ; then
				theme="${1#--theme=}"
			else
				theme=$2
				shift
			fi
			;;


		--keyboard*)
			if echo $1 | grep '=' >/dev/null ; then
				mapping="${1#--keyboard=}"
			else
				mapping=$2
				shift
			fi
			isokeyboard="keyb=$mapping"
			;;

		--resolution*)
			if echo $1 | grep '=' >/dev/null ; then
				resolution="${1#--resolution=}"
			else
				resolution=$2
				shift
			fi
			case $resolution in
				640*)
					vgamode=785
					;;
				800*)
					vgamode=788
					;;
				1024*)
					vgamode=791
					;;
				1280*)
					vgamode=794
					;;
				1600*)
					vgamode=797
					;;
				*)
					echo "ERROR: Invalid resolution '$resolution' specified with '--resolution' option" >&2
					echo "       Valid resolutions are: ">&2
					echo "            640x480" >&2
					echo "            800x600" >&2
					echo "           1024x768" >&2
					echo "          1280x1024" >&2
					echo "          1600x1200" >&2
					exit 1
					;;
			esac
			;;

		--iso-bootmsg*)
			if echo $1 | grep '=' >/dev/null ; then
				isobootmsg="${1#--iso-bootmsg=}"
			else
				isobootmsg=$2
				shift
			fi
			if [ ! -f "$isobootmsg" ]; then
				echo "ERROR: The specified boot message, '$isobootmsg' could not be found." >&2
				exit 1
			fi
			;;

		--iso-bootkey*)
			if echo $1 | grep '=' >/dev/null ; then
				isobootkey="${1#--iso-bootkey=}"
			else
				isobootkey=$2
				shift
			fi
			key=`echo $isobootkey | awk -F',' '{ print $1 }'`
			msg=`echo $isobootkey | awk -F',' '{ print $2 }'`
			if [ -z "$key" -o -z "$msg" ]; then
				echo "ERROR: You need to specify both the key and message for the '--iso-bootkey' parameter." >&2
				exit 1
			fi
			if [ ! -f "$msg" ]; then
				echo "ERROR: The specified boot message, '$msg' could not be found." >&2
				#exit 1
			fi
			isobootkeymsg="$isobootkeymsg $isobootkey"
			;;

		--iso-graphic)
			isographic=1
			;;

		--iso-bgimage*)
			if echo $1 | grep '=' >/dev/null ; then
				isoimage="${1#--iso-bgimage=}"
			else
				isoimage=$2
				shift
			fi
			if [ ! -f "$isoimage" ]; then
				echo "ERROR: The specified boot graphic, '$isoimage' could not be found." >&2
				exit 1
			fi
			;;

		--version)
			echo "$PROGNAME, version $VERSION, http://www.linuxminicd.org/mklivecd/" >&2
			echo "Copyright (C) 2002-2003, Jaco Greeff <jaco@linuxminicd.org>" >&2
			echo "" >&2
			echo "This program is free software; you can redistribute it and/or modify" >&2
			echo "it under the terms of the GNU General Public License as published by" >&2
			echo "the Free Software Foundation; either version 2, or (at your option)" >&2
			echo "any later version." >&2
			echo "" >&2
			echo "This program is distributed in the hope that it will be useful," >&2
			echo "but WITHOUT ANY WARRANTY; without even the implied warranty of" >&2
			echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" >&2
			echo "GNU General Public License for more details." >&2
			echo "" >&2
			echo "You should have received a copy of the GNU General Public License" >&2
			echo "along with this program; if not, write to the Free Software" >&2
			echo "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." >&2
			echo "" >&2
			exit 0
			;;

		-h|--help)
			usage
			;;

		-v|--verbose)
			verbose="-v"
			;;

		-*)
			echo "ERROR: Unknown command-line option '$1'. Try '`basename $0` --help' for valid options." >&2
			exit 1
			;;

		*)
			if [ -z "$target" ]; then
				target="$1"
			else
				usage
			fi
			;;
	esac
	shift
done

### do we have the correct iso options?
if [ -n "$isographic" ]; then
	if [ -n "$isoimage" ]; then
		echo "ERROR: You cannot specify the '--iso-graphic' and '--iso-bgimage' options together." >&2
		exit 1
	fi
	if [ -z "$isobootmsg" ]; then
		echo "ERROR: You should specify the '--iso-bootmsg' option in conjunction with the '--iso-graphic' option/" >&2
		exit 1
	fi
fi

### do we have correct theme options?
if [ -n "$theme" ]; then
	if [ -z "$bootsplash" ]; then
		echo "ERROR: The '--theme' option can only be used with bootsplash enabled ('--splash') enabled." >&2
		exit 1
	fi
fi

### do we have target?
[ -z "$target" ] && usage

### are we root?
if [ `id -u` != "0" ]; then
	echo "ERROR: You need to be root to execute this program." >&2
	exit
fi

### check our passed parameters
[ -z "$kernel" ] && kernel=`uname -r`
[ -n "$verbose" ] && echo "+ Using $kernel as boot kernel"
check_kernel_26 "$kernel"
[[ -n $tmpdir ]] && { is_good_fs $tmpdir || tmpdir= ;} #command-line
if [[ -z $tmpdir ]]; then
	if is_good_fs /tmp; then
		tmpdir=/tmp
	elif is_good_fs $TMPDIR; then
		tmpdir=$TMPDIR
	elif is_good_fs /var/tmp; then
		tmpdir=/var/tmp
	elif is_good_fs /root/tmp; then
		tmpdir=/root/tmp
	else
		echo "Cannot find a suitable tmp directory" >&2
		exit 1
	fi
fi

# fix the root directory to be uniform in "/-before" and "/-after"
rootdir=`echo $rootdir | sed 's,/$,,'`
rootdir="$rootdir/"
[ -n "$verbose" ] && echo "+ Using $rootdir as root directory."

[ -n "$verbose" ] && echo "+ Using $tmpdir as temporary directory."
if [ -z "$target" ]; then
	usage
fi

### create the temporary directories for us to use
echo "* Creating temporary directories..." >&2
do_cmd "rm -rf $tmpdir/mklivecd.*"
CLOOPDIR=`mktemp -d ${tmpdir}/mklivecd.cloop.XXXXXX`
LIVECDDIR=`mktemp -d ${tmpdir}/mklivecd.livecd.XXXXXX`
INITRDDIR=`mktemp -d ${tmpdir}/mklivecd.initrd.XXXXXX`
INITRDMNT=`mktemp -d ${tmpdir}/mklivecd.initrd.mnt.XXXXXX`
EXCLUDELIST=`mktemp ${tmpdir}/mklivecd.excludes.XXXXXX`
TMPISO=`mktemp ${tmpdir}/mklivecd.iso.XXXXXX`
MODULESDIR=/lib/modules/$kernel
ISOLINUXDIR=$LIVECDDIR/isolinux
INITRD=$ISOLINUXDIR/initrd
RCFILE=$INITRDDIR/linuxrc
LIVECDCLOOPDIR=$LIVECDDIR

### cleanup on exit, hangup, interrupt, quit, termination
trap 'rm -rf $CLOOPDIR $LIVECDDIR $INITRDDIR $INITRDMNT $EXCLUDELIST $TMPISO' 0 1 2 3 15

### create directories
if [ -z "$LIVECDDIR" -o -z "$INITRDDIR" -o -z "$INITRDMNT" -o -z "$CLOOPDIR" ]; then
    echo "ERROR: Unable to create temporary files and/or directories." >&2
    exit 1
fi
do_cmd "mkdir -p $LIVECDDIR"
do_cmd "mkdir -p $ISOLINUXDIR"
do_cmd "mkdir -p $LIVECDCLOOPDIR"
do_cmd "mkdir -p $CLOOPDIR"

check_theme
create_initrdimg
create_cloop
create_isolinux
restore_theme
create_liveiso
