#!/bin/bash
#Run from problem directory, no input arguments are relevant for us, the instance is piped on stdin

THEORY=tmp.theory`date +%s`
GROUND=tmp.ground`date +%s`
MODEL=tmp.model`date +%s`

touch $THEORY
touch $GROUND
touch $MODEL

cat /dev/stdin >$THEORY

if [[ `grep -c "?" $THEORY` -ne 0 ]]
then
	echo -n "?" >$MODEL
	grep "?" $THEORY >> $MODEL
fi

CORRECT=0
if [[ $CORRECT -eq 0 ]]
then
	cat $THEORY | shifter -ss -nS -q | `dirname $0`/gringo --lparse --shift >$GROUND
	CORRECT=$?
fi

if [[ $CORRECT -eq 0 ]]
then
	`dirname $0`/minisatid --verbosity=1 --outputformat=asp --format=asp --defsem=stable $GROUND >>$MODEL
	RESULT=$?
	if [[ $RESULT -ne 10 && $RESULT -ne 20 ]]
	then
		CORRECT=1
	fi
fi

if [[ $CORRECT -eq 0 ]]
then
	`dirname $0`/aspcomp3 $3 <$MODEL
fi

if [[ $CORRECT -ne 0 ]]
then
	echo "UNKNOWN"
fi

rm $GROUND &>/dev/null
rm $MODEL &>/dev/null
rm $THEORY &>/dev/null
