#!/bin/sh
# Committed shim for the npm-distributed claudespace (AD-3). Resolves its
# own real location through the npm-created symlink, then execs the bundled
# Nuitka binary with the hardcoded dispatch token - kept out of node's way
# on the steady-state path so a Stop-hook fire costs no interpreter startup
# beyond the binary itself.
set -eu

resolve_link() {
    link="$1"
    while [ -L "$link" ]; do
        target=$(readlink "$link")
        case "$target" in
            /*) link="$target" ;;
            *) link="$(dirname "$link")/$target" ;;
        esac
    done
    printf '%s\n' "$link"
}

SELF="$(resolve_link "$0")"
BIN_DIR="$(cd "$(dirname "$SELF")" && pwd)"
PKG_ROOT="$(cd "$BIN_DIR/.." && pwd)"

case "$(uname -s)" in
    MINGW*|MSYS*|CYGWIN*)
        # Postinstall replaces npm's Windows wrappers with ones that run the
        # native build directly, so this only runs if it didn't (e.g.
        # --ignore-scripts). The x64 build runs on x64 and ARM64 Windows.
        BINARY="$PKG_ROOT/libexec/win32-x64/claudespace.dist/claudespace-bin.exe"
        [ -e "$BINARY" ] || BINARY="$PKG_ROOT/libexec/win32-arm64/claudespace.dist/claudespace-bin.exe"
        ;;
    *)
        BINARY="$PKG_ROOT/libexec/darwin-arm64/claudespace.dist/claudespace-bin"
        ;;
esac

exec "$BINARY" ask "$@"
