# Maintainer: Andrew Gouin <andrew@gouin.io>
#
# Streaming, resumable, space-efficient extractor for compressed
# archives downloaded over HTTP. Upstream: https://github.com/agouin/peel
#
# This package builds peel from source against Arch's system libzstd
# (via the `system-libs` Cargo feature). Users who want a faster install
# and a self-contained binary should use the `peel-bin` AUR package
# instead.

pkgname=peel
pkgver=0.7.2
pkgrel=1
pkgdesc='Streaming, resumable, space-efficient HTTP archive extractor'
arch=('x86_64' 'aarch64')
url='https://github.com/agouin/peel'
license=('MIT' 'Apache-2.0')
depends=('zstd' 'gcc-libs')
makedepends=('rust' 'cargo' 'pkgconf')
provides=("peel=${pkgver}")
conflicts=('peel-bin')
# Opt out of makepkg's global LTO. Arch's default `/etc/makepkg.conf`
# enables `lto` in `OPTIONS`, which appends `-flto=auto` to `CFLAGS`.
# ring 0.17.14's build.rs uses cc-rs to compile its C/asm source for
# `libring_core_0_17_14.a`, and cc-rs picks up `CFLAGS` from the
# environment. With `-flto=auto` set, the resulting archive members
# are LLVM LTO bitcode rather than regular ELF objects. The final
# Rust link step is *not* doing whole-program LTO (the binary is
# linked normally), so the linker treats those bitcode objects as
# opaque and every ring C symbol — `ring_core_0_17_14__*` — comes
# back undefined. Fedora / Debian / Alpine / the Docker image don't
# hit this because their build environments don't push `-flto` into
# `CFLAGS` by default. `!lto` strips the flag from `CFLAGS` /
# `CXXFLAGS` / `LDFLAGS` for this package without touching the rest
# of `/etc/makepkg.conf`.
options=('!lto')
source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz")
# Replace 'SKIP' with the actual sha256 once the v${pkgver} release
# tag exists upstream. The GitHub-released `SHA256SUMS` file in the
# corresponding release has the value; or compute locally with
#   curl -sL ${url}/archive/v${pkgver}.tar.gz | sha256sum
sha256sums=('SKIP')

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

build() {
    cd "${pkgname}-${pkgver}"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    # `system-libs` forwards zstd-sys to pkg-config so we link against
    # the distro's libzstd.so instead of vendoring the C source — Arch
    # convention prefers shared linkage so security updates flow.
    cargo build --frozen --release --features system-libs --bin peel
    # Man page is arch-independent troff; generated by the
    # `peel-mangen` bin gated behind the `man-page` feature
    # (`src/bin/peel-mangen.rs`). Built as a separate bin (not an
    # example) so the build closure stays free of dev-deps.
    cargo build --frozen --release --features system-libs,man-page --bin peel-mangen
    target/release/peel-mangen target/man/peel.1
}

check() {
    cd "${pkgname}-${pkgver}"
    export RUSTUP_TOOLCHAIN=stable
    cargo test --frozen --release --features system-libs
}

package() {
    cd "${pkgname}-${pkgver}"
    install -Dm0755 target/release/peel "${pkgdir}/usr/bin/peel"
    install -Dm0644 target/man/peel.1 "${pkgdir}/usr/share/man/man1/peel.1"
    install -Dm0644 LICENSE-MIT "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE-MIT"
    install -Dm0644 LICENSE-APACHE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE-APACHE"
    install -Dm0644 NOTICE "${pkgdir}/usr/share/licenses/${pkgname}/NOTICE"
}
