makedepends=(great-os)

description="LTS kernel version"
url="http://www.kernel.org"

packager="Great-OS Team"
maintainer="Great-OS Team"

name=kernel-lts

version=6.6.119
release=4
_version=${version:0:4}

_ARCH=$(uname -m)

[ "${_version: -1}" == "." ] && _version=${version:0:3}

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

if [ "${version: -2}" == ".0" ]; then
  patchname="patch-${version::-2}.xz"
fi

## Download all the sources

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

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

[ -z $patchname ] || source+=" $url/pub/linux/kernel/v6.x/$patchname"

## Time to build everything
#

build_x86_64() {

  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/usr modules_install

  KERNEL_VERSION="${version}-GOS-lts"
  [ -z $patchname ] && KERNEL_VERSION="${version}-GOS-lts"


  # depmod needed
  depmod -b $PKG/usr -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-${KERNEL_VERSION}

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

  mkdir -p $PKG/usr/src

  mv $SRC/linux-${_version} $PKG/usr/src/
  rm -f {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/usr/lib/firmware

  # post-install autogen
echo 'LK="."
[ -L boot/kernel-lts ] && LK="$(readlink boot/kernel-lts)"
if [ "$(dirname $LK)" != "." ]; then
  echo "This is a efi installation"
  ln -sf $LK boot/kernel-lts
  if [ -f boot/$LK ]; then' > $PKGMK_ROOT/$name.post-install
    echo "    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-lts ]; then
    if [ -x usr/bin/dracut ]; then
      dracut --kver ${KERNEL_VERSION} 2>&1 > /dev/null
      ln -sf initramfs-${KERNEL_VERSION} boot/initrd-lts
    else
      mkinitramfs -a -k ${KERNEL_VERSION}
      ln -sf initrd-${KERNEL_VERSION} boot/initrd-lts
    fi
  fi
  ln -sf kernel-${KERNEL_VERSION} boot/kernel-lts

fi" >> $PKGMK_ROOT/$name.post-install
}

devel_x86_64() {

  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/src

}

doc_x86_64() {

  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/share

}

build_other() {
  mkdir -p $PKG/usr
}

build() {
  case ${_ARCH} in
    x86_64)
      build_x86_64
    ;;
    *)
      build_other
    ;;
  esac
}

devel() {
  case ${_ARCH} in
    x86_64)
      devel_x86_64
    ;;
    *)
      echo -e "unsupported yet arch"
    ;;
  esac
}

doc() {
  case ${_ARCH} in
    x86_64)
      doc_x86_64
    ;;
    *)
      echo -e "unsupported yet arch"
    ;;
  esac
}
