#!/bin/sh
TARGET=$1
DIR=build-"$TARGET"
CONFIGURE=do-"$TARGET"-configure
shift

for p in /opt/*/bin; do
    PATH="$p":$PATH
done

ANALYZER="-Danalyzer=true"

CDEFS="-Dtests-cdefs=true"
TEST="test"

# Disable analyzer on m68k because the compiler gets very confused
# about complex values

case "$TARGET" in
    *m68k*)
	ANALYZER=""
	;;
esac

# Disable the analyser when not using tinystdio as the stdio code is
# full of problems

case "$*" in
    *-Dtinystdio=false*)
	ANALYZER=""
	;;
esac

mkdir "$DIR"
trap 'rm -rf "$DIR"' 0 1 15
(cd "$DIR" || exit 1
 echo '##################################################'
 echo '##########' ../scripts/"$CONFIGURE" -Dwerror=true $ANALYZER $CDEFS "$@"
 echo '##################################################'
 ../scripts/"$CONFIGURE" -Dwerror=true $ANALYZER $CDEFS "$@"
 case $? in
     0)
	 echo 'Configuration succeeded'
	 ;;
     77)
	 echo 'Configuration skipped'
	 exit 0
	 ;;
     *)
	 echo 'Configuration failed with' "$?"
	 cat meson-logs/*
	 exit 1
	 ;;
 esac
 cat meson-logs/*
 ninja $TEST || exit 1) || exit 1
