#!/bin/bash
# check the command line
if [ $# -lt 1 ] ; then
  echo "Usage: $(basename $0) arcus"
  exit 1
fi

RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
NORMAL="\e[0;39m"

OK=0
DIFF=0

echo -e "${YELLOW}-------------------------------------------------${NORMAL}"
echo -e "${YELLOW}running arcus examples and test cases${NORMAL}"
echo -e "${YELLOW}using eye v$(eye --version 2>&1 | grep EYE | awk '{ print substr($2,2) }') and swipl v$(swipl --version 2>&1 | awk '{ print $3 }')${NORMAL}"
echo -e "${YELLOW}-------------------------------------------------${NORMAL}"
echo ""

pad () {
    [ "$#" -gt 1 ]
    [ -n "$2" ]
    printf "%$2.${2#-}s" "$1"
}

begin=$(($(date +%s)))
for dir in */
do
    pushd "${dir}" > /dev/null
    echo -en "$(pad "${file}${dir}" -34)"
    start=$EPOCHREALTIME
    ./test > /dev/null
    end=$EPOCHREALTIME
    echo -en "${YELLOW}$(pad "$(awk -v s="$start" -v e="$end" 'BEGIN{printf "%.0f", (e-s)*1000}') ms" 10)${NORMAL} "
    if [[ $(git diff . | wc -l) -eq 0 ]]; then
        echo -e "${GREEN}OK${NORMAL}"
        ((OK++))
    else
        echo -e "${RED}DIFF${NORMAL}"
        ((DIFF++))
    fi
    popd  > /dev/null
done
end=$(($(date +%s)))
echo ""

echo -e "${YELLOW}`expr $end - $begin` sec${NORMAL} ${GREEN}${OK} OK${NORMAL} ${RED}${DIFF} DIFF${NORMAL}"
if [[ ${DIFF} -eq 0 ]]; then
    exit 0
else
    exit 2
fi
