#!/usr/bin/env bash
#
# video-projects-mount.sh — network-backed FUSE mount for video-studio projects,
# backed by DO Spaces with a bounded local LRU cache.
#
# WHAT THIS IS
#   The same rclone-mount-over-Spaces pattern as photos-originals-mount.sh, but
#   pointed at the `video-projects` prefix instead of `photos-originals`. The
#   video studio (and the imajin ETL — see .project/storage-portability-plan.md
#   §2.3) read/write project sources and renders here as if they were local
#   files; only hot files stay resident on plum, the rest evict to Spaces. This
#   keeps a small-disk laptop from filling up on multi-GB source footage.
#
#   The mounted tree is a faithful filesystem view of the prefix
#   (video-projects/<project>/<file>) — plain files, no blob-key scheme.
#
# PRECONDITIONS (operator, one-time)
#   1. macFUSE installed + kext approved:   brew install --cask macfuse
#   2. rclone installed:                     brew install rclone
#   3. DO Spaces creds in ~/.vault (see lib/spaces-env.sh).
#
# Secrets come from ~/.vault (0600), never from the environment or in-tree.

set -euo pipefail

# --- config (override via env) ---------------------------------------------
MOUNTPOINT="${MOUNTPOINT:-$HOME/_/video-projects}"     # where rclone mounts
CACHE_DIR="${CACHE_DIR:-$HOME/.cache/video-projects}"
CACHE_MAX="${CACHE_MAX:-15G}"                           # LRU ceiling on plum

# Shared Spaces/rclone config + creds from ~/.vault, but target the
# video-projects prefix rather than the photos default.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PREFIX="${PREFIX:-video-projects}"
# shellcheck source=lib/spaces-env.sh
source "$SCRIPT_DIR/lib/spaces-env.sh"
spaces_env_init

# Homebrew's rclone is built WITHOUT FUSE `mount` support on macOS (licensing).
# Use the official rclone binary (rclone.org) for the mount; brew's is fine for
# plain transfers. Install once: see deploy/README or the official downloads.
RCLONE_BIN="${RCLONE_BIN:-$HOME/bin/rclone}"
if ! "$RCLONE_BIN" mount --help >/dev/null 2>&1; then
  echo "error: $RCLONE_BIN lacks 'mount' support." >&2
  echo "       Homebrew rclone cannot mount on macOS; install the official" >&2
  echo "       binary from https://rclone.org/downloads/ to \$HOME/bin/rclone" >&2
  echo "       (or set RCLONE_BIN to one that supports mount)." >&2
  exit 1
fi

[ -d "$(dirname "$MOUNTPOINT")" ] || mkdir -p "$(dirname "$MOUNTPOINT")"
mkdir -p "$MOUNTPOINT" "$CACHE_DIR"

# Already mounted? (mount table lists the mountpoint)
if mount | grep -q " $MOUNTPOINT "; then
  echo "already mounted at $MOUNTPOINT"
  exit 0
fi

echo "mounting spaces://$BUCKET/$PREFIX → $MOUNTPOINT (cache ≤ $CACHE_MAX)"

# --vfs-cache-mode full → real POSIX read/write semantics editors/ffmpeg expect;
# --vfs-cache-max-size  → LRU eviction ceiling (keeps plum off 100%);
# --dir-cache-time      → keep the directory tree warm so Finder doesn't stall.
exec "$RCLONE_BIN" mount ":s3:$BUCKET/$PREFIX" "$MOUNTPOINT" \
  --vfs-cache-mode full \
  --vfs-cache-max-size "$CACHE_MAX" \
  --vfs-cache-max-age 720h \
  --dir-cache-time 72h \
  --attr-timeout 5s \
  --cache-dir "$CACHE_DIR" \
  --volname "Cocotte Video Projects" \
  --no-modtime \
  --log-level INFO
