url="http://www.kernel.org"
description="OBSOLET LTS version Linux Kernel with virtualbox modules"

packager="tnut <tnut@nutyx.org>"
contributors="Pierre B"

run=(nutyx)

name=kernel-lts
version=4.19.121

INITRD="initrd-${name/*-/}"
KERNEL=${name/-*/}

_version=${version:0:4}

# Virtualbox
vb_version=5.2.40
buildr=137108
_VBARCH=amd64

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

## Download all the sources
#
source=(\
https://libarchive.org/downloads/libarchive-3.3.3.tar.gz
$url/pub/linux/kernel/v4.x/linux-${_version}.tar.xz
$url/pub/linux/kernel/v4.x/patch-$version.xz
http://download.virtualbox.org/virtualbox/${vb_version}/VirtualBox-${vb_version}-${buildr}-Linux_${_VBARCH}.run
config_64)

## Time to build everything
#
prepare() {
tar xf libarchive-3.3.3.tar.gz
cd libarchive-3.3.3
./configure
make
}
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/*)"

# Virtualbox
cd $SRC
# Modules host

sh VirtualBox-${vb_version}-${buildr}-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

../libarchive-3.3.3/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-NuTyX-lts
cp  .config    \
$PKG/boot/config_64-$version-NuTyX-lts
cp  arch/x86_64/boot/bzImage \
$PKG/boot/$KERNEL-$version-NuTyX-lts

# 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

echo \"

********************************************************************
* This kernel-lts version is obsolet, it wont be upgrade anymore.  *
* You should replace this kernel-lts version as soon as possible.  *
* 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,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
}
