#!/bin/bash

if [ $# != 2 ]
then
    echo "usage: $0 <input-xml> <output-dir>" >&2
    exit
fi

mkdir -p $2
xml=`readlink -f $1`
output=`readlink -f $2`

version=`basename $xml .xml`
prefix="$output/automate/"$version

set -e -u -o pipefail

cd `dirname $0`

dir=$PWD/../../
export LD_LIBRARY_PATH=$dir/libs:$dir/libs/fst:$dir/libs/atlas:$PWD/src
export PATH=$dir/bin:$PATH

rm -rf "$output/automate"
mkdir -p "$output/automate"

echo "compilation des modeles"
cat $xml | ./src/process_xml_for_slu -prefix $prefix
./src/compile_automate.sh $prefix

#echo "test sur l'ensemble du spectacle"
#nbsect=1
#while [ "$nbsect" != 10 ]
#do
# if [ -e "$prefix""_section""$nbsect"".fst" ]
# then
#  cat $version.asr/sect"$nbsect".ref | ./src/rocio_slu -word "$prefix"_dico_word.txt -action "$prefix"_dico_action.txt -fstmodel "$prefix"_section"$nbsect".fst -fstclean "$prefix"_clean_tail.fst
# fi
# nbsect=$(($nbsect+1))
#done

echo "production des chaines: action string"
for fst in "$prefix"_section*.fst 
do
    section=`echo $fst | grep -o "section[0-9]*" | cut -f2 -dn`
    echo "$fst => $section" >&2
    ./src/produce_action_string_fst -section $section -fst $fst -dico "$prefix"_dico_word.txt -action "$prefix"_dico_action.txt 
done > "$output/$version".action  
#echo > "$version".action

echo "finished training slu"
#cp -r automate/ $output/
#cp "$version".action $output/

