#!/bin/sh

TMPDIR="/tmp/a2gs_emulator.$$"

cleanupAndExit()
{
    cd
    if [ ! -z $PID ]
    then
        kill $PID 2> /dev/null
    fi
    rm -rf $TMPDIR
    exit 0
}

createConfigTxt()
{
    mkdir "$TMPDIR"
    sed "s:^s7d1 *=.*$:s7d1 = $BOOTIMAGE:
s:^s5d1 *=.*:s5d1 = $DISKIMAGE:" < make/config.txt > "$TMPDIR/config.txt"
    
}

if [ $# -ne 2 ]
then
    echo USAGE $0: diskimage bootimage
    exit 1
fi

DISKIMAGE="$1"
if echo $DISKIMAGE | grep -v '^/' > /dev/null
then
    DISKIMAGE="$PWD/$DISKIMAGE"
fi

BOOTIMAGE="$2"
if echo $BOOTIMAGE | grep -v '^/' > /dev/null
then
    BOOTIMAGE="$PWD/$BOOTIMAGE"
fi

case "$EMULATOR" in
    gsplus)
        EMULATORPATH="$GSPLUS"
        EMULATORARGS="$GSPLUSARGS"
        EMULATORDIR="$TMPDIR"
        createConfigTxt
        ;;
    gsport)
        EMULATORPATH="$GSPORT"
        EMULATORARGS="$GSPORTARGS"
        EMULATORDIR="$TMPDIR"
        createConfigTxt
        ;;
    mame)
        EMULATORPATH="$MAME"
        EMULATORARGS="$MAMEARGS -hard1 "$BOOTIMAGE" -flop3 "$DISKIMAGE""
        EMULATORDIR="$MAMELIB"
        ;;
    *)
        echo Invalid emulator specified, \"$EMULATOR\"
        exit 1
        ;;
esac

if [ ! -x "$EMULATORPATH" ]
then
    echo Unable to find $EMULATOR at $EMULATORPATH
    exit 1
fi

if [ ! -d "$EMULATORDIR" ]
then
    echo Unable to find $EMULATOR library at $EMULATORDIR
    exit 1
fi

cd "$EMULATORDIR"
# This magic ensure that clicking stop in Xcode results in the emulator terminating.
"$EMULATORPATH" $EMULATORARGS &
PID=$!

trap cleanupAndExit SIGTERM SIGINT SIGHUP EXIT
wait
