#!/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`
SMALLINSTANCE=tmp.smallinstance`date +%s`
SOLUTION=tmp.solution`date +%s`
TIME2=tmp.time2`date +%s`

touch $ASPINSTANCE
touch $INSTANCE
touch $SOLUTION

DIRNAME=`dirname $0`

CORRECT=1
if [[ $CORRECT -eq 1 ]]
then 
	cat /dev/stdin | shifter -ss -nS -q >$ASPINSTANCE
	"$DIRNAME/lp2idp" $ASPINSTANCE "$INSTANCE" `cat "$DIRNAME/trans"`
	RESULT=$?
	if [[ RESULT -ne 0 ]]
	then
		CORRECT=0
	fi
fi

if [[ $CORRECT -eq 1 ]]
then 
	"$DIRNAME/gidl" "$DIRNAME/prepare.idp" "$INSTANCE" -W --maxapp=6 --verbose | "$DIRNAME/minisatid" --verbosity=1 --outputformat=fodot | grep "Time2" >"$TIME2"
	RESULT=$?
	if [[ RESULT -ne 0 ]]
	then
		CORRECT=0
	fi
	cat $INSTANCE | sed "s/Time.*/`cat $TIME2`/g" | sed "s/Time2/Time/g" >$SMALLINSTANCE
fi

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

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


