# print the available options
help:
    just -l

local_build_dir := "_build-local"
local_prefix := env_var('HOME') / ".local"

# Build and install Pully as a native (non-Flatpak) binary under ~/.local
local-install:
    #!/usr/bin/env sh
    set -eu
    if [ ! -d {{local_build_dir}} ]; then
        meson setup {{local_build_dir}} --prefix={{local_prefix}} -Dprofile=development
    fi
    meson install -C {{local_build_dir}}

# Run the natively installed binary (run `just local-install` first)
local-run:
    {{local_prefix}}/bin/pully

# Kill any running instance, rebuild, and relaunch in the background — for quick UI iteration
local-reload:
    #!/usr/bin/env sh
    set -eu
    pkill -f '{{local_prefix}}/bin/pully$' 2>/dev/null || true
    just local-install
    nohup {{local_prefix}}/bin/pully > /tmp/pully.log 2>&1 &
    disown
    sleep 1
    pgrep -af '{{local_prefix}}/bin/pully$' || echo "pully did not start — check /tmp/pully.log"

# Uninstall the native binary and remove its build directory
local-clean:
    #!/usr/bin/env sh
    set -eu
    if [ -d {{local_build_dir}} ]; then
        ninja -C {{local_build_dir}} uninstall
    fi
    rm -rf {{local_build_dir}}

# Freshly build and install Flatpak on machine (release profile)
install: flathub-update-cargo-dependencies
    flatpak-builder --disable-rofiles-fuse --user --force-clean --install build-dir page.codeberg.petsoi.pully.json

# Freshly build and install Flatpak on machine (Devel profile, for local testing)
install-devel: flathub-update-cargo-dependencies
    flatpak-builder --disable-rofiles-fuse --user --force-clean --install build-dir page.codeberg.petsoi.pully.devel.json

# Run program inside Flatpak (build-dir must already exist — see `just install`).
# Uses `flatpak build` directly instead of `flatpak-builder --run`: the latter
# always goes through rofiles-fuse, which can't tear itself down inside a
# rootless Podman toolbox and fails with "Failure spawning rofiles-fuse".
run:
    flatpak build build-dir pully

# Installs Pully local as if it was built for Flathub (release profile, no Devel theme)
flathub-install: flathub-update-cargo-dependencies
    flatpak run org.flatpak.Builder --force-clean --sandbox --user --install --install-deps-from=flathub --ccache --mirror-screenshots-url=https://dl.flathub.org/media/ --repo=repo builddir page.codeberg.petsoi.pully.json

# Executes all linters for Flathub
flathub-lint:
    flatpak run --command=flatpak-builder-lint org.flatpak.Builder manifest page.codeberg.petsoi.pully.json

# Update the flatpak dependencies (needed once vendored builds are required, e.g. for Flathub submission)
flathub-update-cargo-dependencies:
    python3 helper/flatpak-cargo-generator.py Cargo.lock -o cargo-sources.json

VERSION := `grep '^version' Cargo.toml | head -1 | cut -d'"' -f2`

# Extract translatable strings into po/pully.pot (uses `xtr`, not `xgettext` —
# the `tr!()` macro isn't a plain gettext() call xgettext understands)
pot:
    xtr src/main.rs \
        -o po/pully.pot \
        --package-name=pully \
        --package-version={{VERSION}} \
        --copyright-holder="Peter Sonntag" \
        --msgid-bugs-address="https://codeberg.org/petsoi/pully/issues"

# Sync all existing .po files with the updated .pot template
update-po: pot
    #!/usr/bin/env sh
    set -eu
    for po in po/*.po; do
        [ -e "$po" ] || continue
        echo "Updating $po…"
        msgmerge --update --backup=none "$po" po/pully.pot
    done

# Show translation completion stats for all languages
translation-stats:
    #!/usr/bin/env sh
    set -eu
    for po in po/*.po; do
        [ -e "$po" ] || continue
        lang=$(basename "$po" .po)
        printf "%-8s " "$lang"
        msgfmt --statistics "$po" -o /dev/null
    done

GPG_KEY := "6759382E3DC5DC0C"

# Build distributable .flatpak bundle
bundle: flathub-update-cargo-dependencies
    flatpak-builder --disable-rofiles-fuse --force-clean --repo=repo build-dir page.codeberg.petsoi.pully.json
    flatpak build-update-repo repo
    flatpak build-sign repo --gpg-sign={{GPG_KEY}}
    ostree gpg-sign --repo=repo appstream/x86_64 {{GPG_KEY}}
    ostree gpg-sign --repo=repo appstream2/x86_64 {{GPG_KEY}}
    # `--prune --prune-depth=0`: keep only the newest commit of every ref,
    # dropping older versions' objects - otherwise repo/, and the `pages`
    # branch it's pushed to, grow with every publish and are never cleaned.
    flatpak build-update-repo --prune --prune-depth=0 --gpg-sign={{GPG_KEY}} repo
    flatpak build-bundle repo page.codeberg.petsoi.pully.flatpak page.codeberg.petsoi.pully --gpg-sign={{GPG_KEY}}
    @echo "Bundle ready: page.codeberg.petsoi.pully.flatpak"

# Build + publish the signed Flatpak repo to Codeberg Pages (resets `pages` to one commit)
publish: bundle
    #!/usr/bin/env sh
    set -eu
    worktree=/tmp/pully-pages
    cleanup() {
        git worktree remove --force "$worktree" 2>/dev/null || true
        rm -rf "$worktree"
        git worktree prune
        git branch -D pages-fresh >/dev/null 2>&1 || true
    }
    cleanup
    trap cleanup EXIT
    git fetch --quiet origin pages 2>/dev/null || true
    # `pages` serves an OSTree store that only needs the current build.
    # Committing each publish on top keeps every past version's objects in
    # git history forever - even the ones `bundle`'s prune just dropped
    # from repo/. So rebuild `pages` as one orphan commit each publish and
    # force-push it: unchanged objects are already in this clone, so the
    # push is a thin pack of only what changed, and Codeberg's gc reclaims
    # the discarded history.
    git worktree add --quiet "$worktree" pages 2>/dev/null \
        || git worktree add --quiet --detach "$worktree"
    git -C "$worktree" checkout --quiet --orphan pages-fresh
    find "$worktree" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
    cp -r "$PWD/repo/." "$worktree"/
    git -C "$worktree" add -A
    if [ "$(git -C "$worktree" write-tree)" = "$(git rev-parse --verify --quiet 'origin/pages^{tree}' || echo none)" ]; then
        echo "pages already up to date - nothing to push"
        exit 0
    fi
    git -C "$worktree" commit --quiet -m "Update Flatpak repository"
    git -C "$worktree" push --force origin pages-fresh:pages
    git branch --force pages pages-fresh
