#!/bin/sh
# CreatorNotes CLI installer — https://creatornotes.app/install
# Usage: curl -fsSL https://creatornotes.app/install.sh | sh
set -eu

main() {
  REGISTRY="https://registry.npmjs.org"
  INSTALL_DIR="${CNOTES_INSTALL_DIR:-$HOME/.cnotes/bin}"

  # ── Platform detection ────────────────────────────────────
  os="$(uname -s)"
  case "$os" in
    Darwin) os="darwin" ;;
    Linux)  os="linux" ;;
    *)
      err "Unsupported OS: $os. On Windows, install with: npm install -g @creator-notes/cnotes"
      ;;
  esac

  arch="$(uname -m)"
  case "$arch" in
    arm64|aarch64) arch="arm64" ;;
    x86_64|amd64)  arch="x64" ;;
    *)
      err "Unsupported architecture: $arch. Install with: npm install -g @creator-notes/cnotes"
      ;;
  esac

  # Rosetta shells report x86_64 on Apple Silicon — prefer the native binary.
  if [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
    if [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = "1" ]; then
      arch="arm64"
    fi
  fi

  command -v curl >/dev/null 2>&1 || err "curl is required but not found."
  command -v tar  >/dev/null 2>&1 || err "tar is required but not found."

  pkg="@creator-notes/cnotes-$os-$arch"

  # ── Resolve the tarball URL ───────────────────────────────
  if [ -n "${CNOTES_VERSION:-}" ]; then
    tarball="$REGISTRY/$pkg/-/cnotes-$os-$arch-${CNOTES_VERSION}.tgz"
  else
    tarball="$(curl -fsSL "$REGISTRY/$pkg/latest" \
      | sed -n 's/.*"tarball":"\([^"]*\)".*/\1/p' | head -n 1)"
    [ -n "$tarball" ] || err "Could not resolve the latest $pkg release."
  fi

  echo "Installing cnotes ($os-$arch) to $INSTALL_DIR ..."

  # ── Download + extract ────────────────────────────────────
  tmp="$(mktemp -d)"
  trap 'rm -rf "$tmp"' EXIT
  curl -fSL --progress-bar "$tarball" -o "$tmp/cnotes.tgz"
  tar -xzf "$tmp/cnotes.tgz" -C "$tmp"
  [ -f "$tmp/package/bin/cnotes" ] || err "Downloaded archive did not contain the cnotes binary."

  mkdir -p "$INSTALL_DIR"
  # mv over any previous copy; -f so a running old binary doesn't block upgrade.
  mv -f "$tmp/package/bin/cnotes" "$INSTALL_DIR/cnotes"
  chmod +x "$INSTALL_DIR/cnotes"

  installed_version="$("$INSTALL_DIR/cnotes" --version)"
  echo "cnotes $installed_version installed."

  # ── PATH setup (idempotent) ───────────────────────────────
  path_hint=""
  case ":$PATH:" in
    *":$INSTALL_DIR:"*) ;; # already on PATH
    *)
      shell_name="$(basename "${SHELL:-sh}")"
      line="export PATH=\"$INSTALL_DIR:\$PATH\""
      case "$shell_name" in
        zsh)  rc="$HOME/.zshrc" ;;
        bash)
          if [ "$os" = "darwin" ]; then rc="$HOME/.bash_profile"; else rc="$HOME/.bashrc"; fi
          ;;
        fish)
          rc="$HOME/.config/fish/config.fish"
          line="fish_add_path $INSTALL_DIR"
          ;;
        *)    rc="$HOME/.profile" ;;
      esac
      if [ -f "$rc" ] && grep -q "cnotes/bin" "$rc" 2>/dev/null; then
        : # rc already references the install dir from a previous run
      else
        mkdir -p "$(dirname "$rc")"
        printf '\n# Added by the CreatorNotes installer\n%s\n' "$line" >> "$rc"
        echo "Added $INSTALL_DIR to PATH in $rc"
      fi
      path_hint="  export PATH=\"$INSTALL_DIR:\$PATH\"   # or open a new terminal"
      ;;
  esac

  echo ""
  echo "Next steps:"
  if [ -n "$path_hint" ]; then
    echo "$path_hint"
  fi
  echo "  cnotes init"
}

err() {
  echo "install.sh error: $1" >&2
  exit 1
}

main "$@"
