#!/bin/bash

TEST_NAME="\"SSSD make check coverage\""

SSSD_SRC_DIR=/home/jakub/devel/sssd
COV_DIR=/home/jakub/devel/coverage
COV_FILE_IN=$COV_DIR/coverage.info.in
COV_FILE=$COV_DIR/coverage.info
COV_HTML=$COV_DIR/html

REMOTE_USER=jhrozek
REMOTE_HOST=fedorapeople.org
REMOTE_PATH=public_html/sssd-coverage

pushd $SSSD_SRC_DIR || exit 1

# Remove any old crap
git clean -fx
find $SSSD_SRC_DIR -name \*.gcda -o -name \*.gcno  | xargs rm -f

# Make sure the code is up-to-date
git checkout review
git fetch origin
git reset --hard origin/master

# TODO - if any patches are given on as arguments, apply them
until [ -z "$1" ]
do
    git am $1 || exit 2
    REMOTE_PATH=public_html/sssd-review-coverage
    shift
done


# Rebuild the sources
autoreconf -if
./configure --enable-all-experimental-features --with-test-dir=/dev/shm

make CFLAGS="$CFLAGS --coverage -fprofile-arcs -ftest-coverage -Wl,-lgcov"
make check CFLAGS="$CFLAGS --coverage -fprofile-arcs -ftest-coverage -Wl,-lgcov"

# Remove old coverage data
rm -rf $COV_FILE_IN $COV_FILE $COV_HTML
mkdir -p $COV_HTML

# Generate new ones

lcov --capture \
     --directory $SSSD_SRC_DIR \
     --no-external \
     --output-file $COV_FILE_IN

lcov --remove $COV_FILE_IN tests\* \
     --output-file $COV_FILE

genhtml $COV_FILE --output-directory $COV_HTML

# Publish them
rsync -r $COV_HTML $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH

popd
