#!/bin/sh

#[ "$1" = --version ] && exec /usr/local/bin/powerpc-bf1 --version
if [ "$1" = --version ]; then
	echo "Rivoreo Brainfuck for PowerPC"
	exit
fi

flags=
[ -n "$DEBUG" ] && flags="$flags -g"
[ -n "$OPTIMIZATION" ] && flags="$flags -O $OPTIMIZATION"
[ -n "$VERBOSE" ] && flags="$flags -v"
case "$ONLY_STAGE" in
	E)
		echo "warning: this language has no preprocessing stage" 1>&2
		exec cat
		;;
	S)
		exec /usr/local/bin/powerpc-bf1 $flags "$@"
		;;
esac
set -e
output_object_file="`mktemp -t bf.XXXXXXXX.o`"
trap 'rm -f "$output_object_file"' EXIT
/usr/local/bin/powerpc-bf1 $flags "$@" | as -m regnames -o "$output_object_file"
case "$ONLY_STAGE" in
	c)
		cat "$output_object_file"
		;;
	"")
		output_executable_file="${output_object_file%.o}"
		rm -f "$output_executable_file"
		trap 'rm -f "$output_object_file" "$output_executable_file"' EXIT
		ld "$output_object_file" -o "$output_executable_file"
		cat "$output_executable_file"
		;;
esac
