#!/bin/bash
HERE="$(dirname "$(readlink -f "${0}")")"/../..

export PATH="$HERE"/bin:"$HERE"/usr/bin:"$PATH"
export LD_LIBRARY_PATH="$HERE"/usr/lib:"$LD_LIBRARY_PATH"
export ZENITY=$(command -v zenity)

if [ -z ${SHIP_HOME+x} ]; then
export SHIP_HOME=$PWD
fi

if [ -z ${SHIP_BIN_DIR+x} ]; then
export SHIP_BIN_DIR="$HERE/usr/bin"
fi

if [[ ! -e "$SHIP_HOME"/mods ]]; then
    mkdir -p "$SHIP_HOME"/mods
    touch "$SHIP_HOME"/mods/custom_mod_files_go_here.txt
fi

while [[ (! -e "$SHIP_HOME"/mm.o2r) ]]; do
    for romfile in "$SHIP_HOME"/*.*64
    do
        if [[ -e "$romfile" ]] || [[ -L "$romfile" ]]; then
            export ASSETDIR="$(mktemp -d /tmp/assets-XXXXX)"
            ln -s "$SHIP_BIN_DIR"/{assets,2s2h.elf,ZAPD} "$ASSETDIR"
            export OLDPWD="$PWD"
            mkdir -p "$ASSETDIR"/tmp
            if [[ -e "$romfile" ]]; then
                ln -s "$romfile" "$ASSETDIR"/tmp/rom.z64
            else
                ORIG_ROM_PATH=$(readlink "$romfile")
                ln -s "$ORIG_ROM_PATH" "$ASSETDIR"/tmp/rom.z64
            fi
            cd "$ASSETDIR"
            ROMHASH=$(sha1sum -b "$ASSETDIR"/tmp/rom.z64 | awk '{ print $1 }')

            # Remap v64 and n64 hashes to their z64 hash equivalent
            # ZAPD will handle converting the data into z64 format
            case "$ROMHASH" in
            ef6d7273a7bc1cbf778f9021753db5efddfe495e) # v64
                ROMHASH=7f5630dbc4d5d61d6276213210c4d5cdd83a47d6
                ;;
            991460c32f53c7bb0bc650baf5a40ffde6165840) # n64
                ROMHASH=7f5630dbc4d5d61d6276213210c4d5cdd83a47d6
                ;;
            22f8bfa1ab6c763013a3c2a568bb014395b12499) # v64
                ROMHASH=d6133ace5afaa0882cf214cf88daba39e266c078
                ;;
            1a26735c63cb3bdf5eb32d21acf774ee6724f227) # n64
                ROMHASH=d6133ace5afaa0882cf214cf88daba39e266c078
                ;;
            8c378b87c83b3f4de20b14accf91e7590399f5dc) # v64
                ROMHASH=9743aa026e9269b339eb0e3044cd5830a440c1fd
                ;;
            8b0436e540b95a5353969d4ee360273cf33fb659) # n64
                ROMHASH=9743aa026e9269b339eb0e3044cd5830a440c1fd
                ;;
            esac

            case "$ROMHASH" in
            7f5630dbc4d5d61d6276213210c4d5cdd83a47d6) # uncompressed
                if [[ ! -e "$SHIP_HOME"/mm.o2r ]]; then
                    ROM=N64_US
                else
                    continue
                fi
                ;;
            d6133ace5afaa0882cf214cf88daba39e266c078) # compressed
                if [[ ! -e "$SHIP_HOME"/mm.o2r ]]; then
                    ROM=N64_US
                else
                    continue
                fi
                ;;
            9743aa026e9269b339eb0e3044cd5830a440c1fd) # compressed
                if [[ ! -e "$SHIP_HOME"/mm.o2r ]]; then
                    ROM=GC_US
                else
                    continue
                fi
                ;;
            *)
                echo -e "\n$romfile - $ROMHASH rom hash does not match\n"
                continue;;
            esac
            if [[ ! -e "$SHIP_HOME"/mm.o2r ]]; then
                if [ -n "$ZENITY" ]; then
                    (echo "# 25%"; echo "25"; sleep 7; echo "# 50%"; echo "50"; sleep 7; echo "# 75%"; echo "75"; sleep 7; echo "# 100%"; echo "100"; sleep 7) |
                    zenity --progress --title="OTR Generating..." --timeout=28 --percentage=0 --icon-name=2s2h --window-icon=2s2h.png --height=80 --width=400 &
                else
                    echo "Processing..."
                fi
                assets/extractor/ZAPD.out ed -eh -i assets/extractor/xmls/"${ROM}" -b tmp/rom.z64 -fl assets/extractor/filelists -o placeholder -osf placeholder -gsf 1 -rconf assets/extractor/Config_"${ROM}".xml -se OTR --otrfile mm.o2r --portVer "@CMAKE_PROJECT_VERSION@" > /dev/null 2>&1
                cp "$ASSETDIR"/mm.o2r "$SHIP_HOME"
            fi
        else
            if [[ (! -e "$SHIP_HOME"/mm.o2r) ]]; then
                if [ -n "$ZENITY" ]; then
                    zenity --error --timeout=5 --text="Place ROM in $SHIP_HOME" --title="Missing ROM file" --width=500 --width=200
                else
                    echo -e "\nPlace ROM in this folder\n"
                fi
                exit
            fi
        fi
    done
    if [[ (! -e "$SHIP_HOME"/mm.o2r) ]]; then
        if [ -n "$ZENITY" ]; then
            zenity --error --timeout=10 --text="No valid ROMs were provided, No OTR was generated." --title="Incorrect ROM file" --width=500 --width=200
        else
            echo "No valid roms provided, no OTR was generated."
        fi
        rm -r "$ASSETDIR"
        exit
    else
        (cd "$SHIP_BIN_DIR"; ./2s2h.elf)
        exit
    fi
    rm -r "$ASSETDIR"
done
    (cd "$SHIP_BIN_DIR"; ./2s2h.elf)
exit
