#!/bin/bash

# Exit on any failure:
set -e

# Verbose:
set -x

if [ "$1" != "" ]; then
	BUILDCONFIG="$*"
fi

export MAKE="make -j3"
export CPPFLAGS="-DFAKELAG_CONFIGURABLE"

extras/build-tests/nix/select-config $BUILDCONFIG
./Config -quick || (tail -n 5000 config.log; echo '*** now tre:'; tail -n 5000 extras/tre-0.8.0-git/config.log; echo '** end of tre config.log **'; exit 1)
$MAKE
yes ''|make pem
make install

set +x
echo ""
echo "***********************************************"
echo "UnrealIRCd build & install successful."
echo "***********************************************"

if [[ "$OSTYPE" == "darwin"* ]]; then
	otool -L ~/unrealircd/bin/unrealircd
else
	ldd ~/unrealircd/bin/unrealircd
fi

if [ "$BUILDCONFIG" = "local-curl" ]; then
	# We broke the system significantly earlier that build tests
	# won't work (eg: 'git' is uninstallable). Don't bother
	# running the tests. I don't expect a 'local-curl' specific
	# bug anyway.
	exit 0
fi

echo ""
echo ""
echo "Now running UnrealIRCd test framework..."
set -x
extras/build-tests/nix/run-tests
