#!/bin/sh
set -e

# TermWatch CLI installer
# Usage: curl -fsSL https://termwatch.dev/install.sh | sh
#
# SHA256 checksums are verified automatically when available.
# Manual download & verification: https://termwatch.dev/download

BASE_URL="https://termwatch.dev/download"
INSTALL_DIR="/usr/local/bin"

# Detect OS
OS="$(uname -s)"
case "$OS" in
  Linux)  OS="linux" ;;
  Darwin) OS="osx" ;;
  *)
    echo "Error: Unsupported operating system: $OS"
    echo "Please install manually: https://termwatch.dev/download"
    exit 1
    ;;
esac

# Detect architecture
ARCH="$(uname -m)"
case "$ARCH" in
  x86_64|amd64)   ARCH="x64" ;;
  aarch64|arm64)   ARCH="arm64" ;;
  *)
    echo "Error: Unsupported architecture: $ARCH"
    echo "Please install manually: https://termwatch.dev/download"
    exit 1
    ;;
esac

BINARY="termwatch-${OS}-${ARCH}"
URL="${BASE_URL}/${BINARY}"
CHECKSUM_URL="${BASE_URL}/checksums.txt"

echo "Downloading TermWatch for ${OS}-${ARCH}..."

# Download to temp file first
TMP_FILE="$(mktemp)"
if ! curl -fsSL "$URL" -o "$TMP_FILE" 2>/dev/null; then
  rm -f "$TMP_FILE"
  echo "Error: Failed to download from $URL"
  echo ""
  echo "Standalone binaries may not be available yet."
  echo "You can install via .NET instead:"
  echo "  dotnet tool install -g termwatch"
  echo ""
  echo "Or download manually: https://termwatch.dev/download"
  exit 1
fi

# Verify SHA256 checksum
TMP_CHECKSUMS="$(mktemp)"
if curl -fsSL "$CHECKSUM_URL" -o "$TMP_CHECKSUMS" 2>/dev/null; then
  EXPECTED=$(grep "$BINARY" "$TMP_CHECKSUMS" | awk '{print $1}')
  if [ -n "$EXPECTED" ]; then
    if command -v sha256sum >/dev/null 2>&1; then
      ACTUAL=$(sha256sum "$TMP_FILE" | awk '{print $1}')
    elif command -v shasum >/dev/null 2>&1; then
      ACTUAL=$(shasum -a 256 "$TMP_FILE" | awk '{print $1}')
    else
      echo "Warning: No SHA256 tool found, skipping verification."
      ACTUAL="$EXPECTED"
    fi

    if [ "$ACTUAL" != "$EXPECTED" ]; then
      rm -f "$TMP_FILE" "$TMP_CHECKSUMS"
      echo "Error: Checksum verification failed!"
      echo "  Expected: $EXPECTED"
      echo "  Got:      $ACTUAL"
      echo ""
      echo "The downloaded binary does not match the expected checksum."
      echo "Download manually instead: https://termwatch.dev/download"
      exit 1
    fi
    echo "Checksum verified (SHA256)."
  else
    echo "Warning: Binary not found in checksums file, skipping verification."
  fi
  rm -f "$TMP_CHECKSUMS"
else
  echo "Warning: Could not fetch checksums, skipping verification."
fi

# Install
if [ -w "$INSTALL_DIR" ]; then
  mv "$TMP_FILE" "${INSTALL_DIR}/termwatch"
  chmod +x "${INSTALL_DIR}/termwatch"
else
  echo "Installing to ${INSTALL_DIR} (requires sudo)..."
  sudo mv "$TMP_FILE" "${INSTALL_DIR}/termwatch"
  sudo chmod +x "${INSTALL_DIR}/termwatch"
fi

echo ""
echo "TermWatch installed successfully!"
echo ""
echo "Get started:"
echo "  termwatch register    # create your account"
echo "  termwatch init        # generate monitors.yaml"
echo "  termwatch deploy      # start monitoring"
echo ""
echo "Docs: https://termwatch.dev"
