#!/bin/bash
USAGE="
USAGE:
$(basename "$0") [BUILD or SCRAP] [PATH] [SYSTEM]"

function help_message() {
    printf "%s\n\n" "$USAGE"
    printf "%s" "Where [SYSTEM] are: "
    for WORD in $SYSTEMS; do
        printf "%s " "$WORD"
    done
    printf "\n"
}
readonly USAGE

readonly SYSTEMS="3do 3ds amiga amstradcpc apple2 arcade arcadia astrocde atari800 atari2600 atari5200 atari7800 atarijaguar atarilynx atarist c16 c64 c128 coco coleco daphne dragon32 dreamcast fba fds gameandwatch gamegear gb gba gbc gc genesis intellivision mame-advmame mame-libretro mame-mame4all mastersystem megacd megadrive msx n64 nds neogeo nes ngp ngpc oric pc pc88 pc98 pcfx pcengine pokemini ports ps2 psp psx saturn scummvm sega32x segacd sg-1000 snes steam ti99 trs-80 vectrex vic20 videopac virtualboy wii wonderswan wonderswancolor x68000 x1 zmachine zx81 zxspectrum"
readonly APP=/usr/local/bin/Skyscraper

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

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

## 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

## variables
#readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
readonly ROM_DIR="$2"
readonly SYSTEM_NAME="$3"
readonly FRONTEND="emulationstation"

## artwork
readonly CONSOLE_ART=artwork.xml
readonly ARCADE_ART=artwork.xml

## scrapers
readonly CONSOLE_SCRAPERS="screenscraper mobygames thegamesdb"
readonly ARCADE_SCRAPERS="screenacraper 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"

## systems
readonly CONSOLE="3do 3ds amiga amstradcpc apple2 arcadia astrocde atari800 atari2600 atari5200 atari7800 atarijaguar atarilynx atarist c16 c64 c128 coco coleco daphne dragon32 dreamcast fds gameandwatch gamegear gb gba gbc gc genesis intellivision mastersystem megacd megadrive msx n64 nds nes ngp ngpc oric pc pc88 pc98 pcfx pcengine pokemini ports ps2 psp psx saturn scummvm sega32x segacd sg-1000 snes ti99 trs-80 vectrex vic20 videopac virtualboy wii wonderswan wonderswancolor x68000 x1 zmachine zx81 zxspectrum"
readonly ARCADE="arcade fba neogeo mame-advmame mame-mame4all"

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

## console systems
if echo "${CONSOLE}" | grep -w "${SYSTEM_NAME}" >/dev/null; then
    if [ "${SCRAP}" = true ]; then
        for SCRAPER in ${CONSOLE_SCRAPERS}; do
            $APP --flags "${SCRAP_FLAGS_CONSOLE}" -p "${SYSTEM_NAME}" -s "${SCRAPER}" -i "${ROM_DIR}"/"${SYSTEM_NAME}"
        done
    fi
    if [ "${BUILD}" = true ]; then
        $APP --flags "${BUILD_FLAGS_CONSOLE}" -a $CONSOLE_ART -p "${SYSTEM_NAME}" -f "${FRONTEND}" -i "${ROM_DIR}"/"${SYSTEM_NAME}"
    fi
    exit 0
fi

## arcade systems
if echo "${ARCADE}" | grep -w "${SYSTEM_NAME}" >/dev/null; then
    if [ "${SCRAP}" = true ]; then
        for SCRAPER in ${ARCADE_SCRAPERS}; do
            Skyscraper --flags "${SCRAP_FLAGS_ARCADE}" -p "${SYSTEM_NAME}" -s "${SCRAPER}" -i "${ROM_DIR}"/"${SYSTEM_NAME}"
        done
    fi
    if [ "${BUILD}" = true ]; then
        Skyscraper --flags "${BUILD_FLAGS_ARCADE}" -a $ARCADE_ART -p "${SYSTEM_NAME}" -f "${FRONTEND}" -i "${ROM_DIR}"/"${SYSTEM_NAME}"
    fi
    exit 0
fi

## pcenginecd folder
if [ "${SYSTEM_NAME}" = pcenginecd ]; then
    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 $CONSOLE_ART -p pcengine -f "${FRONTEND}" -i "${ROM_DIR}"/pcenginecd
        fi
    fi
fi

## arcade folder
if [ "${SYSTEM_NAME}" = arcade ]; then
    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
fi
