#!/usr/bin/env bash
# -*- tab-width : 4; indent-tabs-mode : nil -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

[ "$DEBUG" ] && set -xv

#
# Functions
#

die()
{
    [ "$DEBUG" ] && set -xv

    echo "Error:" "$@" >&2
    exit -1;
}

run_cppcheck()
{
    [ "$DEBUG" ] && set -xv

    pushd "${SRC_DIR?}" > /dev/null || die "Failed to change directory to ${SRC_DIR?}"

    echo "unusedFunction" > "$DATA_DIR"/cppcheck_supp.txt

    "$CPPCHECK_DIR"/cppcheck -i external/ -i workdir/ --xml --suppressions-list="$DATA_DIR"/cppcheck_supp.txt --enable=all --max-configs=25 ./ 2> "$DATA_DIR"/err.xml \
    || die "Failed to run cppcheck."

    "$CPPCHECK_DIR"/htmlreport/cppcheck-htmlreport --file="$DATA_DIR"/err.xml --title="LibreOffice ${COMMIT_DATE_LO?} ${COMMIT_TIME_LO?} ${COMMIT_SHA1_LO?}, CppCheck ${COMMIT_DATE_CPPCHECK?} ${COMMIT_TIME_CPPCHECK?} ${COMMIT_SHA1_CPPCHECK?}" --report-dir="$HTML_DIR" --source-dir=. \
    || die "Failed to run cppcheck-htmlreport."

    popd > /dev/null || die "Failed to change directory out of ${SRC_DIR?}"
}

get_cppcheck()
{
    [ "$DEBUG" ] && set -xv

    if [ ! -d "$CPPCHECK_DIR" ]; then
        git clone "$GIT_URL" "$CPPCHECK_DIR" || die "Failed to git clone $GIT_URL in $CPPCHECK_DIR"
    else
        if [ ! -d "$CPPCHECK_DIR"/.git ] ; then
            die "$CPPCHECK_DIR is not a git repository"
        else
            pushd "$CPPCHECK_DIR" || die "Failed to change directory to $CPPCHECK_DIR"
            git pull || die "Failed to update git repository $CPPCHECK_DIR"
            popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
        fi
    fi
}

build_cppcheck()
{
    [ "$DEBUG" ] && set -xv

    pushd "${CPPCHECK_DIR?}" > /dev/null || die "Failed to change directory to ${CPPCHECK_DIR?}"
    make all || die "Failed to build cppcheck."
    popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
}

get_commit_lo()
{
    [ "$DEBUG" ] && set -xv

    pushd "${SRC_DIR?}" > /dev/null || die "Failed to change directory to ${SRC_DIR?}"

    COMMIT_SHA1_LO=$(git log --date=iso | head -3 | awk '/^commit/ {print $2}')
    COMMIT_DATE_LO=$(git log --date=iso | head -3 | awk '/^Date/ {print $2}')
    COMMIT_TIME_LO=$(git log --date=iso | head -3 | awk '/^Date/ {print $3}')

    popd > /dev/null || die "Failed to change directory out of ${SRC_DIR?}"
}


get_commit_cppcheck()
{
    [ "$DEBUG" ] && set -xv

    pushd "${CPPCHECK_DIR?}" > /dev/null || die "Failed to change directory to ${CPPCHECK_DIR?}"

    COMMIT_SHA1_CPPCHECK=$(git log --date=iso | head -3 | awk '/^commit/ {print $2}')
    COMMIT_DATE_CPPCHECK=$(git log --date=iso | head -3 | awk '/^Date/ {print $2}')
    COMMIT_TIME_CPPCHECK=$(git log --date=iso | head -3 | awk '/^Date/ {print $3}')

    popd > /dev/null || die "Failed to change directory out of ${CPPCHECK_DIR?}"
}


#
# Main
#


usage()
{
    [ "$DEBUG" ] && set -xv

    echo >&2 "Usage: lcov-report.sh -s [DIRECTORY] -w [DIRECTORY]
        -s    source code directory
        -w    html (www) directory
        -c    ccpcheck (git) directory."
    exit 1
}

#
# Main
#

if [ "$#" = "0" ] ; then
    usage
fi

SRC_DIR=
HTML_DIR=
CPPCHECK_DIR=
DATA_DIR=/tmp
GIT_URL="git://github.com/danmar/cppcheck.git"


while getopts ":s:w:c:" opt ; do
    case "$opt" in
    s)
        SRC_DIR="${OPTARG?}"
        ;;
    w)
        HTML_DIR="${OPTARG?}"
        ;;
    c)
        CPPCHECK_DIR="${OPTARG?}"
        ;;
    *)
        usage
        ;;
    esac
done


get_cppcheck
get_commit_cppcheck
get_commit_lo
build_cppcheck
run_cppcheck

