#!/bin/sh

dsp_file="$1"
bin_file="$2"

CFLAGS="-g -O2 -Wall"

if test ! -f compiler.o 
then
	echo "Compile the compiler first"
	exit 1
fi
 
if test $# -lt 1
then
	echo "DSP compiler"
	echo "Usage:"
	echo "	$0 <program.dsp> [program.bin]"
	exit 1
fi

if test ! -f "$dsp_file"
then
	echo "file $dsp_file not found"
	exit 1
fi

if test $# -ne 2
then
	bin_file=`echo "$dsp_file" | sed -e 's/\.dsp$//'`
	bin_file=`echo "$bin_file".bin`
fi

if test "$dsp_file" = "$bin_file"
then
	echo "Source and destination files are the same"
	exit 1
fi

gcc $CFLAGS -D DSP_FILE="\"$dsp_file\"" main.c compiler.o -o tmp

if test -x ./tmp 
then
	./tmp "$bin_file"
	rm -f tmp
else
	echo "Could not compile $dsp_file"
	exit 1
fi

exit 0
