#!/bin/bash
#
# License:
#      Zipios -- a small C++ library that provides easy access to .zip files.
#
#      Copyright (c) 2015-2022  Made to Order Software Corp.  All Rights Reserved
#
#      This library is free software; you can redistribute it and/or
#      modify it under the terms of the GNU Lesser General Public
#      License as published by the Free Software Foundation; either
#      version 2.1 of the License, or (at your option) any later version.
#
#      This library is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#      Lesser General Public License for more details.
#
#      You should have received a copy of the GNU Lesser General Public License
#      along with this library; if not, write to the Free Software Foundation,
#      Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

set -e

if test "$1" = "--help" -o "$1" = "-h"
then
    echo "Usage: $0 [--opt] [test-name]"
    echo "where --opt is one of:"
    echo "   --all          run all tests"
    echo "   --full         run all tests and publish to the world"
    echo
    echo "to run specific tests, use one of these tags (can be used with --full, but probably should not):"
    echo "   [directoryentry]  run the DirectoryEntry class tests"
    echo "   [fileentry]       run all the tests that check the FileEntry interface"
    echo "   [filepath]        run the FilePath class tests"
    echo
    echo "for a complete list of tests use the --list command line option"
    echo "of the test executable:"
    echo "   zipios_tests --list"
    exit 1;
fi

if test "$1" = "--full"
then
    FULL=true
    shift
else
    FULL=false
fi

start_date=`date`
SOURCE_PATH=`pwd`
. dev/version
echo "***"
echo "*** zipios coverage for version $FULL_VERSION (`date`)"
echo "***"
mkdir -p ../BUILD/zipios_coverage
rm -rf ../BUILD/zipios_coverage/*
cd ../BUILD
BUILD_PATH=`pwd`
cd zipios_coverage
# request coverage in this build
cmake -DCMAKE_INSTALL_PREFIX:PATH=$BUILD_PATH/dist \
      -DCMAKE_BUILD_TYPE=Debug \
      -Dzipios_project_COVERAGE:BOOL=ON \
      -DCMAKE_MODULE_PATH:PATH=$SOURCE_PATH/cmake \
          ../../zipios
echo
echo "***"
echo "*** compile (`date`)"
echo "***"
VERBOSE=1 make
echo
echo "***"
echo "*** run (`date`)"
echo "***"
# todo:
# Catch does not give us such... we may want to bypass the limitation...
# For now I have a catch_version.cpp test at least
#if test `tests/zipios_tests --version` != "$FULL_VERSION"
#then
#    echo "the version of zipios_tests (`BUILD/tests/zipios_tests --version`) is not equal to the project version ($FULL_VERSION)"
#    exit 1;
#fi
if $FULL
then
    # We test the pipe status on exit to detect whether the test failed
    echo "Start running the tests on `date`" >test_log.txt
    echo >>test_log.txt
    # --success generates way too much output for HTML
    tests/zipios_tests --source-path "${SOURCE_PATH}" --durations yes 2>&1 | tee -a test_log.txt; test ${PIPESTATUS[0]} -eq 0
    echo >>test_log.txt
    echo "Finished running the tests on `date`" >>test_log.txt
else
    # "brief" test while working on a specific test
    if test "$1" == "--all" -o -z "$1"
    then
        # Do it all, but not published
        tests/zipios_tests --source-path "${SOURCE_PATH}"
    else
        tests/zipios_tests --source-path "${SOURCE_PATH}" $1
    fi
    # just in case, remove the log file if there is one
    rm -f test_log.txt
fi
cd ..

echo
echo "***"
echo "*** gcov/lcov (`date`)"
echo "***"


# The following lcov options can be used under Ubuntu 14.04+
# Use --no-external and --base-directory $SOURCE_PATH
# to avoid /usr/include and other unwanted files
# (only available in lcov version 1.10+)
lcov --capture --no-external --directory zipios_coverage --base-directory $SOURCE_PATH --output-file coverage.info
mkdir -p zipios_coverage_html
genhtml --legend --demangle-cpp --no-branch-coverage --show-details coverage.info --output-directory zipios_coverage_html


end_date=`date`

# Statistics
echo "<html><head><title>zipios $FULL_VERSION statistics</title></head><body>" >zipios_coverage_html/statistics.html
echo "<h3>Statistics of the zipios $FULL_VERSION code</h3><pre>" >>zipios_coverage_html/statistics.html
cloc $SOURCE_PATH/src/ $SOURCE_PATH/tools/ $SOURCE_PATH/zipios/ >>zipios_coverage_html/statistics.html
echo "</pre><h3>Statistics of the zipios $FULL_VERSION tests</h3><pre>" >>zipios_coverage_html/statistics.html
cloc $SOURCE_PATH/tests/ >>zipios_coverage_html/statistics.html
echo "</pre></body></html>" >>zipios_coverage_html/statistics.html

# Test output (Logs)
echo "<html><head><title>zipios $FULL_VERSION test logs</title></head><body><h3>Logs for the zipios $FULL_VERSION tests</h3><p>Tests started on $start_date and finished on $end_date</p><pre>" >zipios_coverage_html/test_log.html
if test -f zipios_coverage/test_log.txt
then
    # If test_log.txt does not exist, the user got the logs in the
    # console already
    cat zipios_coverage/test_log.txt >>zipios_coverage_html/test_log.html
fi
echo "</pre></body></html>" >>zipios_coverage_html/test_log.html


if test -f zipios_coverage/test_log.txt
then
    echo "***"
    echo "*** publication to ... ($end_date)"
    echo "***"

    # For publication, if that directory does not exist, you probably don't
    # have a website to display this data
    if test -d /usr/clients/www/alexis.m2osw.com/public_html/zipios
    then
        mkdir -p /usr/clients/www/alexis.m2osw.com/public_html/zipios/documentation
        cp -r zipios_coverage/doc/zipios-doc-*/* /usr/clients/www/alexis.m2osw.com/public_html/zipios/documentation/.
        cp $SOURCE_PATH/dev/index.php /usr/clients/www/alexis.m2osw.com/public_html/zipios/.
        mkdir -p /usr/clients/www/alexis.m2osw.com/public_html/zipios/zipios-$FULL_VERSION
        cp -r zipios_coverage_html/* /usr/clients/www/alexis.m2osw.com/public_html/zipios/zipios-$FULL_VERSION/.
        cp zipios_coverage_html/statistics.html /usr/clients/www/alexis.m2osw.com/public_html/zipios/zipios-$FULL_VERSION/.
    fi
fi

echo "Process started  on $start_date"
echo "Process finished on $end_date"

# vim: ts=4 sw=4 et
