#!/bin/sh
# Adam Gobiowski <adamg@pld-linux.org> 2005
# Lukasz Pawelczyk <havner@pld-linux.org> 2005
# $Id: livecd_gen_initrd,v 1.21 2006/08/08 19:58:19 havner Exp $

. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/functions-live

check_root
load_config
check_deps /usr/share/pci-database/ide.pci /boot/memtest86+ /usr/lib/syslinux/isolinux.bin splash.bin
check_deps_local busybox genromfs

cd $INITRD/lib/modules
show "Purging old kernel stuff"; busy
rm -rf 2*
ok

show "Preparing new directory tree"; busy
for I in kernel-grsecurity kernel kernel26 kernel24 kernel-desktop; do
	if rpm --root $ROOTFS -q $I > /dev/null; then
		KERNEL=$I
		break
	fi
done
if [ ! "$KERNEL" ]; then
	echo -e "\nThere is no kernel installed in $ROOTFS"
	fail
	exit 1
fi

VR=`rpm --root $ROOTFS --qf "%{VERSION}-%{RELEASE}" -q $KERNEL`
cd $INITRD/lib/modules
mkdir -p $VR/kernel
cd $VR/kernel
for i in $INITRD_MODULES; do 
	mkdir -p `dirname "$i"`;
done
ok

show "Copying kernel modules"; busy
for i in $INITRD_MODULES; do
	cp -r $ROOTFS/lib/modules/$VR/kernel/$i `dirname "$i"`;
done
find . -name "*.gz" | xargs gunzip
ok

run_cmd "Calculating dependencies" depmod -a -b $INITRD -F $ROOTFS/boot/System.map $VR

show "Copying kernel/memtest/isolinux images"; busy
cp -f $ROOTFS/boot/vmlinuz $CD/boot
cp -f $ROOTFS/boot/memtest86+ $CD/boot/memtest
cp -f $ROOTFS/usr/lib/syslinux/isolinux.bin $CD/boot/isolinux/
ok

show "Copying pci-database files"; busy
rm -f $INITRD/pci/*
cp -f $ROOTS/usr/share/pci-database/ide.pci $ROOTS/usr/share/pci-database/scsi.pci $ROOTS/usr/share/pci-database/sata.pci $INITRD/pci
ok

show "Generating initrd"; busy
rm -f $CD/boot/initrd.gz
genromfs -f $CD/boot/initrd -d $INITRD -V "LiveCD initrd" || (fail; exit 1)
busybox gzip $CD/boot/initrd || (fail; exit 1)
# bootsplash
if [ -r $ROOTFS/etc/sysconfig/bootsplash ] ; then
	. $ROOTFS/etc/sysconfig/bootsplash
fi
if [ "$BOOT_SPLASH" = "yes" ]; then
	if [ -z "$THEME" ]; then
		echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
		echo "Generating bootsplashes skipped." 1>&2
	else
		if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
			for res in $BOOT_SPLASH_RESOLUTIONS; do
				if [ -f "$ROOTFS/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
					$ROOTFS/bin/splash.bin -s -f "$ROOTFS/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> $CD/boot/initrd.gz && \
					[ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
				else
					echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
				fi
			done
		else
			echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
			echo "Not adding bootsplash to initrd." 1>&2
		fi
	fi
fi
ok
