#!/bin/sh

set -x -e

tmpdir=`pwd`/kernel-packages.$$

kernel_src_tree=$1
kernel_version=$2
package_version=ipkg2

if [ -z "$kernel_version" -o ! -d "$kernel_src_tree" ]; then
	echo "Usage: $0 <kernel_src_tree> <kernel_version>"
	exit 1
fi

mkdir -p $tmpdir

pushd $tmpdir

km_ipkg_src=$tmpdir/kernel-modules-$kernel_version

mkdir $km_ipkg_src
pushd $km_ipkg_src

mkdir DEBIAN

cat <<EOF | sed "s/@@KERNELVERSION@@/$kernel_version/g; s/@@PACKAGEVERSION@@/$package_version/;" > DEBIAN/control
Package: kernel-modules-@@KERNELVERSION@@
Essential: yes
Priority: required
Version: @@PACKAGEVERSION@@
Architecture: arm
Maintainer: Alexander Guy <a7r@andern.org>
Depends: modutils
Description: kernel modules for kernel @@KERNELVERSION@@
  This package contains almost all of the kernel modules that might be
  useful within familiar. Eventually I would like to split many of
  these modules off into their own packages that would have dependency
  links from the programs that need them. For example, e2fsprogs could
  depend on e2fs-kernel-modules or something like that. I have already
  split off cpu-scale and the pcmcia modules into their own packages.
EOF

mod_base=lib/modules/$kernel_version


#
# Pull the modules out of the kernel.
#
pushd $kernel_src_tree 

INSTALL_MOD_PATH=$km_ipkg_src
export INSTALL_MOD_PATH

make modules_install

popd

#
# Clean up kernel-modules
# 

module_source=$km_ipkg_src/$mod_base

cd $module_source

rm build

for file in modules.*
do
    rm $file
    ln -s /var/run/$file $file
done

popd

#
# Setup pcmcia-modules
#

pm_ipkg_src=$tmpdir/pcmcia-modules-$kernel_version
mkdir $pm_ipkg_src

pushd $pm_ipkg_src

mkdir DEBIAN

cat <<EOF | sed "s/@@KERNELVERSION@@/$kernel_version/g; s/@@PACKAGEVERSION@@/$package_version/;" > DEBIAN/control
Package: pcmcia-modules-@@KERNELVERSION@@
Priority: extra
Maintainer: Alexander Guy <a7r@andern.org>
Depends: kernel-modules-@@KERNELVERSION@@, pcmcia-cs
Version: @@PACKAGEVERSION@@
Architecture: arm
Description: PCMCIA kernel modules
 The full collection of PCMCIA kernel modules from pcmcia-cs. Install
 this if you will be plugging any Compact Flash or PCMCIA cards into
 your computer.
 .
 We may want to split these up into many fine-grained packages.
EOF

mkdir -p $mod_base

cd $mod_base

mv $module_source/pcmcia .

mkdir -p kernel/drivers/{char,net,ide}

cd kernel/drivers

tmp=$module_source/kernel/drivers

mv $tmp/pcmcia .
mv $tmp/char/pcmcia char/.
mv $tmp/net/pcmcia net/.
mv $tmp/ide/ide-cs.o ide/.

popd

#
# Setup irda-modules
#

im_ipkg_src=$tmpdir/irda-modules-$kernel_version
mkdir -p $im_ipkg_src
pushd $im_ipkg_src

mkdir DEBIAN

cat <<EOF | sed "s/@@KERNELVERSION@@/$kernel_version/g; s/@@PACKAGEVERSION@@/$package_version/;" > DEBIAN/control
Package: irda-modules-@@KERNELVERSION@@
Priority: extra
Maintainer: Alexander Guy <a7r@andern.org>
Depends: kernel-modules-@@KERNELVERSION@@
Version: @@PACKAGEVERSION@@
Architecture: arm
Description: IrDA kernel modules
  The entire suite of kernel modules required to use both SIR and FIR
  IrDA.  This includes IrCOMM, and IrLAN support.
EOF


mkdir -p $mod_base
cd $mod_base

tmp=kernel/drivers/net
mkdir -p $tmp
pushd $tmp
mv $module_source/$tmp/irda .
popd

tmp=kernel/net
mkdir -p $tmp
pushd $tmp
mv $module_source/$tmp/irda .
popd

popd

popd

ipkg-build $km_ipkg_src .
ipkg-build $pm_ipkg_src .
ipkg-build $im_ipkg_src .


rm -rf $tmpdir

