#!/usr/bin/env bash
# CelDrive installer — downloads the signed CLI tarball + runs setup.
# Usage:  curl -fsSL https://celdrive.com/install.sh | bash
#         curl -fsSL https://celdrive.com/install.sh | bash -s -- --no-setup
#         curl -fsSL https://celdrive.com/install.sh | bash -s -- --prefix ~/Apps
set -euo pipefail

TARBALL_URL="${CELDRIVE_TARBALL_URL:-https://celdrive.com/dl/celdrive-cli-latest.tar.gz}"
PREFIX="${HOME}/.celdrive"
RUN_SETUP=true

while [[ $# -gt 0 ]]; do
  case "$1" in
    --no-setup) RUN_SETUP=false; shift ;;
    --prefix)   PREFIX="$2"; shift 2 ;;
    --help|-h)
      echo "Usage: install.sh [--no-setup] [--prefix DIR]"
      echo "  --no-setup     don't run celdrive-setup.sh after install"
      echo "  --prefix DIR   install location (default: ~/.celdrive)"
      exit 0 ;;
    *) echo "!! unknown flag: $1"; exit 1 ;;
  esac
done

INSTALL_DIR="${PREFIX}/cli"

# ---- preflight ----
need() {
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "!! '$1' is required but not installed."
    case "$1" in
      curl)   echo "   macOS: pre-installed              Linux: apt install curl" ;;
      tar)    echo "   macOS: pre-installed              Linux: apt install tar" ;;
      rclone) echo "   macOS: brew install rclone        Linux: apt install rclone" ;;
      sqlite3) echo "   macOS: pre-installed             Linux: apt install sqlite3" ;;
    esac
    exit 1
  fi
}
need curl
need tar
need rclone

OS="$(uname)"
case "$OS" in
  Darwin) ;;
  Linux)
    if ! command -v fusermount >/dev/null && ! command -v fusermount3 >/dev/null; then
      echo "   ! FUSE not installed — needed for mount (sync still works without it)"
      echo "     apt install fuse3   |   dnf install fuse3   |   pacman -S fuse3"
    fi ;;
  *) echo "!! Unsupported OS: $OS  (macOS + Linux supported; Windows uses windows/*.ps1 in the bundle)"; exit 1 ;;
esac

# ---- download + extract ----
mkdir -p "$INSTALL_DIR"
TMP_TAR="$(mktemp -t celdrive-cli-XXXXXX.tar.gz)"
trap 'rm -f "$TMP_TAR"' EXIT

echo "==> Downloading CelDrive CLI"
curl -fsSL "$TARBALL_URL" -o "$TMP_TAR"

echo "==> Extracting to $INSTALL_DIR"
tar -xzf "$TMP_TAR" -C "$INSTALL_DIR"

chmod +x "$INSTALL_DIR/bin/"*.sh
# bin/celdrive is the unified dispatcher (no extension) — make sure it's
# executable so users can run plain `celdrive ...` after the install.
[[ -f "$INSTALL_DIR/bin/celdrive" ]] && chmod +x "$INSTALL_DIR/bin/celdrive"

# ---- shell PATH hint ----
SHELL_RC=""
case "${SHELL##*/}" in
  zsh)  SHELL_RC="$HOME/.zshrc" ;;
  bash) SHELL_RC="$HOME/.bashrc" ;;
  fish) SHELL_RC="$HOME/.config/fish/config.fish" ;;
esac
PATH_LINE='export PATH="'"$INSTALL_DIR"'/bin:$PATH"'

if [[ -n "$SHELL_RC" ]] && ! grep -q "$INSTALL_DIR/bin" "$SHELL_RC" 2>/dev/null; then
  echo ""
  echo "   To put CelDrive on your PATH, add this to $SHELL_RC:"
  echo ""
  echo "       $PATH_LINE"
  echo ""
  echo "   Or run now:  echo '$PATH_LINE' >> $SHELL_RC && source $SHELL_RC"
fi

# ---- setup ----
echo ""
echo "==> Installed at $INSTALL_DIR"
echo "    bin/         — CLI scripts"
echo "    dashboard/   — local web dashboard (./bin/celdrive-dashboard.sh start)"
echo "    menubar/     — macOS menu bar app (./menubar/build.sh)"
echo ""

if $RUN_SETUP; then
  echo "==> Running setup..."
  echo ""
  exec "$INSTALL_DIR/bin/celdrive-setup.sh"
else
  echo "Next: $INSTALL_DIR/bin/celdrive-setup.sh"
fi
