#!/bin/bash

#
#  It turned out that the assembler needs some kind of a preprocessor.
# Since I am too lazy to write one I decided to use the cpp which is
# the purpose of this small wrapper script. Assembling some QNICE
# sources is done invoking this script with the source file name as 
# its only parameter.
#

temp_file=__temp__.asm

if [ $# -ne 1 ]
then
  echo "Usage: asm <source.asm>"
  exit
fi

assembler=`dirname $0`/qasm
makerom=`dirname $0`/qasm2rom

destination=${1/.asm/}.out
romfile=${1/.asm/}.rom

rm $destination 2> /dev/null
rm $romfile 2> /dev/null

#
#  We use the standard C-preprocessor to perform the necessary 
# preprocessor steps for the QNICE-assembler. Since the preprocessor
# inserts lines starting with a '#' which the assembler doest
# not like, these are subsequently removed with sed.
#
gcc -E $1 | sed '/^#.*/d' > $temp_file

$assembler $temp_file $destination
if [ $? -ne 0 ]
then
  echo "An unrevoverable error occured!"
  rm $temp_file
  exit -1
fi

rm $temp_file

$makerom $destination $romfile

cat $destination | pbcopy
