# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_flavor=vanilla
pkgname=linux-${_flavor}
pkgver=4.9.75
case $pkgver in
	*.*.*)	_kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=0
pkgdesc="Linux vanilla kernel"
url="http://kernel.org"
depends="mkinitfs linux-firmware"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
options="!strip"
_config=${config:-config-vanilla.${CARCH}}
install=
source="https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kernver.tar.xz
	0001-HID-apple-fix-Fn-key-Magic-Keyboard-on-bluetooth.patch
	config-vanilla.aarch64
	config-vanilla.armhf
	config-vanilla.x86
	config-vanilla.x86_64
	config-vanilla.ppc
	config-vanilla.ppc64le
	config-vanilla.s390x

	"
if [ "${pkgver%.0}" = "$pkgver" ]; then
	source="$source
	https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz"
fi
subpackages="$pkgname-dev::$CBUILD_ARCH"
arch="all"
license="GPL-2.0"

_abi_release=${pkgver}
_carch=${CARCH}
case "$_carch" in
aarch64*) _carch="arm64" ;;
arm*) _carch="arm" ;;
ppc*) _carch="powerpc" ;;
s390*) _carch="s390" ;;
esac

HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_kernver
	if [ "$_kernver" != "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N || return 1
	fi

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config || return 1
	make -C "$srcdir"/linux-$_kernver O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig
}

# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build || return 1
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine" \
		|| return 1
}

package() {
	cd "$srcdir"/build

	mkdir -p "$pkgdir"/boot "$pkgdir"/lib/modules

	local _install
	case "$CARCH" in
	aarch64*|arm*)	_install="zinstall dtbs_install" ;;
	*)		_install="install" ;;
	esac

	make -j1 modules_install firmware_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_PATH="$pkgdir"/boot \
		INSTALL_DTBS_PATH="$pkgdir"/usr/lib/linux-${_abi_release} \
		|| return 1

	rm -f "$pkgdir"/lib/modules/${_abi_release}/build \
		"$pkgdir"/lib/modules/${_abi_release}/source
	rm -rf "$pkgdir"/lib/firmware

	install -D include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/$_flavor/kernel.release
}

dev() {
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for grsec kernel"
	depends="gmp-dev bash perl"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/$_config "$dir"/.config
	make -j1 -C "$srcdir"/linux-$_kernver O="$dir" ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig prepare modules_prepare scripts

	# needed for 3rd party modules
	# https://bugzilla.kernel.org/show_bug.cgi?id=11143
	case "$CARCH" in
	ppc*) (cd "$dir" && make arch/powerpc/lib/crtsavres.o);;
	esac

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk

	cd "$srcdir"/linux-$_kernver
	find . -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir" || return 1
	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="bf67ff812cc3cb7e5059e82cc5db0d9a7c5637f7ed9a42e4730c715bf7047c81ed3a571225f92a33ef0b6d65f35595bc32d773356646df2627da55e9bc7f1f1a  linux-4.9.tar.xz
5373728be2b507c3db5e042e1d768740df7965078868afdc46418b1adc4cae3d8f9f1aedb59975a0f2acf8754340499354fcf97c503397a5d9886ccc9689b782  0001-HID-apple-fix-Fn-key-Magic-Keyboard-on-bluetooth.patch
cdefa950e81b3e4f810210243393841849adc08050e28f2abdc9ac34ec1421aa54b52e08272f990b3f8b10d1e6b9a307f4732d1d55ec838c5271937449fe9cfc  config-vanilla.aarch64
b74f8be311d63db0740439345b0ef10fa8a7faf147b3702a29276d872ebdbe21cc17947201fe12caf26ecb67f40425599357a58e52a5f4cc6a8d652c7cf02a27  config-vanilla.armhf
c73b3d4cae161d6795b0f9b2bf3cc31530177bb33f69ca6e61b0033dd390206f59781875960199057bfcfcef56993b591da3be69beed9d9e628ec56e00bd89fb  config-vanilla.x86
7724c1777f8072d08ec061aa6ad664399de5405c55d8c0f927a818c431b1f11bd57132267a6220ed89e6d8083208f3d5011e2150346de994f7f141de16e7106c  config-vanilla.x86_64
6ab7c375581d0d4b98aa4c8f52060fc2f3f6ffb2de39cb10504e6d82696ba6d3231ac7c0874a9614878b7b4e262e59610edd822f5be74ca148beabc737574565  config-vanilla.ppc
fdc815e1de1a8d25b45d3c40caeacb768d2930d3bef8a8914d164cf072712bf77a09ba36636838d6055357bcb09ff033f0e06d9c467600eddccb886afa5a8096  config-vanilla.ppc64le
4439818ef7e947614026159e76af56b311a00327f614a69fca96e9c143b3473190a161d7431576987278b95f288cf1a438c2b215b43f503fca2a40e544a54c0f  config-vanilla.s390x
4dbf9b7c6da142b63506542c0f1c5f0f3d4bb22c5291d4d99bcfc3945691ab5f969ce16b1d1a30553ba002feb3de66a9c39c1cf9c51a6c315e8820bc8853d221  patch-4.9.75.xz"
