#!/bin/sh
set -eu

# SootSim installer
#
# This installs the SootSim CLI, engine runtime, and background daemon. It
# never uses sudo and never modifies the current project.
#
# Prefer to install it yourself, or don't have npm? Run one of:
#
#   npm install --global sootsim
#   bun add --global sootsim
#   pnpm add --global sootsim
#
# Then finish the guided machine setup:
#
#   sootsim setup
#
# Inspect this entire script without running it:
#
#   curl -fsSL https://sootsim.com/install.sh | less
#
# Automatic installation:
#
#   curl -fsSL https://sootsim.com/install.sh | sh
#
# Installs:
#   CLI:     ${SOOTSIM_CLI_PREFIX:-$HOME/.local}
#   command: ${SOOTSIM_CLI_PREFIX:-$HOME/.local}/bin/sootsim
#   runtime: ${SOOTSIM_HOME:-$HOME/.sootsim}/runtimes
#   daemon:  launchd on macOS, systemd --user on Linux
#
# Options:
#   SOOTSIM_VERSION=0.1.218  install a specific CLI version
#   SOOTSIM_CLI_PREFIX=...   choose the user-writable CLI prefix
#   SOOTSIM_NO_DAEMON=1      skip the background daemon
#   SOOTSIM_NO_MODIFY_PATH=1 do not update your shell profile

SOOTSIM_VERSION="${SOOTSIM_VERSION:-latest}"
CLI_PREFIX="${SOOTSIM_CLI_PREFIX:-$HOME/.local}"
CLI="$CLI_PREFIX/bin/sootsim"

say() {
  printf '%s\n' "$*"
}

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

command_exists() {
  command -v "$1" >/dev/null 2>&1
}

case "$(uname -s)" in
  Darwin)
    PLATFORM="macOS"
    ;;
  Linux)
    PLATFORM="Linux"
    ;;
  *)
    fail "this installer currently supports macOS and Linux"
    ;;
esac

case "$SOOTSIM_VERSION" in
  latest | [0-9]*) ;;
  *) fail "SOOTSIM_VERSION must be 'latest' or a version number" ;;
esac

command_exists node ||
  fail "Node.js is required. Install it from https://nodejs.org and rerun this script."
command_exists npm ||
  fail "npm is required for this installer. See the manual bun and pnpm commands at the top of the script."

say ""
say "SootSim installer"
say ""
say "  platform: $PLATFORM"
say "  version:  $SOOTSIM_VERSION"
say "  CLI:      $CLI_PREFIX"
say "  runtime:  ${SOOTSIM_HOME:-$HOME/.sootsim}/runtimes"
say ""

# Install the npm package without its best-effort postinstall runtime fetch.
# The next command performs that network and filesystem step explicitly.
say "Installing the SootSim CLI from npm..."
SOOTSIM_SKIP_POSTINSTALL=1 npm install \
  --global \
  --prefix "$CLI_PREFIX" \
  "sootsim@$SOOTSIM_VERSION"

[ -x "$CLI" ] || fail "npm completed, but the SootSim CLI was not found at $CLI"

say ""
say "Installing the SootSim engine runtime..."
"$CLI" runtime install

if [ "${SOOTSIM_NO_DAEMON:-0}" = "1" ]; then
  say ""
  say "Skipping the background daemon because SOOTSIM_NO_DAEMON=1."
else
  say ""
  say "Installing the SootSim background daemon..."
  "$CLI" daemon install
fi

add_path_line() {
  profile="$1"
  line="export PATH=\"$CLI_PREFIX/bin:\$PATH\""

  if [ -f "$profile" ] && grep -F "$line" "$profile" >/dev/null 2>&1; then
    return
  fi

  {
    printf '\n'
    printf '# Added by the SootSim installer\n'
    printf '%s\n' "$line"
  } >>"$profile"
  say "Added $CLI_PREFIX/bin to PATH in $profile"
}

case ":$PATH:" in
  *":$CLI_PREFIX/bin:"*) ;;
  *)
    if [ "${SOOTSIM_NO_MODIFY_PATH:-0}" = "1" ]; then
      say ""
      say "$CLI_PREFIX/bin is not currently on PATH. Add it with:"
      say "  export PATH=\"$CLI_PREFIX/bin:\$PATH\""
    else
      case "${SHELL:-}" in
        */zsh) add_path_line "$HOME/.zshrc" ;;
        */bash) add_path_line "$HOME/.bashrc" ;;
        *) add_path_line "$HOME/.profile" ;;
      esac
    fi
    ;;
esac

say ""
say "SootSim is installed."
say ""
"$CLI" version
say ""
say "Open a new terminal, start your React Native dev server, then run:"
say ""
say "  sootsim open 8081"
