#!/bin/bash

## usage
USAGE="
USAGE:
$(basename "$0") [build | scrap | both] [frontend] [path to roms]"
readonly USAGE

FRONTENDS="emulationstation pegasus"
readonly FRONTENDS

function help_message() {
    echo "$USAGE"
    echo
    echo "Where [FRONTEND] are:"
    for WORD in $FRONTENDS; do
        echo -e "\t$WORD"
    done
}

## check if there are all the parameters
if [ "$#" -ne 3 ]; then
    help_message
    exit 0
fi

## check if the frontend parameter is valid
if ! echo "$FRONTENDS" | grep -w "$2" >/dev/null; then
    printf "%s\n" "You must enter a valid FRONTEND name."
    help_message
    exit 0
fi

## parameters
readonly FRONTEND="$2"
readonly ROM_DIR="$3"

## colors
COLOR_WHITE=$(tput setaf 7)
COLOR_BACKGROUND_RED=$(tput setab 1)
RESET_COLOR=$(tput sgr0)
readonly COLOR_WHITE
readonly COLOR_BACKGROUND_RED
readonly RESET_COLOR

## artwork.xml
readonly CONSOLE_ART=~/.skyscraper/artwork.xml.rgbpi
readonly ARCADE_ART=~/.skyscraper/artwork.xml.rgbpi
readonly CD_ART=~/.skyscraper/artwork.xml.cover.320x240

## scrapers
readonly CONSOLE_SCRAPERS="screenscraper mobygames thegamesdb"
readonly ARCADE_SCRAPERS="screenscraper arcadedb"

## flags
readonly BUILD_FLAGS_CONSOLE="videos,unattend,forcefilename,relative,symlink"
readonly BUILD_FLAGS_ARCADE="videos,unattend,relative,symlink"
readonly SCRAP_FLAGS_CONSOLE="videos,unattend,noresize,unpack"
readonly SCRAP_FLAGS_ARCADE="videos,unattend,noresize"
#readonly CACHE_REFRESH="--cache refresh"

## check if BUID, SCRAP or BOTH
if [ "$1" = scrap ] || [ "$1" = SCRAP ]; then
    BUILD=false
    SCRAP=true
    printf "%s\n" "${COLOR_WHITE}${COLOR_BACKGROUND_RED}## SCRAP ONLY ${RESET_COLOR}"
    sleep 1
fi

if [ "$1" = build ] || [ "$1" = BUILD ]; then
    BUILD=true
    SCRAP=false
    printf "%s\n" "${COLOR_WHITE}${COLOR_BACKGROUND_RED}## BUILD ONLY ${RESET_COLOR}"
    sleep 1
fi

if [ "$1" = both ] || [ "$1" = BOTH ]; then
    SCRAP=true
    BUILD=true
    printf "%s\n" "${COLOR_WHITE}${COLOR_BACKGROUND_RED}## SCRAP and BUILD ${RESET_COLOR}"
    sleep 1
fi

## console systems
CONSOLE="3ds amiga amstradcpc apple2 arcadia astrocde atari800 atari2600 atari5200 atari7800 atarilynx atarist c16 c64 c128 coco coleco daphne dragon32 fds gameandwatch gamegear gb gba gbc gc genesis intellivision mastersystem megadrive msx n64 nds nes ngp ngpc oric pc pc88 pc98 pcfx pcengine pokemini ports ps2 psp scummvm sega32x sg-1000 snes ti99 trs-80 vectrex vic20 videopac virtualboy wii wonderswan wonderswancolor x68000 x1 zmachine zx81 zxspectrum"
for SYSTEM in $CONSOLE; do
    if test -d "${ROM_DIR}"/"${SYSTEM}"; then
        if [ "$SCRAP" = true ]; then
            for SCRAPER in $CONSOLE_SCRAPERS; do
                Skyscraper --flags "${SCRAP_FLAGS_CONSOLE}" -p "${SYSTEM}" -s "${SCRAPER}" -i "${ROM_DIR}"/"${SYSTEM}"
            done
        fi
        if [ "$BUILD" = true ]; then
            Skyscraper --flags "${BUILD_FLAGS_CONSOLE}" -a $CONSOLE_ART -p "$SYSTEM" -f "${FRONTEND}" -i "${ROM_DIR}"/"${SYSTEM}"
        fi
    fi
done

## cd systems
CD_SYSTEM="3do atarijaguar dreamcast megacd psx saturn segacd"
for SYSTEM in $CD_SYSTEM; do
    if test -d "${ROM_DIR}"/"${SYSTEM}"; then
        if [ "$SCRAP" = true ]; then
            for SCRAPER in $CONSOLE_SCRAPERS; do
                Skyscraper --flags "${SCRAP_FLAGS_CONSOLE}" -p "${SYSTEM}" -s "${SCRAPER}" -i "${ROM_DIR}"/"${SYSTEM}"
            done
        fi
        if [ "$BUILD" = true ]; then
            Skyscraper --flags "${BUILD_FLAGS_CONSOLE}" -a $CD_ART -p "$SYSTEM" -f "${FRONTEND}" -i "${ROM_DIR}"/"${SYSTEM}"
        fi
    fi
done

## arcade systems
ARCADE="fba neogeo mame-advmame mame-mame4all mame-libretro"
for SYSTEM in $ARCADE; do
    if test -d "${ROM_DIR}"/"${SYSTEM}"; then
        if [ "$SCRAP" = true ]; then
            for SCRAPER in $ARCADE_SCRAPERS; do
                Skyscraper --flags "${SCRAP_FLAGS_ARCADE}" -p "$SYSTEM" -s "$SCRAPER" -i "${ROM_DIR}"/"${SYSTEM}"
            done
        fi
        if [ "$BUILD" = true ]; then
            Skyscraper --flags "${BUILD_FLAGS_ARCADE}" -a $ARCADE_ART -p "$SYSTEM" -f "${FRONTEND}" -i "${ROM_DIR}"/"${SYSTEM}"
        fi
    fi
done

## pcenginecd folder
if test -d "${ROM_DIR}"/pcenginecd; then
    if [ "$SCRAP" = true ]; then
        for SCRAPER in $CONSOLE_SCRAPERS; do
            Skyscraper --flags "${SCRAP_FLAGS_CONSOLE}" -p pcengine -s "$SCRAPER" -i "${ROM_DIR}"/pcenginecd
        done
    fi
    if [ "$BUILD" = true ]; then
        Skyscraper --flags "${BUILD_FLAGS_CONSOLE}" -a $CD_ART -p pcengine -f "${FRONTEND}" -i "${ROM_DIR}"/pcenginecd
    fi
fi

## arcade folder
if test -d "${ROM_DIR}"/arcade; then
    if [ "$SCRAP" = true ]; then
        for SCRAPER in $ARCADE_SCRAPERS; do
            Skyscraper --flags "${SCRAP_FLAGS_ARCADE}" -p fba -s "$SCRAPER" -i "${ROM_DIR}"/arcade
        done
    fi
    if [ "$BUILD" = true ]; then
        Skyscraper --flags "${BUILD_FLAGS_ARCADE}" -a $ARCADE_ART -p fba -f "${FRONTEND}" -i "${ROM_DIR}"/arcade
    fi
fi

## post-script job. add extensions text to pegasus
find "${ROM_DIR}" -type f -iname "metadata.pegasus.txt" -exec sed -i '2 a extensions: zip, chd, cue, iso, cso' {} \;
