# print the available options
help:
    just -l

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

# Build and install Griddle 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/griddle

# 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/griddle$' 2>/dev/null || true
    just local-install
    nohup {{local_prefix}}/bin/griddle > /tmp/griddle.log 2>&1 &
    disown
    sleep 1
    pgrep -af '{{local_prefix}}/bin/griddle$' || echo "griddle did not start — check /tmp/griddle.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 (Devel profile, for local testing)
install: flathub-update-cargo-dependencies
    flatpak-builder --disable-rofiles-fuse --user --force-clean --install build-dir page.codeberg.petsoi.griddle.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 griddle

# Installs Griddle 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.griddle.json

# Executes all linters for Flathub
flathub-lint:
    flatpak run --command=flatpak-builder-lint org.flatpak.Builder manifest page.codeberg.petsoi.griddle.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/griddle.pot (uses `xtr`, not `xgettext` —
# the `tr!()` macro isn't a plain gettext() call xgettext understands)
pot:
    xtr src/main.rs \
        -o po/griddle.pot \
        --package-name=griddle \
        --package-version={{VERSION}} \
        --copyright-holder="Peter Sonntag" \
        --msgid-bugs-address="https://codeberg.org/petsoi/griddle/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/griddle.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
