
#!/bin/env bash
# Checks whether the compiler generated by create_dmd_release
# contains everything needed to compile & execute D programms.

set -eux

# Working directory
DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
GEN="$DIR/generated"

mkdir -p $GEN

###############################################################################
# Extract the release and determine the targets to test
if [ "$OS" == "windows" ]
then
	7z x -y $BUILD -o"$GEN/"

	TARGETS="-m32 -m32mscoff -m64"
	EXE=".exe"
else
	tar xf $BUILD --directory="$GEN/"

	TARGETS="-m64"
	EXE=""
fi

###############################################################################
# Validate that DMD can build working programs

for DMD in $GEN/dmd2/$OS/bin*/dmd$EXE
do
	$DMD --version

	for TARGET in $TARGETS
	do
		$DMD $TARGET -of=$GEN/hello $DIR/hello.d
		$GEN/hello
	done
done

###############################################################################
# Validate that the bundled tools work


for RDMD in $GEN/dmd2/$OS/bin*/rdmd$EXE
do
	$RDMD -m64 $DIR/hello.d
done

for DDEMANGLE in $GEN/dmd2/$OS/bin*/ddemangle$EXE
do
	echo "_D5hello3fooFZv" | $DDEMANGLE | grep "hello.foo"
done

for DUSTMITE in $GEN/dmd2/$OS/bin*/dustmite$EXE
do
	$DUSTMITE --version
done

for DUB in $GEN/dmd2/$OS/bin*/dub$EXE
do
	# FIXME: https://github.com/dlang/dub/issues/3030
	if [ "$OS" != "windows" ]; then
		$DUB run -n --arch=x86_64 --single $DIR/dub_example.d
		rm -rf $DIR/.dub $DIR/dub_example$EXE
	fi
done

###############################################################################
# Remove all temporary files / changes

rm -r $GEN/*
