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

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

OK=0
FAILED=0

echo -e "${YELLOW}-------------------------------------------------${NORMAL}"
echo -e "${YELLOW}running lingua 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 file in input/*.trig
do
    f="$(basename ${file})"
    echo -en "$(pad "${f}" -34)"
    start=$EPOCHREALTIME
    eye --quiet --skolem-genid 8b98b360-9a70-4845-b52c-c675af60ad01 --nope --rdf-trig-output "${file}" > output/${f} 2>&1
    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 output/${f} | wc -l) -eq 0 ]]; then
        echo -e "${GREEN}OK${NORMAL}"
        ((OK++))
    else
        echo -e "${RED}FAILED${NORMAL}"
        ((FAILED++))
    fi
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
