#!/bin/bash
#
# sync-comfyui-snaps — Pull new images from nimo.loc ComfyUI output, sync to NAS,
# then rename AI snaps and organize.
#
# NOTE: superseded by comfyui-sync (systemd user timer on nimo.loc). Kept for
# reference/manual use; the cron entry below is NOT currently installed.
#
# Crontab (every 2 hours):
#   0 */2 * * * /home/schmeeve/.local/bin/sync-comfyui-snaps >> /home/schmeeve/.local/log/sync-comfyui-snaps.log 2>&1
#
# Requires: rsync, SSH key auth to nimo.loc (or runs directly on nimo.loc),
#           rename-ai-snaps and organize-images in ~/.local/bin/
#
# NAS credentials file at ~/.smb/mini.nas (chmod 600):
#   username=schmeeve
#   password=yourpass

# Source is local (this script runs on nimo.loc).
# Set NIMO_HOST to a remote hostname only if running this from another machine.
NIMO_HOST=""
NIMO_USER="schmeeve"
NIMO_SRC="/home/schmeeve/ComfyUI/output"

SHARE="//mini.nas/miniShare1"
NAS_SUBPATH="Pictures"
CREDENTIALS="${HOME}/.smb/mini.nas"

RENAME_SCRIPT="${HOME}/git/schmeeve-toolz/rename-ai-snaps"
ORGANIZE_SCRIPT="${HOME}/git/schmeeve-toolz/organize-images"

START="$(date +%s)"
echo "[sync-comfyui-snaps] Starting at $(date)"

# 1. Determine NAS mount — use pre-mounted /mini.nas/miniShare1 on nimo.loc
#    if available; otherwise mount via CIFS under ~/mnt/miniShare1
if [ -d "/mini.nas/miniShare1" ]; then
  MOUNTPOINT="/mini.nas/miniShare1"
  echo "[sync-comfyui-snaps] Using existing mount at ${MOUNTPOINT}"
else
  MOUNTPOINT="${HOME}/mnt/miniShare1"
  if mount | grep -q "${MOUNTPOINT}"; then
    echo "[sync-comfyui-snaps] Already mounted at ${MOUNTPOINT}"
  else
    echo "[sync-comfyui-snaps] Mounting ${SHARE} → ${MOUNTPOINT}"
    mkdir -p "${MOUNTPOINT}"
    OPTS="username=schmeeve,uid=$(id -u),gid=$(id -g),forceuid,forcegid,nounix,serverino"
    if [ -f "${CREDENTIALS}" ]; then
      OPTS="${OPTS},credentials=${CREDENTIALS}"
    fi
    sudo mount -t cifs "${SHARE}" "${MOUNTPOINT}" -o "${OPTS}"
    if ! mount | grep -q "${MOUNTPOINT}"; then
      echo "[sync-comfyui-snaps] ERROR: Failed to mount ${SHARE}"
      exit 1
    fi
    echo "[sync-comfyui-snaps] Mounted at ${MOUNTPOINT}"
  fi
fi

DEST="${MOUNTPOINT}/${NAS_SUBPATH}"
mkdir -p "${DEST}"

# 2. Rsync from nimo.loc ComfyUI output → NAS Pictures
# If running directly on nimo.loc, set NIMO_HOST="" and it will use a local path instead.
if [ -z "${NIMO_HOST}" ] || [ "${NIMO_HOST}" = "localhost" ]; then
  SRC="${NIMO_SRC}/"
else
  SRC="${NIMO_USER}@${NIMO_HOST}:${NIMO_SRC}/"
fi

echo "[sync-comfyui-snaps] Syncing ${SRC} → ${DEST}/"
rsync -vu --checksum \
  --include="*.png" --include="*.jpg" --include="*.jpeg" --include="*.webp" \
  --exclude="*" \
  --progress --stats \
  "${SRC}" "${DEST}/"

# 3. Route fresh drops into Images/ (neatcli equivalent), then rename in place.
# NOTE: rename-ai-snaps no longer takes --output; it only renames.
# organize-images does the model/lora sorting.
IMAGES_DIR="${DEST}/Images"
mkdir -p "${IMAGES_DIR}"
find "${DEST}" -maxdepth 1 -type f \
  \( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.webp' \) \
  -exec mv -n {} "${IMAGES_DIR}/" \;

if [ -x "${RENAME_SCRIPT}" ]; then
  echo "[sync-comfyui-snaps] Running rename-ai-snaps on ${IMAGES_DIR}"
  "${RENAME_SCRIPT}" "${IMAGES_DIR}" --no-interactive
else
  echo "[sync-comfyui-snaps] WARNING: rename-ai-snaps not found at ${RENAME_SCRIPT}"
fi

# 4. Organize images into AI/{model}/{lora}/
if [ -x "${ORGANIZE_SCRIPT}" ]; then
  echo "[sync-comfyui-snaps] Running organize-images on ${IMAGES_DIR}"
  "${ORGANIZE_SCRIPT}" "${IMAGES_DIR}" -e
else
  echo "[sync-comfyui-snaps] WARNING: organize-images not found at ${ORGANIZE_SCRIPT}"
fi

DURATION=$(($(date +%s) - START))
echo "[sync-comfyui-snaps] Done in ${DURATION}s at $(date)"
