# Depends on: nutyx

url="http://www.kernel.org"
description="The latest stable kernel version with virtualbox modules"
packager="tnut <tnut@nutyx.org>"

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

name=kernel
alias=(linux)

version=4.19.26
_version=${version:0:4}

run=(nutyx)

# Virtualbox
vb_version=5.2.26
buildrelease=128414
_VBARCH=amd64

## Download all the sources
#

source=(\
$url/pub/linux/kernel/v4.x/linux-${_version}.tar.xz
$url/pub/linux/kernel/v4.x/patch-$version.xz config_64
http://download.virtualbox.org/virtualbox/${vb_version}/VirtualBox-${vb_version}-${buildrelease}-Linux_${_VBARCH}.run
)

## 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="${version}-NuTyX"

# Virtualbox
cd $SRC
# Modules host

sh VirtualBox-${vb_version}-${buildrelease}-Linux_${_VBARCH}.run --target vb --keep --noexec
cd vb
tar xfj VirtualBox.tar.bz2
cd src/vboxhost/

for i in `find . -name Makefile.include.header`
do
  sed -i "s@/lib/modules/@$PKG/lib/modules/@" $i
done

KERN_VER=${KERNEL_VERSION} make

for i in *.ko
do
	install -D -m0644 $i \
	$PKG/lib/modules/${KERNEL_VERSION}/misc/$i
done

# Modules guest
mkdir $SRC/vb-guest
cd    $SRC/vb-guest

bsdtar -xf ../vb/additions/VBoxGuestAdditions.iso
sh  VBoxLinuxAdditions.run --target vb --keep --noexec
cd vb
tar xfj VBoxGuestAdditions-x86.tar.bz2
cd src/vboxguest-${vb_version}

for i in `find . -name Makefile.include.header`
do
  sed -i "s@/lib/modules/@$PKG/lib/modules/@" $i
done

KERN_VER=${KERNEL_VERSION} make all

for i in *.ko
do
	install -D -m0644 $i \
	$PKG/lib/modules/${KERNEL_VERSION}/misc/$i
done


# 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-$version
cp  .config    \
$PKG/boot/config_64-$version
cp  arch/x86_64/boot/bzImage \
$PKG/boot/kernel-$version-NuTyX

# 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/kernel ] && LK="`readlink boot/kernel`"
if [ "`dirname $LK`" != "." ]; then
  echo "This is a efi installation"
  ln -sf $LK boot/kernel
  if [ -f boot/$LK ]; then' > $PKGMK_ROOT/$name.post-install
echo "    mv boot/kernel-$version-NuTyX boot/\$LK 2>/dev/null
    ln -sf \$LK boot/kernel-$version-NuTyX
  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 $version-NuTyX
    ln -sf initrd-$version-NuTyX boot/initrd
  fi
  ln -sf kernel-$version-NuTyX boot/kernel
fi" >> $PKGMK_ROOT/$name.post-install

}
devel() {

cd $PKG
rm -r usr/src/linux-${_version}/{tools,ipc,init,drivers,firmware,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
}
