#!/bin/sh
#####################################################-*-mode:shell-script-*-
##                                                                       ##
##                                                                       ##
##                  Language Technologies Institute                      ##
##                     Carnegie Mellon University                        ##
##                      Copyright (c) 1998-2004                          ##
##                        All Rights Reserved.                           ##
##                                                                       ##
##  Permission is hereby granted, free of charge, to use and distribute  ##
##  this software and its documentation without restriction, including   ##
##  without limitation the rights to use, copy, modify, merge, publish,  ##
##  distribute, sublicense, and/or sell copies of this work, and to      ##
##  permit persons to whom this work is furnished to do so, subject to   ##
##  the following conditions:                                            ##
##   1. The code must retain the above copyright notice, this list of    ##
##      conditions and the following disclaimer.                         ##
##   2. Any modifications must be clearly marked as such.                ##
##   3. Original authors' names are not deleted.                         ##
##   4. The authors' names are not used to endorse or promote products   ##
##      derived from this software without specific prior written        ##
##      permission.                                                      ##
##                                                                       ##
##  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         ##
##  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ##
##  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ##
##  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      ##
##  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ##
##  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ##
##  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ##
##  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ##
##  THIS SOFTWARE.                                                       ##
##                                                                       ##
###########################################################################

LANG=C; export LANG

FESTIVAL="@FESTIVALBIN@"; export FESTIVAL
WAGON="@WAGON@"; export WAGON

if [ $# = 0 ]
then
   $0 cummulate "allowables.scm" "lts_scratch/central"
   $0 align "allowables.scm" "lts_scratch/central"
   $0 traintest "allowables.scm" "lts_scratch/central"
   $0 build "allowables.scm" "lts_scratch/central"
   $0 merge "allowables.scm" "lts_scratch/central"
   $0 test "allowables.scm" "lts_scratch/central"
   exit 0
fi

action="$1"
allowables="$2"
outdir="$3"

if [ "${action}" = "cummulate" ]
then

   if [ ! -f "${outdir}/lex_entries.out" ]
   then
        echo "can't find ${outdir}/lex_entries.out"
        exit
   fi

   echo Find probabilities of letter-phone pairs
   $FESTIVAL -b --heap 10000000 "${allowables}" '(begin
       (cummulate-pairs "'"${outdir}"'/lex_entries.out")
       (save-table "'"${outdir}"'/lex-") )' || exit 1
   exit 0
fi


if [ "${action}" = "align" ]
then

   echo "Align letter-phone pairs with best path"
   $FESTIVAL -b --heap 10000000 "${allowables}" "${outdir}/lex-pl-tablesp.scm" '(begin
      (aligndata "'"${outdir}"'/lex_entries.out" "'"${outdir}"'/lex.align")
      (build-feat-file "'"${outdir}"'/lex.align" "'"${outdir}"'/lex.feats"))' |
      tee "${outdir}/align.log" || exit 1
   exit 0
fi

if [ "${action}" = "traintest" ]
then
   echo "Split into training and test sets"
   #$FESTVOXDIR/src/general/traintest lts_scratch/lex.align
   ./traintest "${outdir}/lex.align"
   $FESTIVAL -b --heap 10000000 "${allowables}" "${outdir}/lex-pl-tablesp.scm" '(begin
      (build-feat-file "'"${outdir}"'/lex.align.train" "'"${outdir}"'/lex.feats.train"))'
   $FESTIVAL -b --heap 10000000 "${allowables}" "${outdir}/lex-pl-tablesp.scm" '(begin
      (build-feat-file "'"${outdir}"'/lex.align.test" "'"${outdir}"'/lex.feats.test"))'

   exit 0
fi


if [ "${action}" = "build" ]
then

   echo "Build letter to phone CART trees"
   if [ -f "${outdir}/lex.align.train" ]
   then
        echo "With traintest split"
   fi
   (COSA=${PWD}; cd "${outdir}"; ${COSA}/build_lts_rules ) > "${outdir}/lts.build.out"

   exit 0
fi

if [ "${action}" = "merge" ]
then
   echo Build complete model
   (COSA=${PWD}; cd "${outdir}";
       $FESTIVAL -b --heap 10000000 "${COSA}/${allowables}" '(begin
         (merge_models "lex_lts_rules" "lex_lts_rules.scm" allowables))' || exit 1)
   exit 0
fi

if [ "${action}" = "test" ]
then
  echo Test model
  if [ -f "${outdir}/lex.align.test" ]
  then
     $FESTIVAL -b --heap 10000000 "${allowables}" "${outdir}/lex_lts_rules.scm" '(lts_testset "'"${outdir}"'/lex.align.test" lex_lts_rules)' | tee "${outdir}/lts_test.log" || exit 1
     echo "with TEST set"
  else
     $FESTIVAL -b --heap 10000000 "${allowables}" "${outdir}/lex_lts_rules.scm" '(lts_testset "'"${outdir}"'/lex.align" lex_lts_rules)' | tee "${outdir}/lts_test.log" || exit 1
     echo "with ALL data -- no held out test set"
  fi
  exit 0
fi

if [ "${action}" = "tidyup" ]
then
   rm -rf "${outdir}"
   exit 0
fi

echo $0 unknown option $*
exit 1

