#!/usr/bin/env sh
# Install the photoscand daemon for the current user.
#
#   curl -fsSL https://photo-scan.deverall.nz/install.sh | sh
#
# Downloads the right binary for this OS/arch from $LANDING_URL/downloads/...
# (the photo-scan service itself bundles the daemon binaries), places it in
# $INSTALL_DIR (default ~/.local/bin), strips the macOS quarantine xattr,
# and tells you to run `photoscand setup`.
#
# Override with env:
#   INSTALL_DIR=/usr/local/bin   where to put the binary
#   LANDING_URL=https://...      base URL for /downloads/<platform>
#   PLATFORM=darwin-arm64        skip auto-detection

set -eu

LANDING_URL="${LANDING_URL:-https://photo-scan.deverall.nz}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"

# ---- detect platform ----------------------------------------------------

if [ -z "${PLATFORM:-}" ]; then
  uname_s="$(uname -s 2>/dev/null || echo unknown)"
  uname_m="$(uname -m 2>/dev/null || echo unknown)"
  case "$uname_s" in
    Darwin)
      case "$uname_m" in
        arm64)   PLATFORM=darwin-arm64 ;;
        x86_64)  PLATFORM=darwin-amd64 ;;
        *) echo "Unsupported macOS arch: $uname_m" >&2; exit 1 ;;
      esac ;;
    Linux)
      case "$uname_m" in
        x86_64|amd64)  PLATFORM=linux-amd64 ;;
        *) echo "Unsupported Linux arch: $uname_m (only amd64 is built today)" >&2; exit 1 ;;
      esac ;;
    *)
      echo "Unsupported OS: $uname_s." >&2
      echo "Windows users: download photoscand-windows-amd64.exe from $LANDING_URL manually." >&2
      exit 1 ;;
  esac
fi

case "$PLATFORM" in
  darwin-arm64|darwin-amd64)  bin_name=photoscand ;;
  linux-amd64)                bin_name=photoscand ;;
  windows-amd64)              bin_name=photoscand.exe ;;
  *) echo "Unknown PLATFORM=$PLATFORM" >&2; exit 1 ;;
esac

# ---- download -----------------------------------------------------------

mkdir -p "$INSTALL_DIR"
dest="$INSTALL_DIR/$bin_name"
tmp="$(mktemp -t photoscand.XXXXXX)"
trap 'rm -f "$tmp"' EXIT

echo "Downloading photoscand for $PLATFORM..."
if command -v curl >/dev/null 2>&1; then
  curl -fL --progress-bar -o "$tmp" "$LANDING_URL/downloads/$PLATFORM"
elif command -v wget >/dev/null 2>&1; then
  wget --show-progress -qO "$tmp" "$LANDING_URL/downloads/$PLATFORM"
else
  echo "Need curl or wget on PATH." >&2; exit 1
fi

chmod +x "$tmp"

# macOS: strip the quarantine xattr so Gatekeeper doesn't block the unsigned
# binary. Best-effort; on Linux this is a no-op.
if [ "$(uname -s)" = "Darwin" ]; then
  xattr -d com.apple.quarantine "$tmp" 2>/dev/null || true
fi

mv "$tmp" "$dest"
trap - EXIT

# ---- finish -------------------------------------------------------------

echo
echo "Installed: $dest"

case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    echo
    echo "  ${INSTALL_DIR} is not on your PATH. Add this to your shell rc:"
    echo "    export PATH=\"$INSTALL_DIR:\$PATH\""
    ;;
esac

echo
echo "Next:"
echo "  photoscand setup     # configure Immich key, watch folder, album"
echo "  photoscand           # run in the foreground"
