#!/bin/bash
ARG=()
OPT=
for i in ${@}; do
	if [ "${i:0:1}" = "-" ]; then
		OPT+=$i
	else
		ARG+=($i)
	fi
done
if test "${ARG[0]}" == ""; then exit 1; fi
F="${ARG[0]%.*}"
R1="art2asm-L64A.run $F.art $F.s"
R2="as --64 -o $F.o $F.s"
R3="ld -o $F.run $F.o"
R4="rm $F.s $F.o dump.lex"
if [[ "$OPT" == *@(P|p)* ]]; then
	R1="art2asm-L64I.pl arthely-L64I.txt $F.art $F.asm"
	R2="yasm -f elf64 -o $F.o $F.asm"
#	R4="rm $F.asm $F.o $F.lex"
	R4="echo (intermediary files retained)"
else
	if [[ "$OPT" == *@(I|i)* ]]; then
		R1="art2asm-L64I.run $F.art $F.asm"
		R2="nasm -f elf64 -o $F.o $F.asm"
		R4="rm $F.asm $F.o dump.lex"
	fi
fi
$R1
if test "$?" != "0"; then exit 1; fi
echo ...translated...
$R2
if test "$?" != "0"; then exit 1; fi
echo ...assembled...
$R3
if test "$?" != "0"; then exit 1; fi
echo ...linked...
$R4
if test "$?" != "0"; then exit 1; fi
echo ...compiled...
strip $F.run
echo ...done...
