#!/bin/sh
#
# This script uses mkinitrd from slackware 10
# To create an initrd.gz, and fills it with scsi 
# modules from the TARGET
#
if [ -z "$TARGET" ] ; then
   echo "Missing configuration"
   exit 1 
fi

error_exit()
{
    echo "ERROR: $1"
    exit 1
}

echo Creating INITRD.GZ for SCSI modules

## Mount the TARGET
vcmount $TARGET $TARGET_DEV|| exit 1
CWD=`pwd`

if [ -z "$KERNEL_VERSION" ]; then
    echo Detecting kernel version ...
    # this is a sloopy detection,
    # expecting the vmlinuz links to vmlinuz-VERSION
    cd $TARGET/boot
    [ -h vmlinuz ] || error_exit "no $TARGET/boot/vmlinuz"
    KERNEL=`readlink vmlinuz`
    KERNEL_NAME=`pkgname $KERNEL`
    KERNEL_VERSION=${KERNEL#$KERNEL_NAME-}
fi
echo 
echo "Kernel version=$KERNEL_VERSION"
if [ ! -d $TARGET/lib/modules/$KERNEL_VERSION ]; then
    error_exit "Cannot find $TARGET/lib/modules/$KERNEL_VERSION"
fi
echo "Kernel modules = $TARGET/lib/modules/$KERNEL_VERSION"

## deleting the old tree
echo Making initrd-tree on the HOST ...
rm -rf /boot/initrd-tree
mkinitrd -k $KERNEL_VERSION

## Copying scsi modules from the target
echo Copying modules from the TARGET ...

DFROM=$TARGET/lib/modules/$KERNEL_VERSION/kernel/drivers/scsi
DTO=/boot/initrd-tree/lib/modules/$KERNEL_VERSION

cp -a $DFROM/* $DTO
cp -a $DFROM/aic7xxx/* $DTO

echo Making the final initrd.gz on the HOST ...
mkinitrd &>/dev/null

[ -f /boot/initrd.gz ] || error "The /boot/initrd.gz was not created"

echo The /boot/initrd.gz is ready.
echo It should be packed up by v6-bulk1 later.


