#!/bin/tcsh -f

# ================ general constants ========================================
set PROGNAME="`basename $0`"

set backslash_quote
set lexicon = "\'unilex-rpx"
# ================== help functions =========================================
set USAGE = (utt_list) 
if("$1" == "-H") then 
    cat << EOF
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                           V1.0, V.Strom, 12/04/2005
    Usage:  $PROGNAME $USAGE 

    uttlist   file containing a list of utterance names
              (names like e.g. nina_01_055)

    For each given utterance:
        make the initial phone labs (using $lexicon).
        do the final alignement.

    Use this if there are minor inconsistencies between 
    the initial phones and the current lexicon, i.e.
    "align missmatch" in (build_utts "utts.data" lexicon).

    make_initial_phone_labs is called from within festival,
    you therefore need to set enviroment variables
        \$FESTIVAL
        \$MULTISYN_BUILD
    

    requires:
        ../utts.data  (the transcription)
        config
        hmm25/MMF
        dict2
        phone_list
        train.scp
        ../lab
        \$FESTIVAL
        \$MULTISYN_BUILD/scm/build_unitsel.scm
        \$MULTISYN_BUILD/bin/break_mlf

    creates:
        utts_repeated.data
        train_repeated.mlf
        train_repeated.scp
        aligned_repeated.3.mlf
        ../lab/\$utt.lab  for each \$utt
        
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EOF
    exit 1
else if($#argv != $#USAGE || "$1" == "-h") then 
    echo "Usage:  $PROGNAME $USAGE" 
    exit 1
endif
# ================== catching args ==========================================
set  utt_list = $argv[1]

# ================== enviroment variables ===================================
if(! $?FESTIVAL) then
    echo "enviroment variable  FESTIVAL no defined"
    exit 1
endif
if(! $?MULTISYN_BUILD) then
    echo "enviroment variable  MULTISYN_BUILD no defined"
    exit 1
endif

# =================== check if input files are there ========================
foreach f (../utts.data config hmm25/MMF dict2 phone_list train.scp \
           ../lab  $MULTISYN_BUILD/bin/break_mlf  $FESTIVAL \
           $MULTISYN_BUILD/scm/build_unitsel.scm  $utt_list )
    if(! -e $f) then
        echo "${PROGNAME}: need  $f"; exit 1
    endif
end   
# =================== from these make input file for HTK  ===================

# Rename_if_exists utts_repeated.data 
# Rename_if_exists utts_repeated.mlf
# Rename_if_exists train_repeated.scp
# Rename_if_exists aligned_repeated.3.mlf

if(-e utts_repeated.data) rm utts_repeated.data
if(-e utts_repeated.mlf) rm utts_repeated.mlf
if(-e train_repeated.scp) rm train_repeated.scp
if(-e aligned_repeated.3.mlf) rm aligned_repeated.3.mlf


foreach utt (`cat $utt_list`)
    egrep $utt train.scp   >> train_repeated.scp
    egrep $utt ../utts.data >> utts_repeated.data
end

# the following creates the .mlf from the transscription .data:
echo "making phone labs with festival..."
cat << EOF | $FESTIVAL $MULTISYN_BUILD/scm/build_unitsel.scm
(make_initial_phone_labs "utts_repeated.data" "utts_repeated.mlf" $lexicon)

EOF

$MULTISYN_BUILD/bin/make_mfcc_list ../mfcc utts_repeated.data train_repeated.scp

echo;echo; echo "now you have a chance to edit  utts_repeated.mlf"
echo;echo; echo "press RETURN to continue"

set line = $<

# =================== final alignement ======================================
echo "FINAL ALIGNMENT"
HVite -l \* -C config -a -m -i aligned_repeated.3.mlf -I utts_repeated.mlf -T 1  -H hmm25/MMF dict2 phone_list -S train_repeated.scp

if($status > 0) then
    echo "Final alignment failed, Aborted!"
    exit 1
endif
# ================= script coda =============================================
set wd = `pwd`
set echo
cd ..
../multisyn_build/bin/break_mlf alignment_vstrom/aligned_repeated.3.mlf lab
cd $wd
unset echo

exit 0;
# ==================== end ==================================================
