#!/bin/sh
# MasiDyn CLI installer (POSIX). Fetches the picker + client and puts `masidyn`
# on your PATH. It never automates credentials or runs privileged operations.
#
#   curl -fsSL https://masidyn.dev/install.sh | sh
#
# Env overrides:
#   MASIDYN_HOME   install dir           (default: $HOME/.masidyn)
#   MASIDYN_BIN    launcher dir on PATH  (default: $HOME/.local/bin)
#   MASIDYN_ORIGIN download origin       (default: https://masidyn.dev)
set -eu

ORIGIN="${MASIDYN_ORIGIN:-https://masidyn.dev}"
HOME_DIR="${MASIDYN_HOME:-$HOME/.masidyn}"
BIN_DIR="${MASIDYN_BIN:-$HOME/.local/bin}"

say() { printf '  %s\n' "$*"; }
die() { printf 'error: %s\n' "$*" >&2; exit 1; }

# 1. Require Python 3 (the runtime, CLI, and client are stdlib-only Python).
PY=""
for c in python3 python py; do
  if command -v "$c" >/dev/null 2>&1; then PY="$c"; break; fi
done
[ -n "$PY" ] || die "Python 3 is required but was not found. Install Python 3, then re-run."
say "using python: $($PY --version 2>&1)"

# 2. Pick a downloader.
fetch() { # fetch <url> <dest>
  if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2"
  elif command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1"
  else die "need curl or wget to download files"; fi
}

WEIGHTS_DIR="${MASIDYN_WEIGHTS_DIR:-$HOME_DIR/weights}"
mkdir -p "$HOME_DIR" "$BIN_DIR" "$WEIGHTS_DIR"

# 3. Download the picker (the CLI), hardware-tier classifier, base-weights manifest, and Python client.
say "downloading masidyn CLI + hardware-tier router into $HOME_DIR"
fetch "$ORIGIN/clients/masidyn_pick.py"             "$HOME_DIR/masidyn_pick.py"
fetch "$ORIGIN/clients/masidyn_client.py"           "$HOME_DIR/masidyn_client.py"
fetch "$ORIGIN/clients/masidyn_hardware_tier.py"    "$HOME_DIR/masidyn_hardware_tier.py"
fetch "$ORIGIN/releases/base_weights_manifest.json" "$WEIGHTS_DIR/base_weights_manifest.json"

# 4. Write the launcher shim.
LAUNCHER="$BIN_DIR/masidyn"
cat > "$LAUNCHER" <<EOF
#!/bin/sh
# MasiDyn launcher (generated by install.sh)
MASIDYN_HOME="\${MASIDYN_HOME:-$HOME_DIR}"
PY="$PY"
cmd="\${1:-pick}"
case "\$cmd" in
  pick|"") shift 2>/dev/null || true; exec "\$PY" "\$MASIDYN_HOME/masidyn_pick.py" "\$@" ;;
  *)       exec "\$PY" "\$MASIDYN_HOME/masidyn_pick.py" "\$@" ;;
esac
EOF
chmod +x "$LAUNCHER"
say "installed launcher: $LAUNCHER"

# 4b. If running inside Chromebook Crostini (penguin), also install `masidyn-penguin` and the penguin.linux.test:11460 bridge.
if [ -f /dev/wl0 ] || [ -f /etc/apt/sources.list.d/cros.list ] || [ "$(hostname 2>/dev/null || true)" = "penguin" ]; then
  say "detected Chromebook Crostini (penguin) container — installing masidyn-penguin + ChromeOS bridge"
  fetch "$ORIGIN/install-chromebook.sh" "$HOME_DIR/install-chromebook.sh" 2>/dev/null && sh "$HOME_DIR/install-chromebook.sh" || cp "$LAUNCHER" "$BIN_DIR/masidyn-penguin"
fi

# 5. PATH hint.
case ":$PATH:" in
  *":$BIN_DIR:"*) : ;;
  *) say "NOTE: add $BIN_DIR to your PATH:"
     say "  export PATH=\"$BIN_DIR:\$PATH\"" ;;
esac

printf '\n  MasiDyn CLI installed. Try:  masidyn pick\n'
printf '  (Auto-discovers local MasiDyn via .masidyn/runtime/ports/beam-control.json Port-0 handshake.)\n\n'
