# Maintainer: MrDuartePT <gonegrier.duarte@gmail.com>
# Maintainer: johnfanv2 <https://github.com/johnfanv2>

pkgname=lenovolegionlinux
pkgver=0.0.20
pkgrel=1
pkgdesc="LenovoLegionLinux (LLL) brings additional drivers and tools for Lenovo Legion series laptops to Linux. PLEASE READ THE REPO BEFORE INSTALL THIS PACKAGE!!!"
arch=("x86_64")
url="https://github.com/johnfanv2/LenovoLegionLinux"
license=('GPL-2.0-only')

depends=(
  python-argcomplete
  python-yaml
  python-pyqt6
  polkit
  python-darkdetect
)
makedepends=(
  python-build
  python-installer
  python-setuptools
  python-wheel
  libinih
  git
)
optdepends=(
  "lenovolegionlinux-dkms: DKMS module"
)

source=(lenovolegionlinux::git+https://github.com/johnfanv2/LenovoLegionLinux.git)
sha256sums=('SKIP')
install="lenovolegionlinux.install"

pkgver() {
  cd "${srcdir}/${pkgname}"
  git describe --long --tags --abbrev=7 | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd "${pkgname}"
  _pyver="${pkgver/.r/.post}"
  _pyver="${_pyver/.g/+g}"
  sed -i "s/version = _VERSION/version = ${_pyver}/g" "${srcdir}/${pkgname}/python/legion_linux/setup.cfg"
}

build() {
 cd "${srcdir}/${pkgname}/python/legion_linux"
 python -m build --wheel --no-isolation
 # Build legiond daemon
 cd legion_linux/extra/service/legiond
 make
}
package() {
  mkdir -p ${pkgdir}/etc/systemd/system

  cd "${srcdir}/${pkgname}/python/legion_linux"
  python -m installer --destdir="$pkgdir" dist/*.whl

# Systemd service
  cd "${srcdir}/${pkgname}/extra"
  install -Dm664 service/*.service "${pkgdir}/etc/systemd/system"
  install -Dm664 service/*.timer "${pkgdir}/etc/systemd/system"

# legiond daemon
  install -Dm775 service/legiond/legiond "${pkgdir}/usr/bin"
  install -Dm775 service/legiond/legiond-ctl "${pkgdir}/usr/bin"
}
