#!/bin/bash
# Author M. Vallati
# the script that can launch all the learning system

#COMMAND LINE control
if [ $# -lt 6 ]; then
    echo "Usage: \"learner -o <domain file> -t <training folder> -k <DCK folder> [ -s time (in seconds, e.g. 900.00) ]\""
    exit 1
fi

    if [ ! -e "$2" ]; then
	echo "Domain file \"$2\" does not exist."
	exit 1
    fi
    if [ ! -d "$4" ]; then
	echo "Problem file \"$4\" does not exist."
	exit 1
    fi


TIME=900.00

if [ [ "$7" == "-s" ] && [ "$8" != "" ] ]; then
	TIME=$8
fi

DIR=data/domains
rm -rf $DIR/*
Domain=$2
Name_D=`basename $Domain | sed 's/\.pddl//g'`
mkdir $DIR/$Name_D
mkdir $DIR/$Name_D/bootstrap
cp $Domain $DIR/$Name_D/domain.pddl

Folder=$4
cp $Folder/* $DIR/$Name_D/bootstrap

DCK_folder=$6

if [ ! -d "$DCK_folder" ]
then
	mkdir $DCK_folder
fi


echo "First of All, clean previous results from the ./results directory"
rm -rf results/*
rm -rf DETAILS_RUN


echo "Step 1. Setup directories"
File_D=`basename $Domain`
./scripts/setup.sh $DIR 

echo "Take a coffee, the rest of the learning process will - likely - take several days."

echo "Step 2. Automatic Configuration of ParLPG, it will take about 20 days" 
echo "Step 2. Automatic Configuration of ParLPG in progess"
echo "Step 2. Automatic Configuration of ParLPG in progess" >> DETAILS_RUN
./learning_paramils/params.sh $DIR/$Name_D >> DETAILS_RUN
echo "Step 2. Automatic Configuration of ParLPG COMPLETED"

echo "Step 3. Learning Macros for Macroff" 
echo "Step 3. Learning Macros for Macroff in progess"
echo "Step 3. Learning Macros for Macroff in progess" >> DETAILS_RUN
./learning_macroff/macro.sh $DIR >> DETAILS_RUN
echo "Step 3. Learning Macros for Macroff COMPLETED"

echo "Step 4. Learning Macros for other Planners, by wizard!" 
echo "Step 4. Learning Macros for other Planners, by wizard! in progress" 
echo "Step 4. Learning Macros for other Planners, by wizard! in progress" >> DETAILS_RUN
./learning_wizard/wizard.sh $DIR pddl macro >> DETAILS_RUN
echo "Step 4. Learning Macros for other Planners, by wizard! COMPLETED" 

echo "Step 5. Moving macros in the right places"

cp learning_wizard/ff/pddl/* data/learning/planner_ff.sh
cp learning_wizard/yahsp/pddl/* data/learning/planner_YAHSP.sh
cp learning_wizard/marvin/pddl/* data/learning/planner_Marvin.sh
cp learning_wizard/downward/pddl/* data/learning/planner_downward.sh
cp learning_wizard/sgplan/pddl/* data/learning/planner_sgplan.sh
cp learning_wizard/parlpg/pddl/* data/learning/planner_paramils.sh
cp learning_wizard/lpg/pddl/* data/learning/planner_lpg_t.sh
cp learning_wizard/lama/pddl/* data/learning/planner_LAMA.sh

echo "Step 5. Moving macros in the right places COMPLETED" 

#passo 4, creo l'elenco dei compiti da fare
echo "Step 6. Building the list of planners to run"
./scripts/build_list.sh data/planners/ $DIR ./PbP_executable/learn_single to_do_list results
number=`cat elenco | wc -l`

echo "Step 6. PbP has to run $number planners on learning problems"

echo "Step 7. Running all the planners (with and without macros) on learning problems. Take another coffee, this will take a long time."
echo "Step 7. Running all the planners (with and without macros) on learning problems in progress " 
echo "Step 7. Running all the planners (with and without macros) on learning problems in progress " >> DETAILS_RUN
./scripts/exec_file.sh to_do_list >> DETAILS_RUN
echo "Step 7. Running all the planners (with and without macros) on learning problems COMPLETED, we are almost done."


echo "Step 8. Putting things together for the Wilcoxon. Give me a few minutes." 

./scripts/unisci_learn.sh $DIR results $TIME

echo "Step 9. Wilcoxon and analysis" 
echo "Step 9. Wilcoxon and analysis" >> DETAILS_RUN
./scripts/analyze.sh $DIR results $TIME
echo "Step 9. Wilcoxon and analysis COMPLETED" 

echo "Step 9. Writing the knowledge in $DCK_folder " 
./scripts/write_knowledge.sh $DIR $DCK_folder

echo "Done! :)"

rm output output.sas sas_plan test.groups log.txt rank.txt ffua.out *.soln > /dev/null 2>&1
