#!/bin/sh
set -e

backend="$GHDL_BACKEND"
bindir=$(dirname $0)

if [ ! -x "${bindir}/ghdl-$backend" ]; then
	if [ -x ${bindir}/ghdl-mcode ]; then
		backend=mcode
	elif [ -x ${bindir}/ghdl-gcc ]; then
		backend=gcc
	elif [ -x ${bindir}/ghdl-llvm ]; then
		backend=llvm
	else
		echo >&2 "Error: No installed ghdl backend found. Terminating!"
		exit 2
	fi
fi

exec "${bindir}/ghdl-$backend" "$@"
