#!/bin/bash

set -e

#workdir=/home/jonas/src
#buildhost=auryn
#targetmachine=tibook
#newkerneldir=/debian/local/$buildhost/kernels/images
#ccompile=ccompile
#patchvars=

. ./linux.conf

for var in workdir targetmachine newkerneldir; do
	if [ -z "`eval echo '$'$var`" ]; then
		echo "Variable '$var' missing. Exiting...!"
		exit 1
	fi
done

OLDPWD="`pwd`"

cd ${workdir}/linux
export MODULE_LOC="${workdir}/linux-modules"
PATCH_THE_KERNEL=AUTO $patchvars $ccompile fakeroot make-kpkg modules_clean
PATCH_THE_KERNEL=AUTO MAKEFLAGS="$MAKEFLAGS" $patchvars $ccompile fakeroot make-kpkg ${kernelpatches:+--append-to-version "+${kernelpatches//,/+}${kernelpatches_unofficial:++${kernelpatches_unofficial//,/+}}"} modules_image #"
cd ${workdir}
find . -mindepth 1 -maxdepth 1 -type f -name '*.deb' -exec mv '{}' $newkerneldir/$targetmachine ';'
cd "$OLDPWD"


## TODO: Build packages for upstream kernels against headers:
#KSRC=/usr/src/kernel-headers-2.4.21 APPEND_TO_VERSION="-powerpc" KPKG_DEST_DIR="\$(CURDIR)/.." KDREV="0.0.jones1" fakeroot debian/rules kdist_configure kdist_image
#KSRC=/usr/src/kernel-headers-2.4.25 KVERS="2.4.25-powerpc-small" KPKG_DEST_DIR="\$(CURDIR)/.." KDREV="0.0.jones1" fakeroot debian/rules kdist_clean kdist_configure kdist_image
