#!/bin/sh
# Maritime CLI installer: curl -fsSL https://maritime.sh/install.sh | sh
#
# Installs the maritime binary for this computer from the npm registry
# (package @maritimesh/cli-<os>-<arch>), checks its sha512, and puts it in
# ~/.maritime/bin. At a terminal it then runs 'maritime onboard': sign in,
# get an API key, and start the free trial. Agents and CI get the next
# command printed instead.
#
# Settings (environment):
#   MARITIME_INSTALL_DIR   where the binary goes (default ~/.maritime/bin)
#   MARITIME_VERSION       a version to install (default: latest)
#   MARITIME_NO_ONBOARD=1  install only; do not run onboarding
set -eu

REGISTRY="https://registry.npmjs.org"
INSTALL_DIR="${MARITIME_INSTALL_DIR:-$HOME/.maritime/bin}"
VERSION="${MARITIME_VERSION:-latest}"
PATH_LINE="export PATH=\"$INSTALL_DIR:\$PATH\""

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

need() {
  command -v "$1" >/dev/null 2>&1 || fail "this installer needs '$1'. Install it, or run: npm install -g maritime-cli"
}

platform() {
  case "$(uname -s)" in
    Darwin) os=darwin ;;
    Linux) os=linux ;;
    *) fail "unsupported system $(uname -s). On Windows run: npm install -g maritime-cli" ;;
  esac
  case "$(uname -m)" in
    arm64 | aarch64) arch=arm64 ;;
    x86_64 | amd64) arch=x64 ;;
    *) fail "unsupported processor $(uname -m)" ;;
  esac
  printf '@maritimesh/cli-%s-%s' "$os" "$arch"
}

# json_field NAME: the first "NAME":"value" string in the JSON on stdin.
json_field() {
  tr ',' '\n' | sed -n "s/.*\"$1\": *\"\\([^\"]*\\)\".*/\\1/p" | head -n 1
}

sha512_base64() {
  if command -v openssl >/dev/null 2>&1; then
    openssl dgst -sha512 -binary "$1" | openssl base64 -A
  elif command -v sha512sum >/dev/null 2>&1; then
    sha512sum "$1" | cut -d ' ' -f 1 | xxd -r -p | base64 | tr -d '\n'
  else
    fail "this installer needs openssl or sha512sum to check the download"
  fi
}

add_to_path() {
  case ":$PATH:" in *":$INSTALL_DIR:"*) return ;; esac
  for rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile"; do
    if [ -f "$rc" ] && ! grep -qF "$INSTALL_DIR" "$rc"; then
      printf '\n# Maritime CLI\n%s\n' "$PATH_LINE" >>"$rc"
      say "Added $INSTALL_DIR to PATH in $rc"
    fi
  done
}

need curl
need tar
package="$(platform)"
metadata="$(curl -fsSL "$REGISTRY/$package/$VERSION")" || fail "could not reach the npm registry for $package"
tarball="$(printf '%s' "$metadata" | json_field tarball)"
integrity="$(printf '%s' "$metadata" | json_field integrity)"
version="$(printf '%s' "$metadata" | json_field version)"
[ -n "$tarball" ] && [ -n "$integrity" ] || fail "the npm registry answered without a download for $package"

work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT INT TERM
say "Downloading maritime $version for $package..."
curl -fsSL -o "$work/package.tgz" "$tarball" || fail "download failed"
[ "sha512-$(sha512_base64 "$work/package.tgz")" = "$integrity" ] || fail "the download does not match its checksum; nothing was installed"
tar -xzf "$work/package.tgz" -C "$work"
mkdir -p "$INSTALL_DIR"
mv "$work/package/bin/maritime" "$INSTALL_DIR/maritime"
chmod 755 "$INSTALL_DIR/maritime"
add_to_path
say "Installed $INSTALL_DIR/maritime ($("$INSTALL_DIR/maritime" --version 2>/dev/null || printf '%s' "$version"))"

if [ "${MARITIME_NO_ONBOARD:-}" = "1" ] || [ -n "${CI:-}" ] || ! { [ -r /dev/tty ] && [ -t 1 ]; } 2>/dev/null; then
  say ""
  say "Next: run 'maritime onboard' (agents: 'maritime onboard --json'; guide: https://maritime.sh/start.md)"
  exit 0
fi
say ""
"$INSTALL_DIR/maritime" onboard </dev/tty
say ""
say "Open a new terminal, or run: $PATH_LINE"
