#!/bin/sh

set -e

debhelper_pre() {
	dh_clean -k --package="$1"
	dh_installdirs --package="$1"
}

debhelper_post() {
	dh_installdocs --package="$1"
	dh_installchangelogs --package="$1"
	dh_compress --package="$1"
	dh_fixperms --package="$1"
	dh_installdeb --package="$1"
	dh_gencontrol --package="$1"
	dh_md5sums --package="$1"
	dh_builddeb --package="$1"
}


suffix=${version#*.*.*-}
prefix=${version%*-*}


pkg=kernel-image-$prefix-$suffix

debhelper_pre $pkg
debhelper_post $pkg


prefix=${version%%-*}
bpkg=kernel-build-$prefix
top=$PWD/../debian/$bpkg

mkdir -p $top/usr/src/$bpkg/$suffix/include/linux
ln -s $bpkg/$suffix $top/usr/src/kernel-headers-$version
cp -a .config $top/usr/src/$bpkg/$suffix
cd include
find . -mindepth 1 -maxdepth 1 \
	! -name config -a ! -name linux \
	-printf "../../../kernel-headers-$prefix/include/%f\n" |
	xargs ln -s --target-directory=$top/usr/src/$bpkg/$suffix/include
cp -a config $top/usr/src/$bpkg/$suffix/include
find linux -mindepth 1 -maxdepth 1 \
	! -name autoconf.h -a ! -name modules -a ! -name modversions.h -a \
	! -name compile.h -a ! -name version.h \
	-printf "../../../../kernel-headers-$prefix/include/linux/%f\n" |
	xargs ln -s --target-directory=$top/usr/src/$bpkg/$suffix/include/linux
cp -a linux/autoconf.h linux/modules linux/modversions.h linux/compile.h \
	linux/version.h $top/usr/src/$bpkg/$suffix/include/linux
cd ..

