#! /bin/sh

SDL="no"
DL="no"

echo Removing old files...
./clean

echo Checking prerequisites...
gcc _build/test.c -o a.out `sdl-config --cflags --libs` 2>/dev/null && SDL="yes" && rm a.out*
gcc _build/test.c -o a.out -ldl 2>/dev/null && DL="yes" && rm a.out*

echo Building Toka...
cd toka
if [ "$DL" = "yes" ]; then
  gcc -ldl *.c -o toka
else
  gcc *.c -o toka
fi
cd ..
mv toka/toka bin


echo Building Ngaro...
cd ngaro
if [ "$1" == "--tty" ]; then
  gcc disassemble.c endian.c loader.c ngaro.c tty_devices.c vm.c -o ngaro
else
  if [ "$SDL" = "no" ]; then
    echo SDL Libraries not found. Please visit http://libsdl.org to obtain a copy.
    echo Will build console version of Ngaro
    gcc disassemble.c endian.c loader.c ngaro.c tty_devices.c vm.c -Wall -o ngaro
  else
    gcc disassemble.c endian.c loader.c ngaro.c sdl_devices.c vm.c -DUSE_SDL -Wall `sdl-config --cflags --libs` -o ngaro
  fi
fi
cd ..
mv ngaro/ngaro bin


echo Building forth.image
cd retro
ln -s ../bin/bootstrap.toka
../bin/toka retro.toka
rm bootstrap.toka
cd ..
mv retro/forth.image bin
mv retro/forth.image.map bin

echo All done!
