#!/usr/bin/bash

if [[ -z $version ]]; then
	echo "no version"
	exit 1
fi
if [[ -z $LOCALNAME ]]; then
	echo "no LOCALNAME"
	exit 1
fi
if [[ -z $ARCH ]]; then
	echo "no ARCH"
	exit 1
fi

HEADERS_INSTALL_DIR="/usr/src/linux-headers-${version}${LOCALNAME}"
MODULES_INSTALL_DIR="/usr/lib/modules/${version}${LOCALNAME}"

set -e

progress() {
	echo "[INSTALL HEADER] $@"
}

SRCARCH=$(bash "$SRCDIR"/autobuild/build-common/get-kernel-arch "$ARCH")

progress "Calling kernel install-extmod-build script to install kernel headers"
# This script wants the following environment variables:
#   srctree, SRCARCH, CC, HOSTCC, MAKE
# SRCARCH is set above and CC is set by Autobuild
srctree="$SRCDIR" HOSTCC="$CC" MAKE=make "$SRCDIR"/scripts/package/install-extmod-build "$PKGDIR"/"$HEADERS_INSTALL_DIR"

progress "Removing stray build/source symlinks as we don't separate those"
rm -rvf "$PKGDIR/${MODULES_INSTALL_DIR}"/{build,source}
ln -sfv "${HEADERS_INSTALL_DIR}" "$PKGDIR/${MODULES_INSTALL_DIR}/build"
ln -sfv "${HEADERS_INSTALL_DIR}" "$PKGDIR/${MODULES_INSTALL_DIR}/source"
