#!/bin/bash

prefix=$1

WORDSYMB_I="--isymbols=""$prefix""_dico_word.txt"
WORDSYMB_O="--osymbols=""$prefix""_dico_word.txt"

rm -f "$prefix".fst
nbsect=1
while [ "$nbsect" != 80 ]
do
 if [ -e "$prefix""_section""$nbsect"".txt" ]
 then
  rm -f "$prefix""_section""$nbsect".fst
  if [ `cat "$prefix""_section""$nbsect"".txt" | wc -l` != 0 ]
  then
   cat "$prefix""_section""$nbsect"".txt" | fstcompile $WORDSYMB_I > "$prefix""_section""$nbsect"".fst"
  fi
  if [ -e "$prefix""_section""$nbsect".fst ]
  then
   if [ -e "$prefix".fst ]
   then
    fstconcat "$prefix".fst "$prefix""_section""$nbsect".fst > "$prefix".fst2
    mv "$prefix".fst2 "$prefix".fst
   else
    cp "$prefix""_section""$nbsect".fst "$prefix".fst
   fi
  fi
  if [ -e "$prefix""_section""$nbsect""_text.txt" ] && [ -e "$prefix""_section""$nbsect".fst ]
  then
   fstcompile --acceptor $WORDSYMB_I $WORDSYMB_O "$prefix""_section""$nbsect""_text.txt" > "$prefix""_section""$nbsect""_text.fst"
 
   # check
   nbli=`fstcompose "$prefix""_section""$nbsect""_text.fst" "$prefix""_section""$nbsect".fst | fstprint | wc -l`
   echo "- Check section""$nbsect"": ""$nbli"
  fi
 fi
 nbsect=$(($nbsect+1))
done
# compile tail
fstcompile $WORDSYMB_I $WORDSYMB_O "$prefix""_clean_tail.txt" > "$prefix""_clean_tail.fst"
  
