#!/bin/bash
set -e
NAS_DEST='/mnt/18tb/roms/1g1r'
echo 'Consolidando: "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)" -> "/mnt/18tb/roms/1g1r/Atari - Atari Lynx"'
mkdir -p "/mnt/18tb/roms/1g1r/Atari - Atari Lynx"

if [ -d "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)" ]; then
    find "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)" -maxdepth 1 -type f -name "*.7z" | while read -r f; do
        bname=$(basename "$f")
        if [ ! -f "/mnt/18tb/roms/1g1r/Atari - Atari Lynx/$bname" ]; then
            mv "$f" "/mnt/18tb/roms/1g1r/Atari - Atari Lynx/$bname"
        else
            rm -f "$f"
        fi
    done
    # Si existe carpeta /hacks dentro del origen, mover hacia <dst>/hacks
    if [ -d "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)/hacks" ]; then
        mkdir -p "/mnt/18tb/roms/1g1r/Atari - Atari Lynx/hacks"
        find "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)/hacks" -maxdepth 1 -type f -name "*.7z" | while read -r hf; do
            hbname=$(basename "$hf")
            if [ ! -f "/mnt/18tb/roms/1g1r/Atari - Atari Lynx/hacks/$hbname" ]; then
                mv "$hf" "/mnt/18tb/roms/1g1r/Atari - Atari Lynx/hacks/$hbname"
            else
                rm -f "$hf"
            fi
        done
        rmdir "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)/hacks" 2>/dev/null || true
    fi
    # Eliminar directorio origen si quedó vacío
    rmdir "/mnt/18tb/roms/1g1r/Atari - Atari Lynx (LYX)" 2>/dev/null || true
fi

echo 'Consolidando: "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)" -> "/mnt/18tb/roms/1g1r/Commodore - Commodore 64"'
mkdir -p "/mnt/18tb/roms/1g1r/Commodore - Commodore 64"

if [ -d "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)" ]; then
    find "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)" -maxdepth 1 -type f -name "*.7z" | while read -r f; do
        bname=$(basename "$f")
        if [ ! -f "/mnt/18tb/roms/1g1r/Commodore - Commodore 64/$bname" ]; then
            mv "$f" "/mnt/18tb/roms/1g1r/Commodore - Commodore 64/$bname"
        else
            rm -f "$f"
        fi
    done
    # Si existe carpeta /hacks dentro del origen, mover hacia <dst>/hacks
    if [ -d "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)/hacks" ]; then
        mkdir -p "/mnt/18tb/roms/1g1r/Commodore - Commodore 64/hacks"
        find "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)/hacks" -maxdepth 1 -type f -name "*.7z" | while read -r hf; do
            hbname=$(basename "$hf")
            if [ ! -f "/mnt/18tb/roms/1g1r/Commodore - Commodore 64/hacks/$hbname" ]; then
                mv "$hf" "/mnt/18tb/roms/1g1r/Commodore - Commodore 64/hacks/$hbname"
            else
                rm -f "$hf"
            fi
        done
        rmdir "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)/hacks" 2>/dev/null || true
    fi
    # Eliminar directorio origen si quedó vacío
    rmdir "/mnt/18tb/roms/1g1r/Commodore - Commodore 64 (Headerless)" 2>/dev/null || true
fi

echo 'Consolidando: "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)" -> "/mnt/18tb/roms/1g1r/Fujitsu - FM-7"'
mkdir -p "/mnt/18tb/roms/1g1r/Fujitsu - FM-7"

if [ -d "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)" ]; then
    find "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)" -maxdepth 1 -type f -name "*.7z" | while read -r f; do
        bname=$(basename "$f")
        if [ ! -f "/mnt/18tb/roms/1g1r/Fujitsu - FM-7/$bname" ]; then
            mv "$f" "/mnt/18tb/roms/1g1r/Fujitsu - FM-7/$bname"
        else
            rm -f "$f"
        fi
    done
    # Si existe carpeta /hacks dentro del origen, mover hacia <dst>/hacks
    if [ -d "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)/hacks" ]; then
        mkdir -p "/mnt/18tb/roms/1g1r/Fujitsu - FM-7/hacks"
        find "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)/hacks" -maxdepth 1 -type f -name "*.7z" | while read -r hf; do
            hbname=$(basename "$hf")
            if [ ! -f "/mnt/18tb/roms/1g1r/Fujitsu - FM-7/hacks/$hbname" ]; then
                mv "$hf" "/mnt/18tb/roms/1g1r/Fujitsu - FM-7/hacks/$hbname"
            else
                rm -f "$hf"
            fi
        done
        rmdir "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)/hacks" 2>/dev/null || true
    fi
    # Eliminar directorio origen si quedó vacío
    rmdir "/mnt/18tb/roms/1g1r/Fujitsu - FM-7 (Sector)" 2>/dev/null || true
fi

echo 'Limpiando locks huérfanos...'
find '/mnt/18tb/roms/1g1r' -name '*.lock' -delete
rm -rf /mnt/18tb/1g1r_tmp/* /tmp/1g1r_tmp/* 2>/dev/null || true
echo 'Consolidación terminada con éxito.'