#!/bin/sh
# Lukasz Pawelczyk <havner@pld-linux.org> 2004
# $Id: livecd_gen_iso,v 1.28 2006/08/09 13:18:10 havner Exp $

# TODO: locales remove, GL

# Source function library
. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/functions-live

# parameters variables (can be added to ~/.livecd_remaster)
POLDEK_OPTS=

NOSQUASHFS=
FORCE_LOCALES_UPDATE=
FORCE_INITRD=
FORCE_ROOTPASS=
ISO_ONLY=
LEAVE_DOCS=

check_root
load_config
check_deps_local busybox mkisofs mksquashfs

usage() {
	echo "--nosquashfs, -ns		- dont recreate squashfs"
	echo "--locales-update, -lu	- force updating locales inside rootfs (localedb)"
	echo "--initrd, -i		- force updating initrd (through livecd_gen_initrd)"
	echo "--change-root-pass, -cr	- force changing livecd root password"
#	echo "--iso-only, -io		- only create livecd.iso"
	echo "--leave-docs, -ld		- dont remove /usr/share/doc/* and /usr/src/examples/*"
}

while [ "$#" -gt "0" ]; do
	case "$1" in
		--nosquash | -ns )
			NOSQUASHFS=yes; shift ;;
		--locales-update | -lu )
			FORCE_LOCALES_UPDATE=yes; shift ;;
		--initrd | -i )
			FORCE_INITRD=yes; shift ;;
		--change-root-pass | -cr )
			FORCE_ROOTPASS=yes; shift ;;
		--iso-only | -io )
			ISO_ONLY=yes; shift ;;
		--leave-docs | -ld )
			LEAVE_DOCS=yes; shift ;;
		--help )
			usage; exit 1 ;;
		* )
			echo "Unknown parameter"; exit 1 ;;
	esac
done

fix_dirs() {
rmdir $ROOTFS/bin
rmdir $ROOTFS/sbin
rmdir $ROOTFS/lib
rmdir $ROOTFS/usr
rmdir $ROOTFS/boot

mv $SQUASHFS/bin-bind $ROOTFS/bin
mv $SQUASHFS/sbin-bind $ROOTFS/sbin
mv $SQUASHFS/lib-bind $ROOTFS/lib
mv $SQUASHFS/usr-bind $ROOTFS/usr
mv $SQUASHFS/boot-bind $ROOTFS/boot
mv $SQUASHFS/* $ROOTFS/live/

rmdir $SQUASHFS
}

sigint_action() {
fix_dirs
echo -e "\nInterrupted, exiting..."
exit 1
}

# try to fix directory structure
[ -d $SQUASHFS ] && (fix_dirs || exit 1)

# preparation, external scripts
if [ ! -f $CD/boot/initrd.gz -o "$FORCE_INITRD" = "yes" ]; then
	livecd_gen_initrd || exit 1
fi

if [ ! -f $ROOTFS/usr/lib/locale/locale-archive -o "$FORCE_LOCALES_UPDATE" = "yes" ]; then
	show "Generating locales"; busy
	poldek $POLDEK_OPTS --root=$ROOTFS --install localedb-src || (fail; exit 1)
	chroot $ROOTFS localedb-gen || (fail; exit 1)
	rpm --root $ROOTFS -e localedb-src || (fail; exit 1)
	ok
fi

if [ -f $ROOTFS/etc/shadow ]; then
	grep "^root::" $ROOTFS/etc/shadow && NOROOTPASS=yes
else
	grep "^root::" $ROOTFS/etc/passwd && NOROOTPASS=yes
fi
if [ "$NOROOTPASS" = "yes" -o "$FORCE_ROOTPASS" = "yes" ]; then
	show "Set livecd root password"; busy
	echo -en "\nNew password: "
	read ROOTPASS
	echo "root:$ROOTPASS" | chroot $ROOTFS chpasswd
	ok
fi

show "Creating LiveCD symlinks"; busy
cd $ROOTFS
for I in $SYMLINKS; do
	if [ -e "./$I" -a ! -h "./$I" -a ! -f "live/$I" ]; then
		DIR=`dirname $I`
		#FILE=`basename $I`
		DEEP=`echo $DIR | sed "s/[^\/]//g" | wc -c`
		mkdir -p "live/$DIR"
		mv ".$I" "live/$DIR"
		RECURSE=""
		while [ "$((--DEEP))" != "0" ]; do
			RECURSE="../$RECURSE"
		done
		ln -s "${RECURSE}live${I}" ".$I"
	fi
done
ok

if [ ! "$LEAVE_DOCS" = "yes" ]; then
	show "Clearing /usr/share/doc and /usr/src/examples"; busy
	rm -rf $ROOTFS/usr/share/doc/*
	rm -rf $ROOTFS/usr/src/examples/*
	ok
fi

#if [ -x $ROOTFS/usr/local/bin/fix_live ]; then
show "Running several rootfs fixes"; busy
cat > $ROOTFS/tmp/fix_users << EOF
#!/bin/sh
for U in \`ls /home/users\`; do
	for F in \`ls -A /etc/skel\`; do
		[ -f /home/users/\$U/\$F ] || cp -r /etc/skel/\$F /home/users/\$U/
	done
	chown -R \$U:users /home/users/\$U
done

for F in \`ls -A /etc/skel\`; do
	[ -f /root/\$F ] || cp -r /etc/skel/\$F /root
done
chown -R root:root /root
EOF
chmod 755 $ROOTFS/tmp/fix_users
chroot $ROOTFS /tmp/fix_users
rm -f $ROOTFS/tmp/fix_users

chroot $ROOTFS ldconfig
if [ -x "$ROOTFS/etc/rc.d/init.d/vfmg" ]; then
	chroot $ROOTFS /etc/rc.d/init.d/vfmg start >/dev/null 2>&1
	chroot $ROOTFS chkconfig --level 35 vfmg off
fi
rm -f $ROOTFS/var/lib/rpm/__db*
rm -f $ROOTFS/boot/*.old
rm -f $ROOTFS/boot/initrd*
ok
#fi

trap sigint_action INT

mkdir $SQUASHFS

mv $ROOTFS/bin $SQUASHFS/bin-bind
mv $ROOTFS/sbin $SQUASHFS/sbin-bind
mv $ROOTFS/lib $SQUASHFS/lib-bind
mv $ROOTFS/usr $SQUASHFS/usr-bind
mv $ROOTFS/boot $SQUASHFS/boot-bind
mv $ROOTFS/live/* $SQUASHFS/

mkdir $ROOTFS/bin
mkdir $ROOTFS/sbin
mkdir $ROOTFS/lib
mkdir $ROOTFS/usr
mkdir $ROOTFS/boot
 
show "Generating ramdisk.tgz"; busy
rm -f $CD/ramdisk.tgz
cd $ROOTFS
busybox tar -czf $CD/ramdisk.tgz * || (fail; exit 1)
ok

if [ ! "$NOSQUASHFS" = "yes" ]; then
	show "Generating squashfs"; busy
	rm -f $CD/squashfs
	mksquashfs $SQUASHFS/ $CD/squashfs >/dev/null || (fail; exit 1)
	chmod 644 $CD/squashfs
	cd $CD
	md5sum squashfs > $CD/squashfs.md5
	ok
fi

fix_dirs

trap - INT
 
show "Generating livecd.iso"; busy
rm -f $MAINDIR/livecd.iso
cd $CD
mkisofs -J -R -V "$LIVECD_VERSION" -o $ISODIR/livecd.iso \
-b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat \
-no-emul-boot -boot-load-size 4 -boot-info-table -quiet . || (fail; exit 1)
ok
