#!/bin/bash

KERNEL_BOOT_PATH="kernel"

function f() { 
    v=`perl -Iperl-install -Mmodules -e "print qq(\\$_.o\n) foreach modules::module_of_type__4update_kernel(\"$1\")"` 
}

ARCH=`uname -m | sed -e 's/i.86/i386/' -e 's/sparc.*/sparc/'`

f "scsi" ; SCSI_DRIVERS=$v
f "disk" ; DISK_DRIVERS=$v
f "net" ; NETWORK_DRIVERS=$v
f "scsi cdrom" ; CD_DRIVERS=$v
f "pcmcia net_raw cdrom_raw scsi_raw disk_raw" ; PCMCIA_DRIVERS=$v
f "big net_raw cdrom_raw scsi_raw disk_raw" ; BIG_DRIVERS=$v
f "pcmcia net cdrom scsi disk keepbig" ; ALL_DRIVERS=$v
[ $ARCH == "i386" ] && FSMODULES="vfat.o fat.o"

NETWORK_MODULES="  $FSMODULES $NETWORK_DRIVERS"
CDROM_MODULES="    $FSMODULES $CD_DRIVERS"
HD_MODULES="       $FSMODULES $SCSI_DRIVERS $DISK_DRIVERS loop.o isofs.o"
HDREISER_MODULES=" $FSMODULES $SCSI_DRIVERS loop.o isofs.o" # reiserfs.o   <-- currently unsupported in kernel-boot-2.4
PCMCIA_MODULES="   $FSMODULES $PCMCIA_DRIVERS"
BIG_MODULES="      $FSMODULES $BIG_DRIVERS"
ALL_MODULES="      $FSMODULES $ALL_DRIVERS " # reiserfs.o   <-- currently unsupported in kernel-boot-2.4
NOT_USEFULL_IN_STAGE1="nls_*.o parport_probe.o raid*.o serial.o smbfs.o usb-*.o"

PCMCIA_INSTALLMODULES="pcmcia_core.o tcic.o ds.o i82365.o"

if [ "$ARCH" == "i386" ]; then
    #disable any existing resolution!!!
    cp -f $KERNEL_BOOT_PATH/boot/vmlinuz* vmlinuz
    /usr/sbin/rdev -v vmlinuz 65535 #788 #785 

    rm -rf install_pcmcia_modules ; install -d install_pcmcia_modules
else
    cp -f "$KERNEL_BOOT_PATH"/boot/vmlinux.gz .
fi
cp -f $KERNEL_BOOT_PATH/boot/System.map* System.map

rm -rf modules ; install -d modules
(cd modules ; 
    cp -f `find ../"$KERNEL_BOOT_PATH"/lib/modules/ -name "*.o"` .
    /sbin/depmod -F ../System.map -e *.o | perl -pe 's/\\\n//' | perl -ne 's/\.o//g; s/[ \t]+/ /g; print if /: /' > modules.dep
    perl -pi -e 's/((plip|ppa|imm): parport)/$1 parport_pc/' modules.dep
    ls *.o               | packdrake -b9s modules.cz 400000
    ls *.o               | cpio --quiet -H crc -o | bzip2 -9> modules.cpio.bz2
    ../mdk-stage1/mar/mar -c network_modules.mar  $NETWORK_MODULES
    ../mdk-stage1/mar/mar -c cdrom_modules.mar    $CDROM_MODULES
    ../mdk-stage1/mar/mar -c hd_modules.mar       $HD_MODULES
    ../mdk-stage1/mar/mar -c hdreiser_modules.mar $HDREISER_MODULES
    ../mdk-stage1/mar/mar -c pcmcia_modules.mar   $PCMCIA_MODULES
    ../mdk-stage1/mar/mar -c other_modules.mar    $BIG_MODULES
    ../mdk-stage1/mar/mar -c all_modules.mar      $ALL_MODULES
[ "$ARCH" == "i386" ] && {
    cp $PCMCIA_INSTALLMODULES ../install_pcmcia_modules/
}
    rm -f $ALL_MODULES $PCMCIA_INSTALLMODULES $NOT_USEFULL_IN_STAGE1 # leave in the directory non-install1 used modules
)

# v=kernel-2.2.17-BOOT3.tar.bz2
# scp kenobi:~chmou/work/kernel/archive/$v /tmp
# cd ~/gi/kernel ; rm -rf * ; tar xfI /tmp/$v 
# cd ~/gi ; ./update_kernel ; rm -f network.rdz ; ./make_boot_img network.rdz network ; scp vmlinuz network.rdz kenobi:/tftpboot
# cp -f ~/gi/modules/modules.cz /export/Mandrake/mdkinst/lib/modules.cz