#!/bin/bash

# Script para copiar las roms de Retroid Pocket

## Comprueba los parametros
if [ "$#" -ne 1 ]; then
    printf "No ha especificado destino\n"
    exit 0
else
    DESTINO=$1
    readonly DESTINO
fi

# Crea el directorio de destino de copia
if ! test -d "$DESTINO"; then
    mkdir -p "$DESTINO"
fi

# Origen de la copia
ORIGEN=maverick:/home/sergio/roms/ROMs_ALL
readonly ORIGEN

# Copia los sistemas con una sola carpeta
SISTEMAS="atari2600 atari5200 atari7800 atarilynx colecovision cps1 cps2 cps3 dreamcast fbneo fds gamegear gb gba gbc mastersystem neogeo ngp ngpc odyssey2 psp psx sg-1000 supergrafx virtualboy wonderswan wonderswancolor"
readonly SISTEMAS

OK=1

if [ "$OK" -ne 0 ]; then

    for SISTEMA in $SISTEMAS; do
        printf "\n\n>>> %s\n" "${SISTEMA}"
        mkdir -p "${DESTINO}/${SISTEMA}"
        rsync -avhPL --delete --chmod=755 "${ORIGEN}"/"${SISTEMA}/" "${DESTINO}/${SISTEMA}/"
    done

    # SNES
    SISTEMA=snes
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/snes/ "${ORIGEN}"/snesna/ "${ORIGEN}"/sfc/ "${DESTINO}/${SISTEMA}/"

    # NES
    SISTEMA=nes
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/nes/ "${ORIGEN}"/famicom/ "${DESTINO}/${SISTEMA}/"

    # MEGACD
    SISTEMA=megacd
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/megacd/ "${ORIGEN}"/megacdjp/ "${ORIGEN}"/segacd/ "${DESTINO}/${SISTEMA}/"

    # 32X
    SISTEMA=sega32x
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/sega32x/ "${ORIGEN}"/sega32xjp/ "${ORIGEN}"/sega32xna/ "${DESTINO}/${SISTEMA}/"

    # MEGADRIVE
    SISTEMA=megadrive
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/megadrive/ "${ORIGEN}"/genesis/ "${DESTINO}/${SISTEMA}/"

    # MEGADRIVE
    SISTEMA=megadrive
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/megadrive/ "${ORIGEN}"/genesis/ "${DESTINO}/${SISTEMA}/"

    # PCENGINE
    SISTEMA=pcengine
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/pcengine/ "${ORIGEN}"/tg16/ "${DESTINO}/${SISTEMA}/"

    # PCENGINECD
    SISTEMA=pcenginecd
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/pcenginecd/ "${ORIGEN}"/tg-cd/ "${DESTINO}/${SISTEMA}/"

    # NEOGEOCD
    SISTEMA=neogeocd
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/neogeocd/ "${ORIGEN}"/neogeocdjp/ "${DESTINO}/${SISTEMA}/"

    # SATURN
    SISTEMA=saturn
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/saturnjp/ "${DESTINO}/${SISTEMA}/"
fi
