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

ASPINSTANCE=tmp.aspinstance`date +%s`
INSTANCE=tmp.instance`date +%s`
SOLUTION=tmp.solution`date +%s`

touch $ASPINSTANCE
touch $INSTANCE
touch $SOLUTION

DIRNAME=`dirname $0`

CORRECT=1
if [[ $CORRECT -eq 1 ]]
then 
	cat /dev/stdin | sed "s/?//g" >$ASPINSTANCE
	"$DIRNAME/lp2idp" $ASPINSTANCE "$INSTANCE" `cat "$DIRNAME/trans"`
	RESULT=$?
	if [[ RESULT -ne 0 ]]
	then
		CORRECT=0
		echo "UNKNOWN"
	fi
fi

if [[ $CORRECT -eq 1 ]]
then 
	"$DIRNAME/gidl" "$DIRNAME/fastest.idp" "$INSTANCE" --maxapp=6 | "$DIRNAME/minisatid" --verbosity=1 --outputformat=asp >$SOLUTION
	RESULT=$?
	if [[ RESULT -ne 10 && RESULT -ne 20 ]]
	then
		CORRECT=0
	fi
fi

if [[ $CORRECT -eq 1 ]]
	OUTPUT=`cat $SOLUTION`
	if [[ $OUTPUT == *UNSAT* ]]
	then
	  echo "INCONSISTENT"
	else
		if [[ $OUTPUT == *connected* ]]
		then
		  echo "YES"
		else
			echo "NO"
		fi
	fi
fi

rm "$ASPINSTANCE" 2>/dev/null
rm "$INSTANCE" 2>/dev/null
rm "$SOLUTION" 2>/dev/null
