#!/bin/sh -f
# -*- Mode: sh -*-
# File:
# Info:
#  CVS: $Id: xiddl2plasma-so,v 1.14 2006/01/12 21:49:44 rijsman Exp $

os=`uname -s`

if [ -z $1 ]
then
  echo " "
  echo "usage:"
  echo " "
  echo "     xiddl2plasma-so <model-file> <init-state> <heuristics> <fast-code {fast,not-fast}> [debug]"
  echo "  or"
  echo "     xiddl2plasma-so <model-file>.xiddl  <init-state> <heuristics> <fast-code {fast,not-fast}> [debug]"
  echo " "
  echo " This script requires three symbolic links:"
  echo " "
  echo " -1- xml, link to the XIDDL support software directory, typically  IDEA-DEV/bin"
  echo "     after a 'make' (default install) of IDEA-DEV"
  echo " -2- convert2, link to the DDL-2-NDDL converter application, typically "
  echo "     NewPlan/ModelLoader/converter/convert2 after 'make' in NewPlan"
  echo "     and 'jam' in NewPlan/ModelLoader/converter"
  echo " -3- plasma, link to the plasma software directory, typically PLASMA"
  echo " "
  echo " Output of this script:"
  echo " - plasma_<model-file>, library of the model"
  echo " - plasma_<model-file>.xiddl, XIDDL model for a PLASMA-idea agent"
  echo " - plasma_<init-state>.nddl.xml, initial state for a PLASMA-idea agent"
  echo " - plasma_<heuristics>.xml , heuristics for a PLASMA-idea agent"
  echo " - plasma_<heuristics>.no_branch,  no-branch file for a PLASMA-idea agent"

  exit -1
fi

if [ -e xml ]; then
  xslt_dir=xml
else
  echo " "
  echo "I was expecting to see a (symbolic) link 'xml' to the XIDDL support software."
  echo "These tools live in IDEA-DEV/bin after a default install.  If you make a (symbolic)"
  echo "to this directory from this model directory, this script will have some"
  echo "some hope of working.  Veel geluk!"
  echo " "

  exit -1
fi

if [ $os != 'Linux' ]
then
  echo "This script only supports Linux.  Write your own for $os."

  exit -1
fi

if [ -e $1 ]
then
  xiddl_file=$1
  base_name=`echo $1 | sed -e 's/.xiddl//g'`
else
  xiddl_file=$1.xiddl
  base_name=$1
fi

if [ ! -e $xiddl_file ]
then
  echo "xiddl file \"$xiddl_file\" not found!"
  exit -1
fi

if [ ! -e $2 ] || [ -z $2 ]
then
  echo " "
  echo "No initial state file ($2) found!"
  echo " "
  exit -1
fi

if [ ! -e $3 ] || [ -z $3 ]
then
  echo " "
  echo "No heuristics file ($3) found!"
  echo " "
  exit -1
fi

fast=$4 

if [ $fast != "fast" ] && [ $fast != "not-fast" ] 
then
  echo " "
  echo "Specifiacation for using fast or not-fast code can only be 'fast' or 'not-fast' and is '$4' (parameter 4)!"
  echo " "
  exit -1
fi 

debug=$5

if [ ! -e convert2 ]
then
  echo " "
  echo "It appears that the (symbolic) link 'convert2' to the ddl to nddl converter,"
  echo "which is usually NewPlan/ModelLoader/converter/convert2, does not"
  echo "exist.  Try 'jam' in the ModelLoader directory and linking to it"
  echo "directly from this directory.  Udachi."
  echo " "
  exit -1
fi

if [ ! -e plasma ]
then
  echo " "
  echo "It appears that the (symbolic) link 'plasma' to the directory"
  echo "containing the nddl parser infrastrucure, which is usually"
  echo "PLASMA, does not exist."
  echo "Try 'jam' in PLASMA directory and linking to it directly from"
  echo "this directory. Buona fortuna."
  echo " "
  exit -1
fi

#Set the minimum Java version requirements for the nddl parser/compiler
JAVAVERSTR="Minimum Java version is 1.4.2"
MINJAVA_I=1
MINJAVA_J=4
MINJAVA_K=2
BADJAVA=0

#Check for minimum Java version
JAVAVER=`java -version 2>&1 | egrep 'java version .[0-9]\.[0-9]+\.[0-9]+' | cut -d"\"" -f2`
if [ -n "$JAVAVER" ]
then
  i=`echo $JAVAVER | cut -d. -f1`
  j=`echo $JAVAVER | cut -d. -f2`
  k=`echo $JAVAVER | cut -d. -f3`
