#!/usr/bin/env bash
# package.sh moved into the pirateface CLI (`pf`, on PyPI). This shim keeps old links working:
# it installs uv if needed and passes the same arguments on.
#
#   curl -fsSL https://pirateface.co/package.sh | bash -s -- --repo author/model ./model-folder
#     = uvx -p 3.13 --from pirateface@latest pf submit --repo author/model ./model-folder
#   ... --hf-cache          = pf seed --hf-cache
#   ... --update-trackers   = pf seed --update-trackers
#
# Every other flag (--revision, --name, --out, --direct, --license, --web-seed, --dry-run, -y) goes to pf submit.
set -euo pipefail
# Braces make bash read the whole script before running it (piped from curl).
{
cmd=submit
for a in "$@"; do case "$a" in --hf-cache|--update-trackers) cmd=seed ;; esac; done
export PATH="${XDG_BIN_HOME:-$HOME/.local/bin}:$HOME/.local/bin:$PATH"
if ! command -v uvx >/dev/null; then
  echo "==> Installing uv (https://docs.astral.sh/uv/), which runs pf with its own Python"
  curl -LsSf https://astral.sh/uv/install.sh | sh || {
    echo "error: could not install uv (no network to astral.sh?). Install uv another way (https://docs.astral.sh/uv/getting-started/installation/)," >&2
    echo "       or run pip install pirateface (Python 3.12 or 3.13) and use: pf $cmd ..." >&2
    exit 1
  }
  command -v uvx >/dev/null || { echo "error: uv is installed but uvx is not on PATH. Open a new terminal and rerun." >&2; exit 1; }
fi
# Old name for the site address.
[ -z "${PIRATEFACE_URL:-}" ] || export PF_API="${PF_API:-$PIRATEFACE_URL}"
pf=(uvx -p 3.13 --from pirateface@latest pf "$cmd" "$@")
# Piped from curl, stdin is this script; prompts must read the keyboard.
rc=0
if { : </dev/tty; } 2>/dev/null; then "${pf[@]}" </dev/tty || rc=$?; else "${pf[@]}" || rc=$?; fi
# uv exits 2 when it cannot download pf (pypi.org) or Python (github.com). pf itself also exits 2 on a usage error.
[ "$rc" -ne 2 ] || echo "note: if uv could not download pf or Python above, this computer needs access to pypi.org and github.com (or: pip install pirateface, then pf $cmd ...)." >&2
exit "$rc"
}
