#!/bin/sh
# Copyright 2016 Ghislain Antony Vaillant <ghisvail@gmail.com>
#
# This file is part of the autopkgtest testsuite for the libjsoncpp
# source package.

set -e

# Presence of $AUTOPKGTEST_TMP implies that someone will handle cleanup for us, so we
# can avoid duplicating the effort (signal handling, etc.) here.
if [ -z "$AUTOPKGTEST_TMP" ]
then
        echo "Required envvar \"$AUTOPKGTEST_TMP\"is not set" >&2
        exit 1
fi

# Copy testsuite source code.
cp -a ./src/test_lib_json/* "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"

if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then
    cat <<EOF > "$AUTOPKGTEST_TMP/toolchain.cmake"
set(CMAKE_C_COMPILER $DEB_HOST_GNU_TYPE-gcc)
set(CMAKE_CXX_COMPILER $DEB_HOST_GNU_TYPE-g++)
set(PKG_CONFIG_EXECUTABLE $DEB_HOST_GNU_TYPE-pkg-config)
EOF
    CCFILE=-DCMAKE_TOOLCHAIN_FILE="$AUTOPKGTEST_TMP/toolchain.cmake"
else
    CCFILE=
fi

cat <<EOF > CMakeLists.txt
cmake_minimum_required(VERSION 3.7)
project(jsoncpp_test)

find_package(jsoncpp REQUIRED)
add_executable(jsoncpp_test main.cpp jsontest.cpp jsontest.h fuzz.cpp fuzz.h)
target_link_libraries(jsoncpp_test PRIVATE JsonCpp::JsonCpp)

EOF

# Configure, build and execute.
mkdir build && cd build
cmake "$CCFILE" ..
make VERBOSE=1 -j$(nproc)
./jsoncpp_test
