#!/bin/bash -ex

getdefaultversion()
{
default=$(grep -v "^ *#" </boot/grub/menu.lst|grep default)
eval $default
index=0
grep -v "^ *#" </boot/grub/menu.lst|egrep "^\s*kernel"|while read i
do
if [ $index -eq $default ]; then
kernelfile=$(echo $i| cut -d" " -f 2)
real=$(/bin/ls -l ${kernelfile})
echo ${real##*vmlinuz-}
break
fi
index=$(( index + 1))
done
}

ver=$(getdefaultversion)
[ ! -n "${ver}" ] && echo "error" && exit 1

rm -rf /boot/work
mkdir /boot/work
cd /boot/work
gzip -dc ../initrd.img | cpio -idmv

find /lib/modules/${ver} -name uhci-hcd.ko -exec cp -pi {} lib \;
find /lib/modules/${ver} -name ohci-hcd.ko -exec cp -pi {} lib \;
find /lib/modules/${ver} -name ehci-hcd.ko -exec cp -pi {} lib \;

patch -p0 -b <<EOT
--- init.orig	2008-01-03 22:20:36.000000000 +0900
+++ init	2008-01-03 23:10:56.000000000 +0900
@@ -5,6 +5,12 @@
 echo Mounted /proc filesystem
 echo Mounting sysfs
 mount -t sysfs none /sys
+echo "Loading uhci-hcd.ko module"
+insmod /lib/uhci-hcd.ko 
+echo "Loading ohci-hcd.ko module"
+insmod /lib/ohci-hcd.ko 
+echo "Loading ehci-hcd.ko module"
+insmod /lib/ehci-hcd.ko 
 echo "Loading scsi_mod.ko module"
 insmod /lib/scsi_mod.ko 
 echo "Loading sd_mod.ko module"
EOT

find | cpio -H newc -o | gzip -9 > ../initrd.img
