#! /bin/sh

BASEDIR="$(dirname "$0")"

# Paths to planner components
TRANSLATE="$BASEDIR/translate/translate.py"
PREPROCESS="$BASEDIR/preprocess/preprocess"
SEARCH="$BASEDIR/search/downward"

run_planner() {
    echo "1. Running translator"
    command time --output=elapsed.time --format='%S\n%U\n' \
        "$TRANSLATE" "$1" "$2" || exit 1
    echo "2. Running preprocessor"
    command time --output=elapsed.time --append --format='%S\n%U\n' \
        "$PREPROCESS" < output.sas || exit 1
    echo "3. Running search"
    "$SEARCH" ipc "$PLANNER" --plan-file "$3" < output
}

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 [ $# -ne 4 ]; then
    echo "Usage: \"plan-ipc <planner> <domain_file> <problem_file> <result_file>\""
    exit 1
fi

PLANNER="$1"
shift

check_input_files "$1" "$2"

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

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