#!/bin/sh
# install.sh — Install the unified Ralph CLI launcher
#
# Usage: curl -fsSL https://get.ralph.dropback.com/install.sh | sh
#
# Fetches the published manifest, selects the correct launcher binary,
# verifies the SHA-256 checksum, and installs to /usr/local/bin/ralph
# (or ~/.local/bin/ralph if /usr/local/bin is not writable).

set -eu

BINARY_NAME="ralph"
INSTALL_DIR="/usr/local/bin"
DIST_URL="${RALPH_DISTRIBUTION_URL:-https://get.ralph.dropback.com}"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

info() { printf "${GREEN}[info]${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}[warn]${NC} %s\n" "$1"; }
error() { printf "${RED}[error]${NC} %s\n" "$1" >&2; exit 1; }

detect_os() {
  case "$(uname -s)" in
    Darwin*) echo "darwin" ;;
    Linux*)  echo "linux" ;;
    *)       error "Unsupported operating system: $(uname -s)" ;;
  esac
}

detect_arch() {
  case "$(uname -m)" in
    x86_64|amd64)  echo "amd64" ;;
    arm64|aarch64) echo "arm64" ;;
    *)             error "Unsupported architecture: $(uname -m)" ;;
  esac
}

find_downloader() {
  if command -v curl >/dev/null 2>&1; then
    echo "curl"
  elif command -v wget >/dev/null 2>&1; then
    echo "wget"
  else
    error "Neither curl nor wget found. Please install one and retry."
  fi
}

download() {
  url="$1"
  dest="$2"
  downloader=$(find_downloader)

  if [ "$downloader" = "curl" ]; then
    curl -fsSL -o "$dest" "$url"
  else
    wget -qO "$dest" "$url"
  fi
}

resolve_url() {
  path="$1"
  case "$path" in
    http://*|https://*) printf "%s" "$path" ;;
    *) printf "%s/%s" "$(printf "%s" "$DIST_URL" | sed 's#/*$##')" "$(printf "%s" "$path" | sed 's#^/*##')" ;;
  esac
}

extract_top_level_value() {
  key="$1"
  file="$2"
  sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" "$file" | head -1
}

extract_section_value() {
  section="$1"
  key="$2"
  file="$3"
  awk -v section="\"$section\"" -v key="\"$key\"" '
    $0 ~ section"[[:space:]]*:" { in_section=1; next }
    in_section && /}/ { exit }
    in_section && $0 ~ key"[[:space:]]*:" {
      line=$0
      sub(/^.*:[[:space:]]*"/, "", line)
      sub(/".*$/, "", line)
      print line
      exit
    }
  ' "$file"
}

find_checksum() {
  filename="$1"
  checksums_file="$2"
  awk -v target="$filename" '$2 == target { print $1; exit }' "$checksums_file"
}

main() {
  info "Installing Ralph CLI..."

  os=$(detect_os)
  arch=$(detect_arch)
  platform="${os}-${arch}"
  info "Detected platform: ${platform}"

  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT

  manifest_file="${tmpdir}/manifest.json"
  download "$(resolve_url "manifest.json")" "$manifest_file" || error "Failed to download manifest"

  version=$(extract_top_level_value "version" "$manifest_file")
  [ -n "$version" ] || error "Manifest did not contain a version"
  info "Latest version: ${version}"

  launcher_path=$(extract_section_value "launcher" "$platform" "$manifest_file")
  [ -n "$launcher_path" ] || error "Manifest did not contain a launcher for ${platform}"

  checksums_path=$(extract_top_level_value "checksums" "$manifest_file")
  [ -n "$checksums_path" ] || error "Manifest did not contain a checksums path"

  launcher_name=$(basename "$launcher_path")
  launcher_file="${tmpdir}/${launcher_name}"
  checksums_file="${tmpdir}/checksums.txt"

  info "Downloading ${launcher_name}..."
  download "$(resolve_url "$launcher_path")" "$launcher_file" || error "Failed to download launcher"
  download "$(resolve_url "$checksums_path")" "$checksums_file" || error "Failed to download checksums"

  info "Verifying checksum..."
  expected_checksum=$(find_checksum "$launcher_name" "$checksums_file")
  [ -n "$expected_checksum" ] || error "Checksum not found for ${launcher_name}"

  if command -v sha256sum >/dev/null 2>&1; then
    actual_checksum=$(sha256sum "$launcher_file" | awk '{print $1}')
  elif command -v shasum >/dev/null 2>&1; then
    actual_checksum=$(shasum -a 256 "$launcher_file" | awk '{print $1}')
  else
    error "Neither sha256sum nor shasum found — cannot verify binary integrity."
  fi

  [ "$expected_checksum" = "$actual_checksum" ] || error "Checksum mismatch! Expected: ${expected_checksum}, Got: ${actual_checksum}"
  info "Checksum verified"

  chmod +x "$launcher_file"

  if [ -w "$INSTALL_DIR" ] || [ "$(id -u)" -eq 0 ]; then
    cp "$launcher_file" "${INSTALL_DIR}/${BINARY_NAME}"
    info "Installed to ${INSTALL_DIR}/${BINARY_NAME}"
  else
    INSTALL_DIR="${HOME}/.local/bin"
    mkdir -p "$INSTALL_DIR"
    cp "$launcher_file" "${INSTALL_DIR}/${BINARY_NAME}"
    info "Installed to ${INSTALL_DIR}/${BINARY_NAME}"

    case ":$PATH:" in
      *":${INSTALL_DIR}:"*) ;;
      *) warn "Add ${INSTALL_DIR} to your PATH: export PATH=\"\$PATH:${INSTALL_DIR}\"" ;;
    esac
  fi

  info "Ralph CLI ${version} installed successfully!"
  info "Run 'ralph login' to get started."
}

main "$@"
