#!/bin/sh
set -e

VERSION="1.0"
BASE_URL="https://rosti.cz/cli"
BINARY="rosticli"
INSTALL_DIR="${HOME}/.local/bin"

# ── Colors ────────────────────────────────────────────────────────────────────
if [ -t 1 ]; then
  BOLD="\033[1m"
  DIM="\033[2m"
  GREEN="\033[32m"
  YELLOW="\033[33m"
  CYAN="\033[36m"
  RED="\033[31m"
  RESET="\033[0m"
else
  BOLD=""; DIM=""; GREEN=""; YELLOW=""; CYAN=""; RED=""; RESET=""
fi

info()    { printf "  ${CYAN}•${RESET} %s\n" "$*"; }
success() { printf "  ${GREEN}✔${RESET} ${BOLD}%s${RESET}\n" "$*"; }
warn()    { printf "  ${YELLOW}!${RESET} %s\n" "$*"; }
error()   { printf "  ${RED}✖${RESET} ${BOLD}%s${RESET}\n" "$*" >&2; }
step()    { printf "\n${BOLD}%s${RESET}\n" "$*"; }
dim()     { printf "  ${DIM}%s${RESET}\n" "$*"; }

# ── Header ────────────────────────────────────────────────────────────────────
printf "\n${BOLD}${CYAN}rosticli ${VERSION} installer${RESET}\n"
printf "${DIM}─────────────────────────────────${RESET}\n"

# ── Detect OS ─────────────────────────────────────────────────────────────────
step "Detecting platform ..."
case "$(uname -s)" in
  Linux)  OS="linux" ;;
  Darwin) OS="darwin" ;;
  *)
    error "Unsupported OS: $(uname -s)"
    exit 1
    ;;
esac

case "$(uname -m)" in
  x86_64|amd64)  ARCH="amd64" ;;
  aarch64|arm64) ARCH="arm64" ;;
  *)
    error "Unsupported architecture: $(uname -m)"
    exit 1
    ;;
esac
info "Platform: ${OS}/${ARCH}"

# ── Download ──────────────────────────────────────────────────────────────────
FILE="${BINARY}-${VERSION}-${OS}-${ARCH}"
URL="${BASE_URL}/${FILE}"
TMP_FILE="/tmp/${FILE}"

step "Downloading binary ..."
info "${URL}"
if command -v curl > /dev/null 2>&1; then
  curl -fsSL --progress-bar "$URL" -o "$TMP_FILE"
elif command -v wget > /dev/null 2>&1; then
  wget -q --show-progress "$URL" -O "$TMP_FILE" 2>&1 || wget -q "$URL" -O "$TMP_FILE"
else
  error "curl or wget is required"
  exit 1
fi
success "Downloaded"

# ── Verify checksum ───────────────────────────────────────────────────────────
SUMS_URL="${BASE_URL}/sha256sums-${VERSION}.txt"
SUMS_FILE="/tmp/rosticli-sha256sums.txt"
if command -v sha256sum > /dev/null 2>&1; then
  step "Verifying checksum ..."
  DOWNLOADED_SUMS=0
  if command -v curl > /dev/null 2>&1; then
    curl -fsSL "$SUMS_URL" -o "$SUMS_FILE" 2>/dev/null && DOWNLOADED_SUMS=1 || true
  else
    wget -q "$SUMS_URL" -O "$SUMS_FILE" 2>/dev/null && DOWNLOADED_SUMS=1 || true
  fi
  if [ "$DOWNLOADED_SUMS" = "1" ] && [ -s "$SUMS_FILE" ]; then
    grep "${FILE}$" "$SUMS_FILE" | (cd /tmp && sha256sum -c - > /dev/null 2>&1)
    success "Checksum OK"
    rm -f "$SUMS_FILE"
  else
    warn "sha256sums-${VERSION}.txt not available, skipping verification"
  fi
fi

# ── Install ───────────────────────────────────────────────────────────────────
step "Installing ..."
mkdir -p "$INSTALL_DIR"
chmod +x "$TMP_FILE"
mv "$TMP_FILE" "${INSTALL_DIR}/${BINARY}"
success "Installed to ${INSTALL_DIR}/${BINARY}"

# ── PATH setup ────────────────────────────────────────────────────────────────
case ":${PATH}:" in
  *":${INSTALL_DIR}:"*)
    success "${INSTALL_DIR} is already in your PATH — you're all set!"
    printf "\n"
    exit 0
    ;;
esac

step "PATH configuration ..."
warn "${INSTALL_DIR} is not in your PATH yet."

# Detect current shell
CURRENT_SHELL="$(basename "$SHELL")"

offer_patch() {
  CONFIG_FILE="$1"
  PATCH_LINE="$2"
  printf "\n"
  info "Shell detected: ${BOLD}${CURRENT_SHELL}${RESET}"
  info "Config file   : ${BOLD}${CONFIG_FILE}${RESET}"
  printf "\n"
  printf "  ${DIM}The following line will be appended:${RESET}\n"
  printf "  ${YELLOW}%s${RESET}\n" "$PATCH_LINE"
  printf "\n"
  printf "  Proceed? [y/N] "
  read -r REPLY
  case "$REPLY" in
    y|Y|yes|YES)
      printf '\n%s\n' "$PATCH_LINE" >> "$CONFIG_FILE"
      success "Updated ${CONFIG_FILE}"
      warn "Restart your terminal or run:  source ${CONFIG_FILE}"
      ;;
    *)
      info "Skipped. Add it manually to get ${BINARY} in your PATH."
      ;;
  esac
}

case "$CURRENT_SHELL" in
  bash)
    # Prefer .bash_profile on macOS, .bashrc on Linux
    if [ "$OS" = "darwin" ] && [ -f "${HOME}/.bash_profile" ]; then
      RC="${HOME}/.bash_profile"
    else
      RC="${HOME}/.bashrc"
    fi
    offer_patch "$RC" "export PATH=\"\$HOME/.local/bin:\$PATH\""
    ;;
  zsh)
    RC="${ZDOTDIR:-$HOME}/.zshrc"
    offer_patch "$RC" "export PATH=\"\$HOME/.local/bin:\$PATH\""
    ;;
  fish)
    RC="${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
    offer_patch "$RC" "fish_add_path \$HOME/.local/bin"
    ;;
  *)
    printf "\n"
    warn "Unknown shell '${CURRENT_SHELL}'. Add this line to your shell's config file:"
    printf "  ${YELLOW}export PATH=\"\$HOME/.local/bin:\$PATH\"${RESET}\n"
    ;;
esac

printf "\n"

