# Maintainer: Peter Jung <ptr1337@cachyos.org>

pkgname=tiny-dfr
pkgver=0.3.2
pkgrel=2
pkgdesc="The most basic Apple Silicon Touch Bar dynamic function row daemon possible"
arch=('x86_64')
url="https://github.com/AsahiLinux/tiny-dfr"
license=(MIT and Apache-2.0)
depends=(
  cairo
  fontconfig
  freetype2
  libinput
  librsvg
  systemd
)
makedepends=(
  cargo
  clang
  lld
  llvm
  pkg-config
)
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/AsahiLinux/tiny-dfr/archive/refs/tags/v${pkgver}.tar.gz")
b2sums=('30d3ce80a6319811c4ba632f2e9cd0cb90b1f633e910b109022345eea2231208e935ac2b2b53ab60e93353e7f87f5c2320d2f2a0ceaf119b8f0f72126cbb16b7')

prepare() {
  cd "${pkgname}-${pkgver}"
  cargo fetch --locked --target "${CARCH}-unknown-linux-gnu"
}

build() {
  cd "${pkgname}-${pkgver}"

  export AR=llvm-ar
  export CC=clang
  export CXX=clang++
  export NM=llvm-nm
  export RANLIB=llvm-ranlib

  export RUSTFLAGS="-Cembed-bitcode -C opt-level=3 -Ccodegen-units=1 -Clinker=clang -C link-arg=-flto -Clink-arg=-fuse-ld=/usr/bin/ld.lld"
  cargo build --release --frozen
}

package() {
  cd "${pkgname}-${pkgver}"

  install -Dm755 "target/release/tiny-dfr" "${pkgdir}/usr/bin/tiny-dfr"

  install -Dm644 "etc/systemd/system/tiny-dfr.service" \
    "${pkgdir}/usr/lib/systemd/system/tiny-dfr.service"
  install -Dm644 "etc/systemd/system/systemd-backlight@backlight:228200000.display-pipe.0.service" \
    "${pkgdir}/usr/lib/systemd/system/systemd-backlight@backlight:228200000.display-pipe.0.service"
  install -Dm644 "etc/systemd/system/systemd-backlight@backlight:appletb_backlight.service" \
    "${pkgdir}/usr/lib/systemd/system/systemd-backlight@backlight:appletb_backlight.service"

  install -Dm644 "etc/udev/rules.d/99-touchbar-seat.rules" \
    "${pkgdir}/usr/lib/udev/rules.d/99-touchbar-seat.rules"
  install -Dm644 "etc/udev/rules.d/99-touchbar-tiny-dfr.rules" \
    "${pkgdir}/usr/lib/udev/rules.d/99-touchbar-tiny-dfr.rules"

  install -d "${pkgdir}/usr/share/tiny-dfr"
  cp -r share/tiny-dfr/. "${pkgdir}/usr/share/tiny-dfr/"

  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

# vim:set sw=2 sts=2 et:
