#!/bin/bash
# Run UMM arr data migration from the host
# Mounts arr SQLite databases into the container and runs migration
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

echo "=== UMM Arr Data Migration ==="
echo ""

# Check if backend is running
if ! docker compose -f "$PROJECT_DIR/docker-compose.yml" ps backend | grep -q "Up\|running"; then
    echo "Error: Backend container is not running."
    echo "Start it with: docker compose -f $PROJECT_DIR/docker-compose.yml up -d backend"
    exit 1
fi

echo "Running migration..."
echo ""

docker compose -f "$PROJECT_DIR/docker-compose.yml" exec backend ./umm-migrate \
  --sonarr=/data/arr-configs/sonarr/sonarr.db \
  --radarr=/data/arr-configs/radarr/radarr.db \
  --sonarr-anime=/data/arr-configs/sonarr-anime/sonarr.db \
  --radarr-anime=/data/arr-configs/radarr-anime/radarr.db \
  --lidarr=/data/arr-configs/lidarr/lidarr.db \
  --readarr=/data/arr-configs/readarr/readarr.db \
  --prowlarr=/data/arr-configs/prowlarr/prowlarr.db

EXIT_CODE=$?

echo ""
if [ $EXIT_CODE -eq 0 ]; then
    echo "Migration completed successfully!"
else
    echo "Migration completed with errors (exit code: $EXIT_CODE)"
fi
exit $EXIT_CODE
