url="https://www.kernel.org"
description="The latest stable kernel version"

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

makedepends=(kmod)
run=(nutyx)

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

name=kernel-54
version=5.4.75

alias=(linux-5.4)
INITRD="initrd"
KERNEL=${name/-*/}

_version=${version:0:3}

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


## Download all the sources
#

source=(\
$url/pub/linux/kernel/v5.x/linux-${_version}.tar.xz config
)

[ -z $patchname ] || source+=" $url/pub/linux/kernel/v5.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 ./.config

# make menuconfig

make

cp .config $SRC/config

## 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.map-${KERNEL_VERSION}
cp  .config    \
$PKG/boot/config-${KERNEL_VERSION}
cp  arch/i386/boot/bzImage \
$PKG/boot/$KERNEL-${KERNEL_VERSION}

# 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 "LK=\".\"
[ -L boot/$name ] && LK=\"\$(readlink boot/$name)\"
if [ \"\$(dirname \$LK)\" != \".\" ]; then
  echo \"This is an efi installation\"
  ln -sf \$LK boot/$name
  if [ -f boot/\$LK ]; then
    mv boot/$KERNEL-${KERNEL_VERSION} boot/\$LK 2>/dev/null
    ln -sf \$LK boot/$KERNEL-${KERNEL_VERSION}
  else
    echo \"EFI Partition not found, mount EFI Partition and re-install the kernel\"
  fi
else
  echo \"This is a bios installation\"
  if [ -L boot/$INITRD ]; then
    mkinitramfs ${KERNEL_VERSION}
    ln -sf initrd-${KERNEL_VERSION} boot/$INITRD
  fi
  ln -sf $KERNEL-${KERNEL_VERSION} boot/$name
fi" > $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
}
