#!/bin/bash

set -o errexit

cd /home/cgal-testsuite

# Rotate log files on one month: the logfile name contains the number of
# the day
LOGFILE=$PWD/doxygen_testsuite-`date '+%d'`.log

exec > "$LOGFILE"

# Display commands as if using `set -o xtrace`, but to the stdout
trap 'echo "[$BASH_SOURCE:$LINENO] $BASH_COMMAND" >&1' DEBUG

# A helper error function that outputs both to stderr and stdout before
# aborting the script.
function error() {
    echo "Error: $@"
    echo "Error: $@" >&2
    echo "See $LOGFILE" >&2
    exit 1
}

DIR=`readlink /home/lrineau/latest-internal-release-branch`

rsync -a --delete '--exclude=.git*' "$DIR/" integration
mkdir /home/cgal-testsuite/integration/build_doc
cd /home/cgal-testsuite/integration/build_doc

PYTHONPATH=/home/cgal-testsuite/.local/lib/python2.6/site-packages
export PYTHONPATH
PATH=/home/cgal-testsuite/local/bin:$PATH
export PATH

cmake \
    -DBUILD_DOC:BOOL=ON \
    -DCGAL_DOC_CREATE_LOGS:bool=ON \
    -DCGAL_DOC_MATHJAX_LOCATION:STRING=../../MathJax \
    -DCGAL_DOC_RELEASE:BOOL=TRUE \
    -DCGAL_DOC_PUBLISH_DIR=/srv/CGAL/www/Members/Manual_doxygen_test \
    -DDOXYGEN_EXECUTABLE:FILEPATH=/home/cgal-testsuite/local/bin/doxygen \
    -DPYTHON_EXECUTABLE:FILEPATH=/home/cgal-testsuite/local/bin/python2 \
    .. 2>&1 || error "exit code $? returned by CMake"
# Note that -DCGAL_DOC_RELEASE:BOOL=TRUE should be only for master-only
# internal releases, and not for integration releases.

make doc 2>&1 || error "exit code $? returned by the command 'make doc'"
make doc_and_publish_testsuite 2>&1 || error "exit code $? returned by the command 'make doc_and_publish_testsuite'"

# Then gzip the log file, to save space
exec
gzip -f "$LOGFILE"
