# Maintainer: funman300 <funman300@gmail.com>

pkgname=solitaire-quest-server
pkgver=0.1.0
pkgrel=1
pkgdesc='Self-hosted sync server for Solitaire Quest (stats, achievements, leaderboards)'
url='https://github.com/funman300/solitaire-quest'
license=('MIT')
arch=('x86_64')
makedepends=('cargo' 'rust')
depends=(
    'gcc-libs'
    'glibc'
)
backup=('etc/solitaire-quest-server/server.env')

# Build from the local workspace (two levels above this PKGBUILD).
_srcdir="$startdir/../.."
source=(
    'solitaire-quest-server.service'
    'server.env'
)
b2sums=('SKIP'
        'SKIP')

prepare() {
    export RUSTUP_TOOLCHAIN=stable
    cd "$_srcdir"
    cargo fetch --locked --target "$(rustc -Vv | grep host | cut -d' ' -f2)"
}

build() {
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    cd "$_srcdir"
    cargo build --frozen --release -p solitaire_server
}

check() {
    export RUSTUP_TOOLCHAIN=stable
    cd "$_srcdir"
    cargo test --frozen -p solitaire_server -p solitaire_sync
}

package() {
    cd "$_srcdir"

    # Binary
    install -Dm0755 -t "$pkgdir/usr/bin/" "target/release/solitaire_server"

    # systemd service
    install -Dm0644 "$srcdir/solitaire-quest-server.service" \
        "$pkgdir/usr/lib/systemd/system/solitaire-quest-server.service"

    # Environment file (contains JWT_SECRET, DATABASE_URL, SERVER_PORT)
    install -Dm0640 "$srcdir/server.env" \
        "$pkgdir/etc/solitaire-quest-server/server.env"

    # License and docs
    install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
    install -Dm0644 README_SERVER.md \
        "$pkgdir/usr/share/doc/$pkgname/README_SERVER.md"
}
