#!/bin/sh
# mf, refkit and artgen, run from the toolkit beside this file. The app links
# ~/.local/bin/{mf,refkit,artgen} here through ~/.local/share/monofactory/current
# (desktop/launch.ts), and MonoFactory Beta its own ~/.local/share/monofactory-beta/bin, so the
# name it was called by is the command.
#
# `uv run --project` installs the toolkit editable into an environment under the cache, so what
# runs is always the code in this tree: `uvx --from <dir>` keeps reusing its first build of a
# local directory, however the files under it change. --frozen reads uv.lock without writing it,
# since the tree inside the app is read-only.
set -e
# Plain readlink: `-f` is missing before macOS 12.3. The app's links are absolute.
target=$(readlink "$0" || printf '%s' "$0")
root=$(cd "$(dirname "$target")/../.." && pwd -P)
command -v uv >/dev/null || {
  echo "$(basename "$0"): needs uv. Install it: curl -LsSf https://astral.sh/uv/install.sh | sh" >&2
  exit 127
}
# MonoFactory Beta's plugin carries its marker (desktop/electron-builder.beta.mjs), and its cache
# is its own, as _dirs() in mf_canvas.py has it.
app=monofactory
grep -qs '"channel":"beta"' "$root/identity.json" && app=monofactory-beta
cache=${MONOFACTORY_HOME:+$MONOFACTORY_HOME/cache}
cache=${cache:-${XDG_CACHE_HOME:-$HOME/.cache}/$app}
# Python's bytecode goes to the cache too: written beside the sources it lands inside the app,
# which then no longer matches its signature.
UV_PROJECT_ENVIRONMENT=$cache/venv PYTHONPYCACHEPREFIX=$cache/pycache \
  exec uv run --quiet --frozen --no-dev --project "$root/tools" "$(basename "$0")" "$@"
