#!/bin/bash
#
# ./make-exercises
# ./make-exercises clean
# ./make-exercises clean all

# You may have to change these warning flags depending on your compiler.  

export CPPUTEST_PLATFORM_CXXFLAGS="-Wno-c++98-compat-pedantic -Wno-missing-prototypes -Wno-unused-parameter -Wno-old-style-cast"


initMakeCode()
{
    MAKEFILES=$(find * -name "makefile" | sort)
    echo "Makefiles to make ${CODE_DIRS}"
    echo "" >error.tmp.txt
}

makeCode()
{
    for file in $MAKEFILES
    do
        dir=$(dirname $file)
        echo ""
        echo "== Making -C ${dir} $1 =========================="
        make -C $dir $1
        if [ $? -ne 0 ]; then
            echo "Making -C ${dir} $1 failed" >>error.tmp.txt
        fi
    done
}

target=${1:-all}
initMakeCode
makeCode $target
shift
while (( "$#" )); do
    makeCode $1
    shift
done

cat error.tmp.txt

cd ..

echo "To make an individual directory:"
echo "    export CPPUTEST_HOME=path/to/cpputest   # ${CPPUTEST_HOME} for example"
echo "    cd the/exercise/directory"
echo "    make"
