#!/bin/bash

ALL_KERNELS="all.kernels"
RPMS=/export/media/main

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


# move stuff to this new "kernel" directory
if [ ! -d all.kernels ]; then
  if [ -d ../all.kernels ]; then
  mv ../all.kernels .
  rm -rf ../all.modules
  else
    # make sure "all.kernels" directory exists
    mkdir all.kernels
  fi
fi

function create_marfile() {
    marfile=$1
    shift
    $GIBASEDIR/mdk-stage1/mar/mar -c $marfile $*
}

function build_kernel_is_26() { 
    uname -r | grep -q '^2\.6'
}

function kern_version() {
    if echo $1 | grep -q ^2.4; then
        version=24
        objects="*.o"
    else
        version=25
        objects="*.ko"
    fi
}
    
function create_modules() {
    kernel_path=$1
    kern=$2
    echo "Updating modules in '`pwd`' for kernel '$kern'"
    kern_version $kern
    find $kernel_path/lib/modules/ -name "$objects" -exec cp -f {} . \;
    if [ $version -eq 24 ]; then
        /sbin/depmod-24 -F $kernel_path/boot/System.map* -e *.o | perl -pe 's/\\\n//' \
            | perl -ne 's/\.o//g; s/[ \t]+/ /g; print if /: /' > modules.dep
    else
        cp $kernel_path/lib/modules/*/modules.dep .
        perl -ni -e 's|/\S+/([^/]+)\.k?o(\.gz)|$1|g; print if /: /' modules.dep
    fi
    perl -pi -e 's/((plip|ppa|imm): parport)/$1 parport_pc/' modules.dep
    perl -pi -e 's/(vfat: fat)/$1 nls_cp437 nls_iso8859-1/' modules.dep

    if build_kernel_is_26; then
	/sbin/modinfo $objects | perl -lne 'print "$name\t$1" if $name && /^description:\s*(.*)/; $name = $1 if m!^filename:\s*(.*)\.k?o!' > modules.description
    else
	/sbin/modinfo-$version -f '%{filename} %{description}\n' $objects | perl -lne 'print "$1\t$2" if /(.*?)\.k?o "(.*)"/' > modules.description
    fi
}

function create_modules_mar() {
    kern_version $kern

    if [ $version -eq 24 ]; then
        echo -n "stripping $kern: "
        $GIBASEDIR/kernel/strip_modules *.o 2>/dev/null
        echo "done"
    else
        echo "don't strip on 2.6 since it breaks modules"
    fi

    if [ -z "$MOVE" ]; then
        echo -n "packdrake $kern: "
        ls $objects | packdrake -b9s "modules.cz" 400000
        echo "done"
        mv modules.cz ../modules.cz-$kern
    fi
    for i in $images; do
        eval "modules=\$${i}_modules_raw"
        modules_with_deps=`perl -I $GIBASEDIR/kernel $GIBASEDIR/kernel/dependencies.pl $version modules.dep $modules`
	if [ $i = "all" ]; then
	    echo $modules_with_deps > "${i}_modules.list"
	else
	    eval "create_marfile ${i}_modules.mar $modules_with_deps"
	fi
    done
    echo
}

function install_kernel() {
    rpm=$1

    ver=$(rpm --nosignature -qpl $rpm | sed -n 's!/boot/vmlinuz-!!p')
    [ -n "$ver" ] || return
    
    if [ -n "$2" ]; then
        [ -d $ALL_KERNELS/$ver ] && return
        main_ver=$2
	echo Removing previous kernel $ALL_KERNELS/$main_ver*
        rm -rf $ALL_KERNELS/$main_ver*
    fi
    
    rm -rf $ALL_KERNELS/$ver
    echo "Installing kernel $ver"
    mkdir -p $ALL_KERNELS/$ver
    rpm2cpio $rpm | (cd $ALL_KERNELS/$ver ; cpio -id)
    
    find $ALL_KERNELS/$ver -type f -name "*.o.gz" -o -name "*.ko.gz" | xargs gunzip
    
    # will get back when make_boot_img is called
    for dir in /tftpboot /var/lib/tftpboot; do
        rm -f $dir/{vmlinuz,network.rdz,all.rdz}
    done
}

[ -e $ALL_KERNELS/.main ] && main=$(cat $ALL_KERNELS/.main)

if [ "$1" = "--move" ]; then
    MOVE=1
    shift
fi

if [ "$ARCH" == "ia64" ] || [ "$ARCH" == "ppc" ]; then
    name=kernel
else 
    name=kernel-BOOT
fi

if [ -n "$1" ]; then
    #- allow specifying a kernel file on commandline (./update_kernel /RPMS/kernel-2.4.22.10mdk-1-1mdk.i586.rpm)
    install_kernel $1
else
#    install_kernel $RPMS/kernel-BOOT-2.4*.rpm 2.4
    install_kernel $RPMS/kernel-BOOT-2.6*.rpm 2.6
fi

[ -n "$main" -a -e $ALL_KERNELS/$main ] || {
    cd $ALL_KERNELS
    main=$(echo 2.6* | sed 's/.* //')
    [ -e "$main" ] || main=$(echo 2.4* | sed 's/.* //')
    [ -e "$main" ] || { echo "no kernel installed in $ALL_KERNELS"; exit 1; }
    echo "Choosing $main"
    echo $main > .main
    cd ..
}


if [ "$ARCH" == "i386" ]; then
    for i in $ALL_KERNELS/*/boot/vmlinuz*; do
	#disable any existing resolution!!!
	/usr/sbin/rdev -v $i 65535 #788 #785 
    done
fi


GIBASEDIR=`pwd`/..

rm -rf all.modules ; mkdir all.modules
for i in $ALL_KERNELS/*; do
	kern=`basename $i`
	(
	    mkdir all.modules/$kern
	    cd all.modules/$kern

	    create_modules ../../$i $kern
	) || exit 1
done
cp -f all.modules/$main/modules.description .

l=`MOVE=$MOVE perl modules.pl images` || exit 1
eval $l

for i in $ALL_KERNELS/*; do
	kern=`basename $i`
	(
	    cd all.modules/$kern

	    if [ "$kern" = "$main" ]; then
		create_modules_mar ../../$i $kern
	    else
		echo "$kern ($main)"
		create_modules_mar ../../$i $kern 2>/dev/null
	    fi
	) || exit 1
done