#!/usr/bin/env bash
set -euo pipefail

BACKUP_DIR=/mnt/toshiba/backups/media
LOG="$BACKUP_DIR/backup.log"
VERBOSE=0

for arg in "$@"; do
  case $arg in
    -v|--verbose) VERBOSE=1 ;;
  esac
done

log() { echo "[$(date)] $*" >> "$LOG"; }
say() { [[ $VERBOSE -eq 1 ]] && echo "$*" || true; }


mkdir -p "$BACKUP_DIR"

say ""
say "┌─────────────────────────────────────┐"
say "│        Immich Backup Starting        │"
say "└─────────────────────────────────────┘"
say ""
log "Starting Immich backup"

# Rsync critical asset folders (skip thumbs and encoded-video — regeneratable)
RSYNC_OPTS="-a --ignore-existing"
[[ $VERBOSE -eq 1 ]] && RSYNC_OPTS="$RSYNC_OPTS --info=progress2"

for DIR in library upload profile; do
  say "  Syncing $DIR/ ..."
  rsync $RSYNC_OPTS /mnt/smsg/media/upload/$DIR/ "$BACKUP_DIR/$DIR/" 2>&1 | \
    tee -a "$LOG" | { [[ $VERBOSE -eq 0 ]] && cat > /dev/null || cat; }
  log "Synced $DIR"
  say ""
done

touch "$BACKUP_DIR/.last_sync"
log "Immich backup complete"
say "✓ Done"
say ""
