# Maintainer: Xeonacid <h.dwwwwww@gmail.com>

pkgname=stellar-cli
pkgver=27.1.0
pkgrel=2
pkgdesc='Command-line multi-tool for running and deploying Stellar contracts on the Stellar network'
url='https://stellar.org'
arch=(x86_64)
license=(Apache-2.0)
depends=(glibc libgcc libstdc++ systemd-libs openssl dbus)
makedepends=(git cargo clang)
source=(git+https://github.com/stellar/$pkgname#tag=v$pkgver
        stellar-cli-gcc16-directory-iterator.patch::https://github.com/WebAssembly/binaryen/commit/6a78bf0af57dbff8f9d6a77b2527eb0ebb5b73e9.patch?full_index=1)
sha512sums=('e798c8f290c75e084bb16e5028df312d146b7cc357f3d6119a6a9eb71f6076683e8c48b595b764dc63b19f83e28f8d7f536ee66b442415b9fa0bf2b0348fb277'
            'd9c4a1f387a402fffabb01a7a2a30599aad0693bde6be511175b1a869de91a492bd127b9713f088770e06246a117f5f3b7af7f61e1b510e112b827695e42bdbb')

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

  local _binaryen
  while IFS= read -r -d '' _binaryen; do
    patch -N -p1 -d "$_binaryen" -i "$srcdir/stellar-cli-gcc16-directory-iterator.patch"
  done < <(find "$HOME/.cargo" -type d -path '*/wasm-opt-sys-*/binaryen' -print0)

  mkdir completions
}

build() {
  cd $pkgname
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  CFLAGS+=' -ffat-lto-objects'
  CXXFLAGS+=' -ffat-lto-objects'
  cargo build --frozen --release -p stellar-cli
  local _completion="target/release/stellar completion"
  $_completion --shell bash > "completions/stellar"
  $_completion --shell fish > "completions/stellar.fish"
  $_completion --shell zsh  > "completions/_stellar"
}

package() {
  cd $pkgname
  install -Dm755 "target/release/stellar" -t "$pkgdir/usr/bin/"
  install -Dm644 README.md FULL_HELP_DOCS.md -t "$pkgdir/usr/share/doc/$pkgname"
  install -Dm644 "completions/stellar" -t "$pkgdir/usr/share/bash-completion/completions/"
  install -Dm644 "completions/stellar.fish" -t "$pkgdir/usr/share/fish/vendor_completions.d/"
  install -Dm644 "completions/_stellar" -t "$pkgdir/usr/share/zsh/site-functions/"
}
