#!/bin/sh

# Execute either the real dasm, or dasm2atasm + atasm.
# If we have dasm on the PATH, use it, otherwise fake it.
# dasm2atasm is NOT perfect! It does however work OK for
# fenders.dasm, fendersdbl.dasm, and loadscreen.dasm.

# The .syms file format is totally different for dasm and atasm, but
# fenders_offsets.pl can handle either one (adapt for your purposes).

# If you have dasm, but want to force atasm, pass any non-empty string
# as the second argument to this script.

# Could also use "dasm2atasm -c" and ca65/ld65 to assemble, but I can't
# seem to get ca65 or ld65 to emit a symbol file or listing (the -l
# option doesn't work; the -m option doesn't do what I expect), so
# I don't know any way to extract the OFFSET_* labels from ca65's output.

if [ -z "$1" ]; then
	echo "$0: missing argument"
	exit 1
fi

if [ -n "`which dasm 2>/dev/null`" -a -z "$2" ] ; then
	exec dasm $1.dasm -f3 -s$1.syms -o$1.bin
elif [ -n "`which atasm 2>/dev/null`" ]; then
	ln -sf equates.inc equates.m65
	perl dasm2atasm $1.dasm $1.atasm
	exec atasm -r -s -o$1.bin $1.atasm > $1.syms
else
	echo "$0: you need either dasm or atasm on your PATH"
	exit 1
fi

exit 0