# ignore version info after the "_" as in 1.4.2_05
  k=`echo $k | cut -d_ -f1`
  if [ $i -lt $MINJAVA_I ]
  then 
    BADJAVA=1 
  else
    if [ $i -le $MINJAVA_I -a $j -lt $MINJAVA_J ]
    then
      BADJAVA=1 
    else
      if [ $i -le $MINJAVA_I -a $j -le $MINJAVA_J -a $k -lt $MINJAVA_K ]
      then
        BADJAVA=1 
      fi
    fi
  fi
  if [ $BADJAVA -eq 1 ]
  then
    echo "  Java version found is too old for nddl-compiler/parser: version " $JAVAVER 
    echo " " $JAVAVERSTR
    echo "Gokouun o inorimasu."
    exit -1
  fi
else
  BADJAVA=1 
  echo "Did not find any version of java in search path"
  echo " " $JAVAVERSTR
  echo "Gokouun o inorimasu."
  exit -1
fi

if [ -n $JAVA ]; then
  if [ -n $JAVA_HOME ]; then
    JAVA=java;
  else
    JAVA=$JAVA_HOME/bin/java;
  fi;
fi

if [ ! -e xml/build-xiddl ]
then
  echo " "
  echo "It appears that the script need to assemble the final final xiddl model"
  echo "(by resolving xincludes and so on) is not in the IDEA-DEV/bin directory."
  echo " "
  exit -1
fi

xiddl_2_ddl_script=xml/run-xiddl2ddl

if [ ! -e $xiddl_2_ddl_script ]
then
  echo " "
  echo "It appears that the script which converts xiddl to ddl isn't in the"
  echo "IDEA-DEV/bin directory..."
  echo "Try 'make install' in IDEA-DEV/XIDDL directory. Gute Besserung!"
  echo " "
  exit -1
fi

# first, assemble the final xiddl "model" file

echo "[`date +%T`] Started building XIDDL file"

start=`date +%s`

./xml/build-xiddl $xiddl_file plasma_$xiddl_file $xiddl_file.errors $debug

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Building the XIDDL file failed."
  exit -1
fi

end=`date +%s`
xiddlduration=`expr $end - $start`

echo "[`date +%T`] Finished building XIDDL file in $xiddlduration second(s)."

# next, generate the ddl

echo "[`date +%T`] Started building DDL from XIDDL file"

start=`date +%s`

$xiddl_2_ddl_script plasma_$xiddl_file $debug

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Building DDL from XIDDL file failed."
  exit -1
fi

end=`date +%s`
ddlduration=`expr $end - $start`

echo "[`date +%T`] Finished building DDL from XIDDL file in $ddlduration second(s)."

errors=`egrep "ERROR" plasma_$base_name.xiddl-tests plasma_$base_name.europa-tests \
        | wc | awk '{ print $1 }'`

if [ $errors -ne 0 ];
then
  exit -1
fi

# next, generate the nddl crap

convert_cmd="./convert2 -m plasma_$base_name.ddl -n $base_name.nddl -i $2 -x $2.nddl -u $3 -l plasma_$3.xml -b plasma_$3.no_branch"

echo "[`date +%T`] Started converting DDL to NDDL"

start=`date +%s`

$convert_cmd

rvalue=$?

if [ $rvalue -ne 0 ]
then 
  echo " "
  if [ $rvalue -eq 1 ]
  then 
    echo "[`date +%T`] Converting DDL to NDDL failed reading the model or 'convert2' arguments are incorrect."
    exit -1
  fi
  if [ $rvalue -eq 2 ]
  then 
    echo "[`date +%T`] Converting DDL to NDDL failed reading the initial state."
    exit -1
  fi
  if [ $rvalue -eq 3 ]
  then 
    echo "[`date +%T`] Converting DDL to NDDL failed reading the heuristics file."
    exit -1
  fi

  echo "[`date +%T`] Converting DDL to NDDL failed"
  exit -1
fi

end=`date +%s`
convertduration=`expr $end - $start`

echo "[`date +%T`] Finished converting DDL to NDDL in $convertduration second(s)."

# pre-process to include the #include verbatim in the init file
# uses the gcc pre-processor to include the #include

echo "[`date +%T`] Started pre-processing NDDL (copy include files into NDDL)"

start=`date +%s`

gcc -I plasma/NDDL/core -E -x c -P $2.nddl > plasma_$2.nddl

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Pre-processing NDDL failed"
  exit -1
fi

end=`date +%s`
preprocessduration=`expr $end - $start`

echo "[`date +%T`] Finished pre-processing NDDL  $preprocessduration second(s)."

echo "[`date +%T`] Started converting NDDL into C++"

start=`date +%s`

# parse nddl-init file (includes the model-file so this is sufficient to parse)
$JAVA -cp plasma/NDDL/core/libraries/nanoxml.jar:plasma/NDDL/core/libraries/antlr.jar:plasma/NDDL/core/jars/nddl.jar nddl.Parse `pwd`/plasma_$2.nddl

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Converting NDDL to C++ failed, parsing nddl-init file failed."
  exit -1
fi

# compile nddl-init file (includes the model-file so this is sufficient to parse)
$JAVA -cp plasma/NDDL/core/libraries/nanoxml.jar:plasma/NDDL/core/libraries/antlr.jar:plasma/NDDL/core/jars/nddl.jar nddl.NddlCompiler `pwd`/plasma_$2.nddl.xml

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Converting NDDL to C++ failed."
  exit -1
fi

end=`date +%s`
cplusplusduration=`expr $end - $start`

echo "[`date +%T`] Finished converting NDDL into C++ in $cplusplusduration second(s)."

echo "[`date +%T`] Started compiling " plasma_$2.nddl.cc

start=`date +%s`

# compiling the generated c++ code

if [ $fast != "fast" ]
then
g++ -g -I. -I plasma/PlanDatabase -I plasma/PlanDatabase/core -I plasma/PlanDatabase/component  -I plasma/TemporalNetwork -I plasma/TemporalNetwork/core -I plasma/TemporalNetwork/component -I plasma/ConstraintEngine -I plasma/ConstraintEngine/core -I plasma/ConstraintEngine/component -I plasma/Utils -I plasma/Utils/core -I plasma/HSTS/core -I plasma/HSTS/component -I plasma/RulesEngine -I plasma/RulesEngine/core -I plasma/CBPlanner/ -I plasma/CBPlanner/core -I plasma/CBPlanner/component -I plasma/Resource/ -I plasma/Resource/core -I plasma/NDDL/ -I plasma/NDDL/core -I plasma/NDDL/component -I plasma/System/ -I plasma/System/core -fpic -c plasma_$2.nddl.cc -o plasma_$2.nddl.o_g
else
g++ -g -O3 -DEUROPA_FAST -I. -I plasma/PlanDatabase -I plasma/PlanDatabase/core -I plasma/PlanDatabase/component  -I plasma/TemporalNetwork -I plasma/TemporalNetwork/core -I plasma/TemporalNetwork/component -I plasma/ConstraintEngine -I plasma/ConstraintEngine/core -I plasma/ConstraintEngine/component -I plasma/Utils -I plasma/Utils/core -I plasma/HSTS/core -I plasma/HSTS/component -I plasma/RulesEngine -I plasma/RulesEngine/core -I plasma/CBPlanner/ -I plasma/CBPlanner/core -I plasma/CBPlanner/component -I plasma/Resource/ -I plasma/Resource/core -I plasma/NDDL/ -I plasma/NDDL/core -I plasma/NDDL/component -I plasma/System/ -I plasma/System/core -fpic -c plasma_$2.nddl.cc -o plasma_$2.nddl.o_o
fi;

if [ $? != 0 ]
then 
  echo " "
  echo "[`date +%T`] Compiling failed."
  exit -1
fi

end=`date +%s`
compileduration=`expr $end - $start`

echo "[`date +%T`] Finished compiling " plasma_$2.nddl.cc " in $compileduration second(s)."

# linking the code

start=`date +%s`

if [ $fast != "fast" ]
then

target='plasma_'$base_name'_g'

echo "[`date +%T`] Started linking " $target

start=`date +%s`

g++ -shared -o $target plasma_$2.nddl.o_g

else
target='plasma_'$base_name'_o'

echo "[`date +%T`] Started linking " $target

g++ -shared -o $target plasma_$2.nddl.o_o

fi;

end=`date +%s`
linkduration=`expr $end - $start`

echo "[`date +%T`] Finished linking " $target " in $linkduration second(s)."


if [ "$debug" != 'debug' ]
then
rm plasma_$base_name.ddl
rm -f plasma_$base_name.xiddl.temp
rm -f $base_name.xiddl.temp
rm -f *.xiddl.temp
rm plasma_$base_name.xiddl-tests 
rm plasma_$base_name.europa-tests 
rm -f $base_name.nddl 
rm -f $base_name.xiddl.errors 
rm -f $base_name.xiddl.errors.temp

rm -f $2.nddl
rm -f plasma_$2.nddl
rm -f plasma_$2.nddl.o_o
rm -f plasma_$2.nddl.o_g
rm -f plasma_$2.nddl.cc
rm -f plasma_$2.nddl.hh
fi

echo "[`date +%T`] Finished succesfully."

echo "--------------------------------------------------------------"
echo "         Step                |       Duration in seconds     "
echo "--------------------------------------------------------------"
echo "  Creating XIDDL file        |              $xiddlduration  "
echo "  Creating DDL file          |              $ddlduration "
echo "  Converting DDL into NDDL   |              $convertduration "
echo "  Pre-processing NDDL        |              $preprocessduration "
echo "  Creating C++ code          |              $cplusplusduration "
echo "  Compiling C++ code         |              $compileduration "
echo "  Linking compiled code      |              $linkduration "
echo "--------------------------------------------------------------"

exit 0
     
# EOF
