#!/bin/sh
set -e

# Pane installer - https://runpane.com
# Usage: curl -fsSL https://runpane.com/install.sh | sh
#
# This script downloads Pane via runpane.com, which logs anonymous install
# metrics (OS, architecture, version) - the same fields HTTP already exposes.
# No usernames, hostnames, or other PII collected.
# Read this script first:  curl -fsSL https://runpane.com/install.sh
# Skip logging - install directly from:
#   https://github.com/greenfield-inc/Pane/releases/latest

REPO="greenfield-inc/Pane"
BINARY_NAME="Pane"

get_latest_version() {
  curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" |
    sed -nE 's/.*"tag_name"[[:space:]]*:[[:space:]]*"v?([0-9]+(\.[0-9]+)+)".*/\1/p' |
    head -n 1
}

detect_os() {
  case "$(uname -s)" in
    Linux*)  echo "linux" ;;
    Darwin*) echo "macos" ;;
    MINGW*|MSYS*|CYGWIN*) echo "windows" ;;
    *)       echo "unknown" ;;
  esac
}

detect_arch() {
  case "$(uname -m)" in
    x86_64|amd64)  echo "x64" ;;
    aarch64|arm64) echo "arm64" ;;
    *)             echo "unknown" ;;
  esac
}

main() {
  echo ""
  echo "  Pane - Run any agent. Any OS. Ship faster."
  echo "  https://runpane.com"
  echo ""

  PANE_REF=""
  REF_PARAM=""
  if [ -n "$PANE_REF" ]; then
    mkdir -p "$HOME/.pane" && printf '%s\n' "$PANE_REF" > "$HOME/.pane/attribution_ref" || true
    REF_PARAM="&ref=${PANE_REF}"
  fi

  VERSION=$(get_latest_version)
  if [ -z "$VERSION" ] || ! echo "$VERSION" | grep -Eq '^[0-9]+(\.[0-9]+)+$'; then
    echo "Error: Could not determine latest version."
    echo "Download manually: https://github.com/${REPO}/releases/latest"
    exit 1
  fi

  OS=$(detect_os)
  ARCH=$(detect_arch)

  echo "  Detected: ${OS} ${ARCH}"
  echo "  Version:  ${VERSION}"
  echo ""

  case "${OS}" in
    macos)
      FILENAME="${BINARY_NAME}-${VERSION}-macOS-${ARCH}.dmg"
      URL="https://runpane.com/api/download?platform=macos&arch=${ARCH}&v=${VERSION}&file=${FILENAME}&source=install-script${REF_PARAM}"
      DEST="$HOME/Downloads/${FILENAME}"
      echo "  Downloading ${FILENAME}..."
      curl -fSL --progress-bar -o "$DEST" "$URL"
      echo ""
      echo "  Downloaded to: ${DEST}"
      echo "  Opening installer..."
      open "$DEST"
      echo ""
      echo "  Next step:"
      echo "  Drag Pane.app into Applications."
      echo "  If macOS asks, choose Replace to update your existing install."
      ;;

    linux)
      if command -v dpkg >/dev/null 2>&1; then
        EXT="deb"
        if [ "$ARCH" = "arm64" ]; then
          FILENAME="${BINARY_NAME}-${VERSION}-linux-arm64.deb"
        else
          FILENAME="${BINARY_NAME}-${VERSION}-linux-amd64.deb"
        fi
      else
        EXT="AppImage"
        if [ "$ARCH" = "arm64" ]; then
          FILENAME="${BINARY_NAME}-${VERSION}-linux-arm64.AppImage"
        else
          FILENAME="${BINARY_NAME}-${VERSION}-linux-x86_64.AppImage"
        fi
      fi

      URL="https://runpane.com/api/download?platform=linux&arch=${ARCH}&v=${VERSION}&file=${FILENAME}&source=install-script${REF_PARAM}"
      DEST="/tmp/${FILENAME}"
      echo "  Downloading ${FILENAME}..."
      curl -fSL --progress-bar -o "$DEST" "$URL"
      echo ""

      if [ "$EXT" = "deb" ]; then
        echo "  Installing with dpkg..."
        sudo dpkg -i "$DEST"
        echo "  Installed! Run 'pane' to get started."
      else
        chmod +x "$DEST"
        INSTALL_DIR="$HOME/.local/bin"
        mkdir -p "$INSTALL_DIR"
        mv "$DEST" "$INSTALL_DIR/pane"
        echo "  Installed to: ${INSTALL_DIR}/pane"
        echo "  Make sure ${INSTALL_DIR} is in your PATH."
        echo "  Run 'pane' to get started."
      fi
      ;;

    windows)
      if [ "$ARCH" = "arm64" ]; then
        FILENAME="${BINARY_NAME}-${VERSION}-Windows-arm64.exe"
      else
        FILENAME="${BINARY_NAME}-${VERSION}-Windows-x64.exe"
      fi
      URL="https://runpane.com/api/download?platform=windows&arch=${ARCH}&v=${VERSION}&file=${FILENAME}&source=install-script${REF_PARAM}"
      DEST="$HOME/Downloads/${FILENAME}"
      echo "  Downloading ${FILENAME}..."
      curl -fSL --progress-bar -o "$DEST" "$URL"
      echo ""
      echo "  Downloaded to: ${DEST}"
      echo "  Run the installer to complete setup."
      start "$DEST" 2>/dev/null || echo "  Open ${DEST} to install."
      ;;

    *)
      echo "  Unsupported OS. Download manually:"
      echo "  https://github.com/${REPO}/releases/latest"
      exit 1
      ;;
  esac

  echo ""
  echo "  Questions? https://runpane.com"
  echo "  Discord:   https://discord.gg/BdMyubeAZn"
  echo ""
}

main
