# Maintainer: funman300 <funman300@gmail.com>
pkgname=solitaire-quest
pkgver=0.1.0
pkgrel=1
pkgdesc="Cross-platform Klondike Solitaire in Rust + Bevy with sync, achievements, and four game modes"
arch=('x86_64')
url="https://git.aleshym.co/funman300/Rusty_Solitare"
license=('MIT' 'OFL-1.1')
# MIT for the project code and the hayeah/playing-cards-assets card art it
# bundles; OFL-1.1 for FiraMono. See /usr/share/licenses/$pkgname/CREDITS.md
# on an installed system for the full attribution table.

depends=(
    'alsa-lib'          # audio output via kira
    'libxkbcommon'      # keyboard input on X11/Wayland
    'wayland'           # native Wayland support
    'libx11'            # X11 fallback
    'systemd-libs'      # libudev, gamepad/input device discovery
    'gcc-libs'
    'libsecret'         # keyring crate — sync server credential storage
    'vulkan-icd-loader' # Bevy's wgpu backend
    'fontconfig'        # glyph fallback
)

makedepends=(
    'cargo'
    'rust'
    'pkgconf'
)

# Tarball produced by Gitea's `<repo>/archive/<tag>.tar.gz` endpoint. If the
# project moves to GitHub or another forge, replace the URL.
source=("$pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz")
sha256sums=('SKIP')
# After the v0.1.0 tag is published and the tarball is reachable, run:
#   updpkgsums
# in this directory to replace SKIP with the real sha256 sum, then commit.

# Gitea archives unpack to a directory named after the repo and tag. The
# exact name depends on the Gitea version; check with `tar tf` if unsure.
_archive_dir="Rusty_Solitare"

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

build() {
    cd "$srcdir/$_archive_dir"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    cargo build --frozen --release --package solitaire_app
}

check() {
    cd "$srcdir/$_archive_dir"
    export RUSTUP_TOOLCHAIN=stable
    cargo test --frozen --release --workspace --no-run
    # Skipping `cargo test --release` execution because the workspace is
    # large and tests are independently CI-checked; just verifying the
    # release artefacts compile is sufficient as a packaging gate.
}

package() {
    cd "$srcdir/$_archive_dir"

    # Layout chosen so the binary's parent + "../assets" resolves to the
    # bundled assets directory. Bevy's `current_exe()` follows the
    # /usr/bin symlink to the real binary, so resolution lands inside
    # /usr/lib/$pkgname.
    install -Dm755 "target/release/solitaire_app" \
        "$pkgdir/usr/lib/$pkgname/bin/solitaire-quest"

    install -dm755 "$pkgdir/usr/lib/$pkgname/assets"
    cp -r assets/. "$pkgdir/usr/lib/$pkgname/assets/"

    # /usr/bin entry — symlink so PATH-based launches resolve correctly.
    install -dm755 "$pkgdir/usr/bin"
    ln -s "/usr/lib/$pkgname/bin/solitaire-quest" \
        "$pkgdir/usr/bin/solitaire-quest"

    # Desktop entry + icon for application menus.
    install -Dm644 "$srcdir/solitaire-quest.desktop" \
        "$pkgdir/usr/share/applications/$pkgname.desktop"
    # Icon is sourced from the packaging repo (PKGBUILD's directory),
    # not the upstream archive — drop a 256×256 PNG named
    # solitaire-quest.png next to the PKGBUILD when one becomes available.
    if [ -f "$srcdir/solitaire-quest.png" ]; then
        install -Dm644 "$srcdir/solitaire-quest.png" \
            "$pkgdir/usr/share/icons/hicolor/256x256/apps/$pkgname.png"
    fi

    # License + credit attribution.
    install -Dm644 LICENSE \
        "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
    install -Dm644 CREDITS.md \
        "$pkgdir/usr/share/licenses/$pkgname/CREDITS.md"
    install -Dm644 README.md \
        "$pkgdir/usr/share/doc/$pkgname/README.md"
}
