#!/bin/bash

## usage
USAGE="USAGE:
$(basename "$0") source dest system"

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

readonly SYSTEM_LIST="console handheld computer cd_system arcade"

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

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

## vars
SOURCE="$(
    cd -- "$1" >/dev/null 2>&1 || exit
    pwd -P
)"
TARGET="$(
    cd -- "$2" >/dev/null 2>&1 || exit
    pwd -P
)"
readonly SOURCE
readonly TARGET

readonly HOMEBREW=/home/sergio/roms/homebrew
readonly TRANSLATIONS=/home/sergio/roms/translations
readonly HACKS=/home/sergio/roms/hacks

readonly CONSOLE="atari800 atari2600 atari5200 atari7800 coleco fds intellivision mastersystem megadrive n64 nes neogeo pcengine sega32x sg-1000 snes"
readonly HANDHELD="atarilynx gamegear gb gba gbc gc ngp ngpc virtualboy wonderswan wonderswancolor"
readonly COMPUTER="amiga amstradcpc apple2 atarist c16 c64 c128 msx x68000 zxspectrum"
readonly CD_SYSTEM="3do atarijaguar dreamcast megacd ngc pcfx pcenginecd ps2 psp psx saturn segacd wii"
readonly ARCADE="arcade cps1 cps2 neogeo"

case $3 in
console)
    SCAN_SYSTEM=$CONSOLE
    ;;

handheld)
    SCAN_SYSTEM=$HANDHELD
    ;;

computer)
    SCAN_SYSTEM=$COMPUTER
    ;;

cd_system)
    SCAN_SYSTEM=$CD_SYSTEM
    ;;

arcade)
    SCAN_SYSTEM=$ARCADE
    ;;

*)
    SCAN_SYSTEM=$CONSOLE
    ;;
esac

readonly TEXT1="añadiendo enlaces para "
readonly TEXT2=" ..."
C_BLUE=$(tput setaf 4)
readonly C_BLUE
C_NORMAL=$(tput sgr0)
readonly C_NORMAL

## console systems
for SYSTEM in $SCAN_SYSTEM; do
    ## check source
    if test -d "${SOURCE}"/"${SYSTEM}"; then
        printf "%s\n" "${TEXT1}${C_BLUE}${SYSTEM}${C_NORMAL}${TEXT2}"
        mkdir -p "${TARGET}"/"${SYSTEM}"
        for FILE in "${SOURCE}"/"${SYSTEM}"/*; do
            ln -s "${FILE}" "${TARGET}"/"${SYSTEM}"/"$(basename "${FILE}")"
        done
    fi

    ## check homebrew
    if test -d "${HOMEBREW}"/"${SYSTEM}"; then
        printf "%s\n" "${TEXT1}${C_BLUE}${SYSTEM}/_homebrew${C_NORMAL}${TEXT2}"
        mkdir -p "${TARGET}"/"${SYSTEM}"/_homebrew
        for FILE in "${HOMEBREW}"/"${SYSTEM}"/*; do
            ln -s "${FILE}" "${TARGET}"/"${SYSTEM}"/_homebrew/"$(basename "${FILE}")"
        done
    fi

    ## check translations
    if test -d "${TRANSLATIONS}"/"${SYSTEM}"; then
        printf "%s\n" "${TEXT1}${C_BLUE}${SYSTEM}/_translations${C_NORMAL}${TEXT2}"
        mkdir -p "${TARGET}"/"${SYSTEM}"/_translations
        for FILE in "${TRANSLATIONS}"/"${SYSTEM}"/*; do
            ln -s "${FILE}" "${TARGET}"/"${SYSTEM}"/_translations/"$(basename "${FILE}")"
        done
    fi

    ## check hacks
    if test -d "${HACKS}"/"${SYSTEM}"; then
        printf "%s\n" "${TEXT1}${C_BLUE}${SYSTEM}/_hacks${C_NORMAL}${TEXT2}"
        mkdir -p "${TARGET}"/"${SYSTEM}"/_hacks
        for FILE in "${HACKS}"/"${SYSTEM}"/*; do
            ln -s "${FILE}" "${TARGET}"/"${SYSTEM}"/_hacks/"$(basename "${FILE}")"
        done
    fi
done
