#!/bin/sh
# Usage:
# runtest [-v]

. ./testing.sh

total_failed=0

: "${tsdir:=$PWD}"
: "${bindir:=${PWD%/*}}" # one directory up from $PWD

if [ "${1-}" = "-v" ]; then
	VERBOSE=1
	shift
fi

export VERBOSE="${VERBOSE-}"

# Test whether the binary has non-POSIX extensions enabled.
# Specifically, test whether double-colon rules are supported.
unset PDPMAKE_POSIXLY_CORRECT
unset MAKEFLAGS
export OPTIONFLAGS=":"
if ../make -f - 2>/dev/null <<EOF
target::
	@:
target::
	@:
EOF
then
	OPTIONFLAGS="${OPTIONFLAGS}FEATURE_MAKE_EXTENSIONS:"
fi

# Test whether the binary has POSIX 2024 features enabled.
# Specifically, test whether pattern macro expansion is supported.
OBJ=$(../make -f - 2>/dev/null <<EOF
SRC = src/util.c
target:
	@echo \$(SRC:src/%.c=obj/%.o)
EOF
)
if [ "$OBJ" = "obj/util.o" ]
then
	OPTIONFLAGS="${OPTIONFLAGS}FEATURE_MAKE_POSIX_2024:"
fi

if [ "${OPTIONFLAGS}" = ":" ]
then
	OPTIONFLAGS="::"
fi

# The feature tests above required PDPMAKE_POSIXLY_CORRECT to be
# unset.  To run the tests in POSIX mode uncomment the following.
# export PDPMAKE_POSIXLY_CORRECT=1
status=0
PATH="$tsdir:$bindir:$PATH" \
	"$tsdir/make.tests"
rc=$?
total_failed=$((total_failed + rc))
test $rc -ne 0 && status=1

if [ $status -ne 0 ] && [ -z "$VERBOSE" ]; then
	echo "$total_failed failure(s) detected; running with -v (verbose) will give more info"
fi
exit $status
