#!/bin/bash

# Script para copiar las roms de RGBPI

## 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="dreamcast mastersystem neogeo psx"
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

    # ARCADE
    SISTEMA=arcade/fbneo
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/cps1/ "${ORIGEN}"/cps2/ "${ORIGEN}"/cps3/ "${DESTINO}/${SISTEMA}/"

    # 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=segacd
    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}/"

    # SG-1000
    SISTEMA=sg1000
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/sg-1000/ "${DESTINO}/${SISTEMA}/"

    # PCENGINE
    SISTEMA=pcengine
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/pcengine/ "${ORIGEN}"/tg16/ "${ORIGEN}"/supergrafx/ "${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=neocd
    printf "\n\n>>> %s\n" "${SISTEMA}"
    mkdir -p "${DESTINO}/${SISTEMA}"
    rsync -avhPL --delete --chmod=755 "${ORIGEN}"/neogeocd/ "${ORIGEN}"/neogeocdjp/ "${DESTINO}/${SISTEMA}/"
fi
