#!/bin/bash

cppcode=${1}
cppoutfile=`mktemp`
exoutfile=`mktemp`
sha256saved=`cat ./sha256value`

sha256file=`sha256sum ${cppcode} | xargs | cut -f 1 -d ' '`

if [ "${sha256saved}" == "${sha256file}" ] ; then
	echo '---- compile skipped ----'
else
	echo '---- compiling... ----'
	set -e
	g++ -std=gnu++1y -O2 -o ./a.out ./${cppcode}
	set +e
        echo ${sha256file} > ./sha256value
fi

echo '---- executing... ----'
cat in.txt | ./a.out | grep ^ > ${cppoutfile}

echo '---- output ----'
cat ${cppoutfile}

echo '---- diff ----'
cat out.txt | grep ^ > ${exoutfile}
diff -u ${exoutfile} ${cppoutfile} ; ret=$?

echo '---- result ----'
if [ ${ret} -eq 0 ] ; then
	echo 'OK'
else
	echo 'NG'
fi

rm ${cppoutfile} ${exoutfile}
