#!/bin/sh

set -e

readonly wrkdir="$(mktemp -d /tmp/irccd-XXXXXX)"
readonly repo="http://hg.malikania.fr/irccd"

trap "cleanup" INT TERM EXIT

cleanup()
{
	rm -rf $wrkdir
}

if [ "$#" -ne 1 ]; then
	echo "abort: $(basename $0) revision" 1>&2
	exit 1
fi

case $(uname -s) in
Darwin)
	extra_args="-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl"
	;;
esac

echo "=> Cloning repository $repo (revision $1) into $wrkdir"
hg clone -r "$1" "$repo" "$wrkdir"
cd "$wrkdir"

echo "=> Configuring CMake"
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DIRCCD_WITH_TESTS=On $extra_args

echo "=> Building"
cmake --build build

echo "=> Running test suite"
cmake --build build --target test
