#!/bin/sh
#
# Copyright (c) 2019-2021 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#
# User notes
# ----------
#
# The Linux kernel is built without modifications as if `make defconfig` was
# used.
#
# You can provide your own kernel configuration with the CONFIG option which
# should point to your custom file.
#

PKGNAME=linux
PKGVERSION=5.15.2
PKGREVISION=1
PKGLICENSE="GPL-2.0-only"
PKGSUMMARY="the Linux kernel"
PKGDOWNLOAD="https://cdn.kernel.org/pub/linux/kernel/v5.x/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="bison:build elfutils:build flex:build"
PKGOPTIONS="CONFIG"
PKGSPLIT="linux-headers"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${CONFIG:=}   # Note: path to kernel config file.

target=$(vpk arch)

case $target in
aarch64)
	arch=arm64 ;;
*)
	arch=$(uname -m) ;;
esac

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar -xvf $PKGNAME-$PKGVERSION.tar.xz
	cd $PKGNAME-$PKGVERSION

	if [ -n "$CONFIG" ]; then
		cp $CONFIG .config
	else
		cp ../config-$target .config
	fi

	mkdir -p $DESTDIR/boot
	mkdir -p $DESTDIR/lib
	#make \
	#	CC="$CC" \
	#	HOSTCC="$CC" \
	#	CFLAGS="$CFLAGS" \
	#	INSTALL_PATH=$DESTDIR/boot \
	#	INSTALL_MOD_PATH=$DESTDIR \
	#	LLVM=1 \
	#	ARCH=$arch \
	#	all install modules_install

	# linux-headers split.
	mkdir -p $DESTDIR-linux-headers/include
	make \
		CC="$CC" \
		HOSTCC="$CC" \
		LLVM=1 \
		ARCH=$arch \
		INSTALL_HDR_PATH=$DESTDIR-linux-headers \
		headers_install

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
