#! /usr/bin/perl
#
# Read a Praat label phoneme file and write the xml files requirted by corex
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: Rob.van.Son@hum.uva.nl
#        rob.van.son@workmail.com
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Herengracht 338
#        NL-1016CG Amsterdam
#        The Netherlands
#        tel +31 205252183
#        fax +31 205252197
#
# License for use and disclaimers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#######################################################
#
$HomeDir = '..';
require "$HomeDir/Links.pl";
require "$Scripts/SentenceLabel.pl";

# Make sure the target directory exists
sub create_Labeldirectory  # ($ID, $LabelType) -> 1/0 create directory if necessary
{
    my $ID = shift || return 0;
    my $LabelType = shift || return 0;
    #
    my $DirExists = -d "$SentenceLabels/$ID/$LabelType";
    (return mkdir("$SentenceLabels/$ID/$LabelType", 0744) || die "$SentenceLabels/$ID/$LabelType: $!\n") unless $DirExists;
    return 0;   # Nothing done
}

# Read label files
sub convert_phoneme_file      # ($ID, $FileID)
{
    my $ID = shift || die "No speaker ID\n";
    my $FileID = shift || die "No file ID\n";
    
    # Create target directory if necessary
    my $LabelType = "crx";
    create_Labeldirectory($ID, $LabelType);
    
    my $SourceFile = "$SentenceLabels/$ID/phoneme/$FileID.phoneme";
    my $TargetFile = "$SentenceLabels/$ID/$LabelType/$FileID.$LabelType";

    # Don't change existing files that are newer than the source
    return 0 if -e "$TargetFile" && (-M $TargetFile > -M $SourceFile);
    
    # read source label file
    my $Label = new SentenceLabel;
    $Label->ReadLabelFile($SourceFile);
    
    # Construct the COREX tiers
    $Label->copyTier('s', 'TRANSLIT');
    $Label->copyTier('w', 'WORDS');
    
    # Cleanout sentence values
    $Label->presetItem('s');
    while($Label->nextItem('s') > -1){ $Label->currentValue('s', '') if $Label->currentValue('s') =~ /[^\s\#\*]/;};

    # Write Target File
    return $Label->printxml($TargetFile, 's', 'w');
};

sub Convert_All_Sentences # ($ID) -> 0/1;
{
    my $ID = shift || return ();
    
    my @FileList = glob("$SentenceLabels/$ID/phoneme/*.phoneme");
    foreach $FilePath (@FileList)
    {
        $FilePath =~ m@/([^/]+)/phoneme/([^/]+)$@;
        my $Speaker = $1;
        my $FileName = $2;
        $FileName =~ s/\.[\w]+$//ig;
        die "$FilePath does not contain a filename\n" unless $FileName =~ /\w/;
	# Stop if the target exists and is newer than the source
        next if -e "$SentenceLabels/$ID/$LabelType/$FileName.$LabelType" && 
	        -M "$SentenceLabels/$ID/$LabelType/$FileName.$LabelType" > -M "$SentenceLabels/$ID/phoneme/$FileName.phoneme";
        
	convert_phoneme_file($Speaker, $FileName);
    };
    return 1;
};

#
my $ID;
while($ID = shift(@ARGV))
{
    Convert_All_Sentences($ID);
};

