#!/bin/sh
#
# Continuous Testing 
#
# A small script to compile, run and debug unit tests at your finger tips.
#

clear

CXXFLAGS="-O0 -ggdb -Wall -Woverloaded-virtual"

runtests()
{
    echo "Compiling and running unit tests...."
    echo
    make CXXFLAGS="$CXXFLAGS" -s runtests
}

compileapplication()
{
    echo "Compiling application...."
    echo
    make CXXFLAGS="$CXXFLAGS" -s LIBDIR=. VDRDIR=/usr/include/vdr all
}

compiletestrunner()
{
    echo "Compiling tests...."
    echo
    make CXXFLAGS="$CXXFLAGS" -s LIBDIR=. VDRDIR=/usr/include/vdr testrunner
}

while true ; do
    echo
    echo "Possible actions:"
    echo "    [ENTER]      - Compile changed code and run tests"
    echo "    [d]+[ENTER]  - Compile changed code and run tests in debugger"
    echo "    [c]+[ENTER]  - Recompile and run all tests"
    echo "    [p]+[ENTER]  - Compile production code"
    echo "    [cp]+[ENTER] - Recompile production code"
    echo "    [v]+[ENTER]  - Verify test code with valgrind"
    echo

    read key

    clear

    runtests="false"    
    
    case "$key" in
        "c") 
            make clean
            runtests
            ;;
        "p")
            compileapplication
            ;;
        "cp")
            make clean
            compileapplication
            ;;
        "d")
            compiletestrunner
            gdb testrunner
            ;;
        "v")
            compiletestrunner
            valgrind --tool=memcheck --leak-check=yes ./testrunner
            ;;
        *)
            runtests
            ;;
    esac
done
