#!/bin/sh
# This script is used for testing the build, primarily for use
# with GitLab CI, but may be used by hand as well.

set -e
set -x

# Test autoconf build
autoconf_build()
{
    autoreconf -ivf

    mkdir autoconf-build
    cd autoconf-build
    echo "Running ../configure --prefix=$(pwd)/../autoconf-install) ${opts}"
    ../configure --prefix=$(pwd)/../autoconf-install ${opts}
    make
    make install
    make check
    make distcheck
    mkdir ../distribution
    cp tiff-*.tar.* tiff*.zip ../distribution
}

# Test autoconf build (minimal; no distcheck)
autoconf_build_minimal()
{
    autoreconf -ivf

    mkdir autoconf-build
    cd autoconf-build
    echo "Running ../configure --prefix=$(pwd)/../autoconf-install) ${opts}"
    ../configure --prefix=$(pwd)/../autoconf-install ${opts}
    make
    make install
    make check
}

# Test cmake build
cmake_build()
{
    PATH="$(pwd)/tools/bin:$PATH"
    if [ "$(uname -s)" = "Darwin" ]; then
        PATH="$(pwd)/tools/CMake.app/Contents/bin:$PATH"
    fi
    mkdir cmake-build
    cd cmake-build
    opts="-Dfatal-warnings=ON"
    para3=`echo "$3" | sed 's/./\L&/g'`
    if [ "$para3" = "static" ]; then
        opts2="-DBUILD_SHARED_LIBS:BOOL=OFF"
    else
        opts2=""
    fi
    echo "Running cmake -G "$1" -DCMAKE_BUILD_TYPE="$2" -DCMAKE_INSTALL_PREFIX=../cmake-install ${opts} ${opts2} .."
    cmake -G "$1" -DCMAKE_BUILD_TYPE="$2" -DCMAKE_INSTALL_PREFIX=../cmake-install ${opts} ${opts2}  ..
    $COVERITY_BUILD cmake --build .
    cmake --build . --target install
    ctest -V
    cd ..
    if [ "$para3" = "static" ]; then
        echo No test project build for STATIC CI/CD build - skipped -.
    else
        # Build test project using find_package(Tiff CONFIG)
        mkdir cmake-test-build
        cd cmake-test-build
        cmake -G "$1" -DCMAKE_BUILD_TYPE="$2" "-DTiff_DIR=$PWD/../cmake-install/lib/cmake/tiff" -S ../build/test_cmake -B .
        cmake --build .
        cd ..
        mkdir cmake-test-no-target-build
        cd cmake-test-no-target-build
        cmake -G "$1" -DCMAKE_BUILD_TYPE="$2" "-DTiff_DIR=$PWD/../cmake-install/lib/cmake/tiff" -S ../build/test_cmake_no_target -B .
        cmake --build .
        # Return to cmake-build for coverity_build()
        cd ../cmake-build
    fi
}

# Static-analysis with coverity
coverity_build()
{
    curl -o /tmp/cov-analysis-linux64.tgz \
        https://scan.coverity.com/download/linux64 \
        --form project=$COVERITY_SCAN_PROJECT_NAME \
        --form token=$COVERITY_SCAN_TOKEN
    tar xfz /tmp/cov-analysis-linux64.tgz
    COVERITY_BUILD="$(pwd)/cov-analysis-linux64-*/bin/cov-build --dir cov-int" cmake_build "$@"
    tar cfz cov-int.tar.gz cov-int
    curl https://scan.coverity.com/builds?project=$COVERITY_SCAN_PROJECT_NAME \
        --form token=$COVERITY_SCAN_TOKEN \
        --form email=$GITLAB_USER_EMAIL \
        --form file=@cov-int.tar.gz \
        --form version="`git describe --tags`" \
        --form description="`git describe --tags` / $CI_COMMIT_TITLE / $CI_COMMIT_REF_NAME:$CI_PIPELINE_ID"
}


build=$1
shift

case $build in
    autoconf)
        echo "Testing Autoconf build"
        autoconf_build "$@"
        ;;
    autoconf-minimal)
        echo "Testing Autoconf build (minimal)"
        autoconf_build_minimal "$@"
        ;;
    cmake)
        echo "Testing CMake build"
        cmake_build "$@"
        ;;
    coverity)
        echo "Static analysis with Coverity"
        coverity_build "$@"
        ;;
    *)
        echo "Invalid argument: \"$arg\"" >&2
        exit 1
        ;;
esac

exit 0
