url="https://www.kernel.org"
description="The 4.14 longterm support Linux Kernel version"

packager="Tnut <tnut@nutyx.org>"

makedepends=(bison flex perl)
run=(nutyx kmod busybox)

PKGMK_IGNORE_UNPACK="yes"
PKGMK_NO_STRIP="yes"
PKGMK_GROUPS=(devel doc)

name=kernel-414
version=4.14.306

alias=(linux-4.14)
INITRD="initrd-${name/*-/}"
KERNEL="${name}"
_version=${version:0:4}

# Linux patch version
[ "${_version}" != "${version}" ] && patchname="patch-$version.xz"

## Download all the sources
#

source=($url/pub/linux/kernel/v4.x/linux-${_version}.tar.xz config_64)

[ -z $patchname ] || source+=" $url/pub/linux/kernel/v4.x/$patchname"
## Time to build everything
#

build(){

bsdtar -p -o -C $SRC -xf linux-${_version}.tar.xz

cd linux-${_version}

if [ -f $SRC/patch-$version.xz ]; then
	xz -d -c  $SRC/patch-$version.xz | patch -Np1
fi

make mrproper
cp $SRC/config_64 ./.config

# make menuconfig

make

cp .config $SRC/config_64

## Modules
#
# Kernel modules
cd $SRC/linux-${_version}
make INSTALL_MOD_PATH=$PKG modules_install

KERNEL_VERSION="$(basename $PKG/lib/modules/*)"

# depmod needed
depmod -b $PKG -a ${KERNEL_VERSION}

# Install config and kernel files
cd $SRC/linux-${_version}

mkdir -p $PKG/boot

cp  System.map \
$PKG/boot/System_64.map-${KERNEL_VERSION}
cp  .config    \
$PKG/boot/config_64-${KERNEL_VERSION}
cp  arch/x86_64/boot/bzImage \
$PKG/boot/$KERNEL

# Reorgenise the sources
cd $PKG/lib/modules/${KERNEL_VERSION}/

mkdir -p $PKG/usr/src

mv $SRC/linux-${_version} $PKG/usr/src/
rm {build,source}

ln -sv /usr/src/linux-${_version} build
ln -sv /usr/src/linux-${_version} source

mkdir $SRC/linux-${_version}
mv $PKG/usr/src/linux-${_version}/Documentation \
$SRC/linux-${_version}

# Firmware are in linux-firmware
rm -rf $PKG/lib/firmware

# post-install autogen
echo "if [ -f boot/$INITRD ]; then
    mkinitramfs ${KERNEL_VERSION}
    rm boot/$INITRD
    mv boot/initrd-${KERNEL_VERSION} boot/$INITRD
fi

echo \"

******************************************************
* This kernel-414 version is obsolet from Jan,2023.  *
* After this date it will be removed from the system.*
* You should replace this version before this date.  *
* See https://www.nutyx.org/en/upgrade-kernel-lts    *
* or                                                 *
* https://www.nutyx.org/fr/upgrade-kernel-lts        *
******************************************************

\"" > $PKGMK_ROOT/$name.post-install

}
devel() {

cd $PKG
rm -r usr/src/linux-${_version}/{tools,ipc,init,drivers,fs,mm,samples,security,sound,virt}
for i in alpha arc arm arm26 arm64 avr32 blackfin c6x \
cris frv h8300 hexagon ia64 m32r m68k m68knommu metag \
mips microblaze mn10300 openrisc parisc powerpc ppc s390 \
score sh sh64 sparc sparc64 tile unicore32 um v850 xtensa
do rm -rf usr/src/linux-${_version}/arch/$i
done

bsdtar -cf \
$PKGMK_PACKAGE_DIR/$name.devel${PKGMK_BUILDVER}any.${PKGMK_PACKAGE_EXT} \
usr/src/linux-${_version}

rm -r usr
}

doc() {

cd $PKG
mkdir -p usr/share/doc
mv $SRC/linux-${_version}/Documentation \
usr/share/doc/linux-$version

bsdtar -cf \
$PKGMK_PACKAGE_DIR/$name.doc${PKGMK_BUILDVER}any.${PKGMK_PACKAGE_EXT} \
usr/share/doc/linux-$version

rm -r usr
}
