#!/bin/sh

if [ ! -z "$GSPLUS" ] && [ -x "$GSPLUS" ]
then
    EMULATORPATH="$GSPLUS"
elif [ ! -z "$GSPORT" ] && [ -x "$GSPORT" ]
then
    EMULATORPATH="$GSPORT"
fi

if [ -z "$EMULATORPATH" ]
then
    echo Unable to find GSplus or GSport at these locations.
    echo "    GSPLUS=$GSPLUS"
    echo "    GSPORT=$GSPORT"
    exit 1
fi

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

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

# This magic ensures that clicking stop in Xcode results in the emulator terminating.
if false
then
    cd make
    sed -i "" "s:^s7d1 *=.*$:s7d1 = $BOOTIMAGE:
s:^s5d1 *=.*:s5d1 = $DISKIMAGE:" config.txt
    $EMULATORPATH -fullscreen -mem 1572864 &
    PID=$!
else
    cd "/Users/jrand/Library/Application Support/Ample"
    /Applications/Ample.app/Contents/MacOS/mame64 apple2gs -skip_gameinfo -mouse -window -resolution 1408x1056 -ramsize 4M -sl3 uthernet -sl7 cffa2 -hard1 "$BOOTIMAGE" -flop3 "$DISKIMAGE" &
    PID=$!
fi

trap 'kill $PID' SIGTERM SIGINT SIGHUP EXIT
wait

exit 0
