# Maintainer: tirith contributors
pkgname=tirith
pkgver=0.3.1
pkgrel=1
pkgdesc='Terminal security - catches homograph attacks, pipe-to-shell, ANSI injection'
arch=('x86_64' 'aarch64')
url='https://github.com/sheeki03/tirith'
license=('AGPL-3.0-only')
depends=('gcc-libs')
makedepends=('cargo' 'base-devel')
options=(!lto)
install=tirith.install
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('988e4a77a495b0a71f823aaef2ea8f115eb1389ce365f24cb06bbd79cf617cf1')

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

build() {
  cd "$pkgname-$pkgver"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  # Force gcc linker — ring (crypto dep) can fail with lld on some setups
  export RUSTFLAGS="-C linker=gcc"
  cargo build --release -p tirith
}

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

  install -Dm755 "target/release/tirith" "$pkgdir/usr/bin/tirith"

  # Shell hooks
  install -Dm644 "shell/tirith.sh" "$pkgdir/usr/share/tirith/shell/tirith.sh"
  install -Dm644 "shell/lib/zsh-hook.zsh" "$pkgdir/usr/share/tirith/shell/lib/zsh-hook.zsh"
  install -Dm644 "shell/lib/bash-hook.bash" "$pkgdir/usr/share/tirith/shell/lib/bash-hook.bash"
  install -Dm644 "shell/lib/fish-hook.fish" "$pkgdir/usr/share/tirith/shell/lib/fish-hook.fish"
  install -Dm644 "shell/lib/powershell-hook.ps1" "$pkgdir/usr/share/tirith/shell/lib/powershell-hook.ps1"
  install -Dm644 "shell/lib/nushell-hook.nu" "$pkgdir/usr/share/tirith/shell/lib/nushell-hook.nu"

  # Completions
  install -Dm644 <(target/release/tirith completions bash) "$pkgdir/usr/share/bash-completion/completions/tirith"
  install -Dm644 <(target/release/tirith completions zsh) "$pkgdir/usr/share/zsh/site-functions/_tirith"
  install -Dm644 <(target/release/tirith completions fish) "$pkgdir/usr/share/fish/vendor_completions.d/tirith.fish"

  # Man page
  install -Dm644 <(target/release/tirith manpage) "$pkgdir/usr/share/man/man1/tirith.1"

  # License
  install -Dm644 LICENSE-AGPL "$pkgdir/usr/share/licenses/$pkgname/LICENSE-AGPL"
}
