#! /bin/sh

#ulimit -v 3000000
#ulimit -d 3000000
#ulimit -c 0

TIMETOT=900

# Paths to planner components
TRANSLATE="./lama/translate/translate.py"
PREPROCESS="./lama/preprocess"
SEARCH="./lama/release-search"

run_planner() {

    echo "1. Running translator"
{ time -p "$TRANSLATE" "$1" "$2" ;} 2> time.log
    echo "2. Running preprocessor"
{ time -p "$PREPROCESS" < output_l.sas ;} 2> time2.log

TIME1=$(grep real time.log | sed 's/.*l//g' | cut -d' ' -f2| cut -d. -f1)
TIME2=$(grep real time.log | sed 's/.*l//g' | cut -d' ' -f2 | cut -d. -f1)
TIME3=$(echo "$TIMETOT-$TIME1-$TIME2"|bc)

#echo T1: $TIME1 T2: $TIME2 T3: $TIME3

ulimit -t $TIME3

    echo "3. Running search $4"
    "$SEARCH" "fFlL$4" "$3" < output_l
}

check_input_files() {
    if [ ! -e "$1" ]; then
	echo "Domain file \"$1\" does not exist."
	exit 1
    fi
    if [ ! -e "$2" ]; then
	echo "Problem file \"$2\" does not exist."
	exit 1
    fi
}

# Make sure we have exactly 3 command line arguments
if [ $# -lt 3 ]; then
    echo "Usage: \"plan <domain_file> <problem_file> <result_file>\""
    exit 1
fi

check_input_files "$1" "$2"

# Command line arguments seem to be fine, run planner
run_planner "$1" "$2" "$3" "$4"

# We do not clean up temporary files here (not necessary for IPC-2008)
