#!/usr/bin/env bash
#
# resumable installer
#
#   curl -fsSL https://resumable-cli.towhid.space/install.sh | bash
#
# Downloads the prebuilt `resumable` binary for your OS/arch from GitHub
# Releases and installs it into ~/.local/bin. No Rust toolchain required.
#
# Environment overrides:
#   RESUMABLE_REPO         GitHub owner/repo   (default: itsTowhid/resumable-cli)
#   RESUMABLE_VERSION      release tag         (default: latest)
#   RESUMABLE_INSTALL_DIR  install directory   (default: $HOME/.local/bin)

set -eu

REPO="${RESUMABLE_REPO:-itsTowhid/resumable-cli}"
VERSION="${RESUMABLE_VERSION:-latest}"
INSTALL_DIR="${RESUMABLE_INSTALL_DIR:-$HOME/.local/bin}"
BIN="resumable"

# ----- pretty output (only colorize a real terminal) -----
if [ -t 1 ]; then
  BOLD="$(printf '\033[1m')"; DIM="$(printf '\033[2m')"
  GREEN="$(printf '\033[32m')"; YELLOW="$(printf '\033[33m')"
  RED="$(printf '\033[31m')"; RESET="$(printf '\033[0m')"
else
  BOLD=""; DIM=""; GREEN=""; YELLOW=""; RED=""; RESET=""
fi

info()  { printf '%s==>%s %s\n' "$GREEN" "$RESET" "$1"; }
warn()  { printf '%s!  %s%s\n' "$YELLOW" "$1" "$RESET"; }
error() { printf '%serror:%s %s\n' "$RED" "$RESET" "$1" >&2; }
die()   { error "$1"; exit 1; }

cleanup() { [ -n "${TMPDIR_RESUMABLE:-}" ] && rm -rf "$TMPDIR_RESUMABLE"; }
trap cleanup EXIT

# ----- detect OS / arch -> Rust target triple -----
detect_target() {
  os="$(uname -s)"
  arch="$(uname -m)"

  case "$os" in
    Darwin) os_part="apple-darwin" ;;
    Linux)  os_part="unknown-linux-gnu" ;;
    MINGW*|MSYS*|CYGWIN*)
      die "Windows isn't supported directly — install inside WSL (https://learn.microsoft.com/windows/wsl/)." ;;
    *) die "unsupported OS: $os" ;;
  esac

  case "$arch" in
    x86_64|amd64)  arch_part="x86_64" ;;
    arm64|aarch64) arch_part="aarch64" ;;
    *) die "unsupported architecture: $arch" ;;
  esac

  printf '%s-%s' "$arch_part" "$os_part"
}

# ----- pick a downloader -----
if command -v curl >/dev/null 2>&1; then
  DL() { curl -fSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  DL() { wget -q "$1" -O "$2"; }
else
  die "need curl or wget to download the release."
fi

command -v tar >/dev/null 2>&1 || die "need tar to extract the release."

TARGET="$(detect_target)"
ASSET="${BIN}-${TARGET}.tar.gz"

if [ "$VERSION" = "latest" ]; then
  URL="https://github.com/${REPO}/releases/latest/download/${ASSET}"
else
  URL="https://github.com/${REPO}/releases/download/${VERSION}/${ASSET}"
fi

info "Installing ${BOLD}${BIN}${RESET} (${TARGET}, ${VERSION})"
printf '    %sfrom%s %s\n' "$DIM" "$RESET" "$URL"

TMPDIR_RESUMABLE="$(mktemp -d)"
ARCHIVE="${TMPDIR_RESUMABLE}/${ASSET}"

if ! DL "$URL" "$ARCHIVE"; then
  error "download failed: $URL"
  warn  "check that a release exists for ${REPO} and target ${TARGET}."
  exit 1
fi

info "Extracting"
tar -xzf "$ARCHIVE" -C "$TMPDIR_RESUMABLE"

# The binary may sit at the archive root or inside a folder — find it.
BIN_PATH="$(find "$TMPDIR_RESUMABLE" -type f -name "$BIN" -perm -u+x 2>/dev/null | head -n1)"
[ -z "$BIN_PATH" ] && BIN_PATH="$(find "$TMPDIR_RESUMABLE" -type f -name "$BIN" 2>/dev/null | head -n1)"
[ -z "$BIN_PATH" ] && die "could not find '$BIN' inside the downloaded archive."

info "Installing to ${BOLD}${INSTALL_DIR}${RESET}"
mkdir -p "$INSTALL_DIR"
install -m 755 "$BIN_PATH" "$INSTALL_DIR/$BIN" 2>/dev/null \
  || { cp "$BIN_PATH" "$INSTALL_DIR/$BIN" && chmod 755 "$INSTALL_DIR/$BIN"; }

# ----- PATH check -----
case ":$PATH:" in
  *":$INSTALL_DIR:"*) ON_PATH=1 ;;
  *) ON_PATH=0 ;;
esac

printf '\n'
info "${GREEN}Installed ${BIN} -> ${INSTALL_DIR}/${BIN}${RESET}"

if [ "$ON_PATH" -eq 1 ] && command -v "$BIN" >/dev/null 2>&1; then
  "$INSTALL_DIR/$BIN" --version 2>/dev/null || true
else
  warn "${INSTALL_DIR} is not on your PATH."
  printf '   Add this to your shell profile (~/.zshrc or ~/.bashrc):\n\n'
  printf '     %sexport PATH="%s:$PATH"%s\n\n' "$BOLD" "$INSTALL_DIR" "$RESET"
fi

printf '\nNext: %sresumable init%s in an empty folder, then %sresumable tailor%s.\n' \
  "$BOLD" "$RESET" "$BOLD" "$RESET"
printf 'Docs: https://resumable-cli.towhid.space/docs/\n'
