#! /bin/sh
#
# Create an initrd for the specified kernel.

# XXX: Remove temporary files if interrupted.

if [ "$#" -ne "1" ]
then
    echo "Usage: $0 VERSION"
    exit 1
fi

version=$1

if [ ! -f /boot/vmlinuz-$version ]
then
    echo "$0: Could not open /boot/vmlinuz-$version" >&2
    exit 1
fi

if [ ! -f /boot/System.map-$version ]
then
    echo "$0: Could not open /boot/System.map-$version" >&2
    exit 1
fi

KERNEL_VERSION=$(uname -r)
if expr "$KERNEL_VERSION" : "2\.2" > /dev/null 2>&1
then
    blocks=4096
    inodes=2048
elif expr "$KERNEL_VERSION" : "2\.4" > /dev/null 2>&1
then
    blocks=8192
    inodes=4096
else
    echo "$0: Warning: Not updating initrd; don't know what to do with kernel version $KERNEL_VERSION." | fold -s >&2
    exit 0
fi

initrd=/tmp/initrd.$$
initrd_gz=/boot/initrd-$version.gz

if [ ! -f $initrd_gz ]
then
    echo -n "Creating $initrd_gz... "
else
    echo -n "Updating $initrd_gz... "
    mv -f $initrd_gz $initrd_gz.old
fi

depmod -F /boot/System.map-$version -a $version

dd if=/dev/zero of=$initrd bs=1k count=$blocks 2>/dev/null

/sbin/mke2fs -Fq -N $inodes $initrd 2>/dev/null

root=/tmp/initrd-mnt.$$
mkdir $root
mount -o loop $initrd $root

cp /usr/share/discover/linuxrc $root/linuxrc
mkdir -m 755 $root/bin
cp /bin/ash $root/bin/sh
cp /bin/mount $root/bin/mount
cp /bin/umount $root/bin/umount
mkdir -m 755 $root/dev
( cd $root/dev \
    && /sbin/MAKEDEV std console hda hdb hdc hdd sda sdb sdc sdd scd0 scd1 )
mkdir -m 755 $root/etc
touch $root/etc/ld.so.conf
echo "alias block-major-8 sd_mod" > $root/etc/modules.conf
mkdir -m 755 $root/lib
cp /lib/ld-linux.so.2 $root/lib/ld-linux.so.2
cp /lib/libc.so.6 $root/lib/libc.so.6
cp /usr/lib/libdiscover.so.0 $root/lib/libdiscover.so.0
cp /lib/libm.so.6 $root/lib/libm.so.6
rmdir $root/lost+found
mkdir -m 755 $root/proc
mkdir -m 755 $root/sbin
cp /lib/discover/discover $root/sbin/discover
cp /sbin/insmod $root/sbin/insmod
cp /sbin/modprobe $root/sbin/modprobe
mkdir -m 755 $root/share
mkdir -m 755 $root/share/libdiscover0
cp /usr/share/libdiscover0/*.lst $root/share/libdiscover0
mkdir -m 755 $root/tmp
ln -s . $root/usr

# Automatically copy in all of the first stage drivers and all drivers
# they depend on:

# Get a list of the SCSI drivers that discover might detect:
scsi=`awk '{ if (match($2, "(scsi)") \
	&& !match($3, "(ignore|unknown)")) \
	print $3 }' /usr/share/libdiscover0/pci.lst \
    | sort \
    | uniq`

# Copy in the SCSI disk and IDE-SCSI modules as well:
scsi="$scsi sd_mod ide-scsi"

for module in $scsi
do
    awk '! /\\$/ { print $0 } \
	    /\\$/ { printf("%s", substr($0, 1, length($0) - 1)) }' \
	    /lib/modules/$version/modules.dep \
	| grep "^/lib/modules/$version/.*/$module.o" \
	| awk '{ print substr($1, 1, length($1) - 1) ; \
	    for (i = 2; i <= NF; i++) print $i }' \
        | xargs tar cf - 2>/dev/null \
	| ( cd $root && tar xf - )
done

cp /lib/modules/$version/modules.dep $root/lib/modules/$version/modules.dep

umount $root
rmdir $root
gzip -9 < $initrd > $initrd_gz
rm $initrd
echo "done."
