#!/bin/bash

BASEDIR=$PWD

isearch_file=isearch_new_all.tlp

if [[ $# -ne 2 ]] 
then
    echo "Usage: $0 domain_name strategy"
    echo "strategy is a number 1-44, which defines the heuristic to be used. Competition stategy was 18"
    echo "Example $0 TPP 18"
    echo "The results are put in a tlplan-all-result/ directory"
else 
    name=$1
    PROBLEMS="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20"
    
    echo "====================== DOMAIN:  $name  ====================="
    mkdir -p $BASEDIR/tlplan-result/$name/QualitativePreferences
    mkdir -p $BASEDIR/tlplan-output
    mkdir -p $BASEDIR/tlplan-all-result/
    cd $BASEDIR
    for p in $PROBLEMS; do
	pname=p${p}.pddl
	echo "---------------------- Problem:  $pname  ---------------------"
	echo "-- running translator"
	NEWNAME=${name}-${p}-$2
	OUTPUTFILE=output-$NEWNAME
	/usr/bin/time -f "scale=2;%S+%U" -o _elapsedtime pddl2tlplan -o $OUTPUTFILE ipc5/domains/$name/QualitativePreferences/domain.pddl ipc5/domains/$name/QualitativePreferences/$pname > /dev/null
	echo "-- creating script.tlp file"
	T=`cat _elapsedtime | bc`
	
	echo -e "(load-file \"$OUTPUTFILE\")
(set-plan-name \"$BASEDIR/tlplan-result/$name/QualitativePreferences/$NEWNAME\")
(load-file \"${isearch_file}\")
(def-defined-predicate do-it
    (local-vars ?extra-time ?parse-time ?results-file)
    (and 
       (:= ?results-file \"$BASEDIR/tlplan-all-result/${name}-$p-$2\") 
       (:= ?extra-time $T)
       (:= ?parse-time (get-cpu-time))
       (set-search-limit 50000)
       (isearch 1800 $2)))
(do-it)
(exit)
">  script-${p}-$2.tlp
	
	echo "-- running tlplan"
	
	./tlplan -l tlplan-output/$NEWNAME.log script-${p}-$2.tlp 2> tlplan-output/$NEWNAME.err
	rm script-${p}-$2.tlp
    done;
    
fi
