#! /usr/bin/perl
#
if(grep(/^(\-h|\-\-help)$/, @ARGV))
{
print << "ENDOFHELPTEXT";
Combine the CGN text-grids to a "master" textgrid
and add IDcodes for DBMS. 

Use:
./CombineCGNLabelFiles.pl ... [--root=<dir>] 2> error.txt

Options:

--root=<dir>
  CGN annotation text root directory. 
  Default is "../data/annot/text"

-l
--license
  License text
  
-h
--help
  This help text

Processing:
Files are read from "\$IFADVrootDir/<type>".
The output TextGrids are placed in "\$IFADVrootDir/dbl/"
the filename will be extended with a list
of the types included in that .dbl file

<CHANGED>
E.g., [ort, awd, plk] -> *.ort.awd.plk.dbl

Eg
./CombineCGNLabelFiles.pl ort awd 2> error.txt

Possible types:
ort fon awd plk
Individual tiers get the type of origin attached, if needed.
</CHANGED>

DBMS item ID codes:
Every item in the text-grids is given a unique ID code.
The codes are hierarchically organized. For example, a certain
phoneme, the last consonant, 'r', from 'voor',
has ID code: DVB3F29F1GG4SC1

This is build up like:
<DV><A/B><recording no><speaker><repeat><chunk><word><syllable><part><phoneme>

In the example: DV-B-3-F29F-1-GG-4-S-C-1
DVB:      Dialog Video Camera/Subject B
3:        Recording 3
F29F:     Female, 29yo, ID code F 
1:        First participation of this subject
GG:       Chunk YH,starting with 'A',..,'Z','AA',...
4:        Word number 4
S:        First syllable (counting: S, T, U, ....)
C:        Coda position, can be Onset, Kernel, or Coda
1:        Position of the phoneme in the syllable part
          Ie, the first phoneme of the coda
Note that there are no sub-part separators. The ID codes are build
from <alpha><digit> sequences. Insertions are entered with the
old item sub-code, a hyphen '-', and a new counter code (eg, 'AO-A',
a single chunk added after chunk 'AO', or '6-1', a new word entered
after word '6'). Note that a silent pauze counts as an item. Ranges
are indicated by combining the first and last item with a '+'. Eg,
words 2 to 4 are indicated with:  'DVB3F29F1GG2+4'.

The corresponding codes for the syllable, word and chunk are obtained
by removing the rightmost parts. So this phoneme is found in syllable
DVB3F29F1GG4S of word DVB3F29F1GG4 from chunk DVB3F29F1GG.

These ID codes are unique for each recorded segment. So, the ID codes
of the corresponding items in awd, POS, and lem are ALL the same. 
This allows all the DBMS tables to be easily linked.

ENDOFHELPTEXT
exit;
};
if(grep(/\-?\-l(icense)?/, @ARGV))
{
print << "ENDOFLICENSETEXT";
###############################################################################
#
# Copyright R.J.J.H. van Son © 2007
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: R.J.J.H.vanSon\@uva.nl
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Spuistraat 210-212
#        NL-1012VT Amsterdam
#        The Netherlands
#        tel +31 205252196
#        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.
#
#
#######################################################
ENDOFLICENSETEXT
exit;
};
#
require "SentenceLabel.pl";
require "CGN_phonemes.pl";

my $CGNrootDir = "../";
if(grep(/\-\-root\=/, @ARGV))
{
    my ($Option) = grep(/\-\-root\=/i, @ARGV);
    $Option =~ s/\-\-root\=//isg;
    $CGNrootDir = $Option;
    @ARGV = grep(!/\-\-root\=/i, @ARGV);
};

$CGNrootDir .= "/" unless $CGNrootDir =~ m!/$!;

foreach my $Recording (@ARGV)
{
    # Get ORT and AWD files
    my @GazeFiles = glob("${CGNrootDir}EAF/$Recording*.gaze.TierName");
    my $OutFile = "${CGNrootDir}dbl/$Recording.gaze.dbl";

next unless @GazeFiles;
next if -s $OutFile;

    my $GazeLabel = new SentenceLabel;
    # This gives off a warning if the file is corrupt!
    $GazeLabel->ReadLabelFile($GazeFiles[-1]);
    # Gaze label
    my @NewGazeTierList = ();
    my @GazeTierList = @{$GazeLabel->{TIERORDER}};
    foreach my $Tier (@GazeTierList)
    {
        next unless $Tier =~ /gaze$/;
        push(@NewGazeTierList, $Tier);
        
        # Clean up items: Merge empty items
        $GazeLabel->presetItem ($Tier);
        # Add ID codes
        my $FixedID = $Tier;
        $FixedID =~ s/[a-z]+\s*$//g;
        my $counter = A;
        $GazeLabel->presetItem ($Tier);
        while($GazeLabel->nextItem ($Tier) > -1)
        {
            $GazeLabel->currentIDcode($Tier, "$FixedID$counter");
            if($GazeLabel->currentValue ($Tier) =~ /^\s*(sil|sp|\s*)$/i)
            {
                $GazeLabel->currentValue ($Tier, "NULL");
            };

            # Next chunk
            ++$counter;
        };
    };    
        
    # Write resulting file
    $GazeLabel->FoldedIDcodes(@NewGazeTierList);
    $GazeLabel->print("$OutFile", reverse(@NewGazeTierList));

};

