#!/bin/sh
# install.sh — download and install texforge from GitHub Releases
# tectonic (LaTeX engine) is installed automatically on first build
# Usage: curl -fsSL https://raw.githubusercontent.com/JheisonMB/texforge/main/install.sh | sh
set -eu

REPO="JheisonMB/texforge"
BINARY="texforge"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"

info() { printf '  \033[1;34m%s\033[0m %s\n' "$1" "$2"; }
error() { printf '  \033[1;31merror:\033[0m %s\n' "$1" >&2; exit 1; }

# --- detect OS ---
OS="$(uname -s)"
case "$OS" in
  Linux*)  OS_TARGET="unknown-linux-musl" ;;
  Darwin*) OS_TARGET="apple-darwin" ;;
  *)       error "Unsupported OS: $OS (only Linux and macOS are supported)" ;;
esac

# --- detect arch ---
ARCH="$(uname -m)"
case "$ARCH" in
  x86_64|amd64)   ARCH_TARGET="x86_64" ;;
  arm64|aarch64)   ARCH_TARGET="aarch64" ;;
  *)               error "Unsupported architecture: $ARCH" ;;
esac

TARGET="${ARCH_TARGET}-${OS_TARGET}"
info "platform" "$TARGET"

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

# ============================================================
# 1. Install texforge
# ============================================================

# --- resolve latest version ---
if [ -n "${VERSION:-}" ]; then
  TAG="v$VERSION"
  info "version" "$TAG (pinned)"
else
  TAG=$(curl -fsSL -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" | rev | cut -d'/' -f1 | rev)
  [ -z "$TAG" ] && error "Could not resolve latest release tag"
  info "version" "$TAG (latest)"
fi

# --- download ---
ARCHIVE="${BINARY}-${TAG}-${TARGET}.tar.gz"
URL="https://github.com/$REPO/releases/download/${TAG}/${ARCHIVE}"

info "download" "$URL"
HTTP_CODE=$(curl -fSL -w '%{http_code}' -o "$TMPDIR/$ARCHIVE" "$URL" 2>/dev/null) || true
[ "$HTTP_CODE" = "200" ] || error "Download failed (HTTP $HTTP_CODE). Check that $TAG exists for $TARGET at:\n  $URL"

# --- extract ---
tar xzf "$TMPDIR/$ARCHIVE" -C "$TMPDIR"
[ -f "$TMPDIR/$BINARY" ] || error "Binary not found in archive"

# --- install ---
mkdir -p "$INSTALL_DIR"
mv "$TMPDIR/$BINARY" "$INSTALL_DIR/$BINARY"
chmod +x "$INSTALL_DIR/$BINARY"
info "installed" "$INSTALL_DIR/$BINARY"

# ============================================================
# 2. Ensure PATH
# ============================================================

PATHS_TO_ADD=""
case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *) PATHS_TO_ADD="$INSTALL_DIR" ;;
esac

if [ -n "$PATHS_TO_ADD" ]; then
  for dir in $PATHS_TO_ADD; do
    export PATH="$dir:$PATH"
  done

  for profile in "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.profile"; do
    if [ -f "$profile" ]; then
      for dir in $PATHS_TO_ADD; do
        if ! grep -q "export PATH=\"$dir:\$PATH\"" "$profile" 2>/dev/null; then
          printf '\n# Added by texforge installer\nexport PATH="%s:$PATH"\n' "$dir" >> "$profile"
          info "updated" "$profile"
        fi
      done
    fi
  done
fi

# ============================================================
# 3. Verify
# ============================================================

info "done" "$($INSTALL_DIR/$BINARY --version 2>/dev/null || echo "$BINARY installed")"
echo ""
info "ready" "Run 'texforge new my-project' to get started!"
info "note"  "tectonic (LaTeX engine) will be installed automatically on first build"
