#!/bin/bash

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

OK=0
FAILED=0

version=$(eye --version 2>&1 | grep EYE | awk '{ print $2 }')

echo -e "${YELLOW}---------------------${NORMAL}"
echo -e "${YELLOW}Running eye reasoning${NORMAL}"
echo -e "${YELLOW}eye $version${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}" -24)"
    start=$(($(date +%s%N)/1000000))
    ./test > /dev/null 2>&1
    end=$(($(date +%s%N)/1000000))
    echo -en "${YELLOW}$(pad "`expr $end - $start` msec" 12)${NORMAL} "
    if [[ $(git diff . | wc -l) -eq 0 ]]; then
        echo -e "${GREEN}OK${NORMAL}"
        ((OK++))
    else
        echo -e "${RED}FAILED${NORMAL}"
        ((FAILED++))
    fi
    popd  > /dev/null
done
end=$(($(date +%s)))
echo ""

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