# Klammertext via Apple's `container` — macOS (Apple Silicon) shell wrapper
# -----------------------------------------------------------------------------
# Lets you run Klammertext without typing the full `container run ...` command.
# Install: save this file (e.g. ~/klammertext.zsh) and add to your ~/.zshrc:
#
#     source ~/klammertext.zsh
#
# Then open a new terminal and use `ktext`, `kdesc`, `kdiag` like normal
# commands.  Requires Apple Silicon + macOS 26 or later, with Apple's
# `container` runtime installed and its service started (`container system
# start`).  Install `container` from the signed .pkg at
# https://github.com/apple/container/releases (NOT Homebrew).  See
# doc/install/macos_container_install.md for the full guide.
# -----------------------------------------------------------------------------

# The published image is multi-arch; on Apple Silicon `container` pulls the
# native arm64 build, so no --platform / --rosetta is needed.
KLAMMERTEXT_IMAGE="${KLAMMERTEXT_IMAGE:-akopra/klammertext:latest}"

_klammertext_run() {
  local cmd="$1"; shift
  container run --rm \
    -v "$PWD:/work" -w /work \
    "$KLAMMERTEXT_IMAGE" "$cmd" "$@"
}

# The three Klammertext commands. Files are read from and written to the
# current directory (mounted into the container as /work).
ktext() { _klammertext_run ktext "$@"; }
kdesc() { _klammertext_run kdesc "$@"; }
kdiag() { _klammertext_run kdiag "$@"; }

# Download or update to the latest published image (delete first so the moving
# `latest` tag is definitely refreshed).
klammertext-update() {
  container image delete "$KLAMMERTEXT_IMAGE" 2>/dev/null
  container image pull "$KLAMMERTEXT_IMAGE"
}
