# Maintainer: Changwoo Min <changwoo@igalia.com>
# Maintainer: Tejun Heo <tj@kernel.org>
# Maintainer: Peter Jung ptr1337 <admin@ptr1337.dev>
# Maintainer: Piotr Górski <lucjan.lucjanov@gmail.com>

pkgname=scx-scheds
gitname=scx
pkgver=1.1.2.linux.steamos.3
pkgrel=4
pkgdesc="sched_ext schedulers"
arch=('x86_64' 'aarch64')
url="https://gitlab.steamos.cloud/holo/scx/-/tree/v$pkgver"
license=('GPL-2.0-only')
#scx_lavd depends upon bpf so likely will need to be rebuilt anytime we upgrade the kernel
#for example in the switch from 6.18 -> 7.2 kernel that was required
depends=('libelf' 'zlib' 'jq' 'bpf')
makedepends=('git' 'openssh' 'python' 'clang' 'llvm' 'llvm-libs' 'rust' 'cargo' 'upx')
backup=('etc/default/scx' 'etc/systemd/journald@sched-ext.conf')
source=("git+ssh://git@gitlab.steamos.cloud/holo/scx.git#tag=v$pkgver")
sha256sums=('fe5332de5777b3a3185dcfed4cbc55b9459cfd5614bfa9bbbaf732a993cf457a')
options=(!lto)
provides=("scx-scheds=$pkgver")
conflicts=(
  "scx-scheds-git"
)

_backports=(
)

_reverts=(
)

prepare() {
 export RUSTUP_TOOLCHAIN=stable
 cd $gitname

 local _c _l
  for _c in "${_backports[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git cherry-pick --mainline 1 --no-commit "${_c}"
  done
  for _c in "${_reverts[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git revert --mainline 1 --no-commit "${_c}"
  done

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done
}

build() {
  cd $gitname
  cargo build --profile release-tiny  -p scx_lavd
  # Disable upx (the binary compressor) for now since it is broken with makepkg
  # upx --best --lzma  target/release-tiny/scx_lavd
}

package() {
  cd $gitname
  install -Dm644 "services/scx" "${pkgdir}/etc/default/scx"
  install -Dm644 "services/scx.service" "${pkgdir}/usr/lib/systemd/system/scx.service"
  install -Dm755 "target/release-tiny/scx_lavd" "${pkgdir}/usr/bin/scx_lavd"
}

