# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgname=openpgp-card-tools
pkgver=%version%
pkgrel=%release%
pkgdesc="CLI tool to inspect, manage and use OpenPGP cards"
arch=(x86_64)
url="https://codeberg.org/openpgp-card/openpgp-card-tools"
license=('Apache-2.0 OR MIT')
depends=(
  ccid
  gcc-libs
  glibc
  gmp
)
makedepends=(
  bzip2
  cargo
  clang
  nettle
  pcsclite
)
source=($pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz)
sha512sums=('261c48254b91cbe975eca34d84e7e07fc4d168a08f063ad8e9a595d5a6321757c47ba36ddcd563d133ba82f48fda41877d291ca3be541df240733a988a221a34')
b2sums=('9eabc4272731e91e14b15b6bd6e7dbab73dadcb19a1d11e8924dbe3673805af9f78ff1c0c494524ba8628fa42f768f4d387a0ec261761c55a4dbd8a04ef66cff')

prepare() {
  cd $pkgname
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd $pkgname
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo build --frozen --release
  OCT_MANPAGE_OUTPUT_DIR="$PWD/../man/" cargo run
  OCT_COMPLETION_OUTPUT_DIR="$PWD/../completion/" cargo run
}

package() {
  depends+=(
    bzip2 libbz2.so
    nettle libhogweed.so libnettle.so
    pcsclite libpcsclite.so
  )

  cd $pkgname
  install -vDm 755 target/release/oct -t "$pkgdir/usr/bin/"
  install -vDm 644 {scripting,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 LICENSES/MIT.txt -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 ../man/*.1 -t "$pkgdir/usr/share/man/man1/"
  install -vDm 644 ../completion/oct.bash "$pkgdir/usr/share/bash-completion/completions/oct"
  install -vDm 644 ../completion/_oct -t "$pkgdir/usr/share/zsh/site-functions/"
  install -vDm 644 ../completion/oct.fish -t "$pkgdir/usr/share/fish/vendor_completions.d/"
}
