$\ = "\n";
$, = "\t";
use XML::LibXML;

binmode( STDOUT, ":utf8" );

open FILE, "/var/www/html/teitok/egpa/Scripts/moderniza.vrt";
binmode( FILE, ":utf8" );
while ( <FILE> ) {
    chop;
    if ( /^<.*id="(.*?)".*lang="(.*?)"/ ) { $id = $1; $lang= $2; next; }
    if ( /^<.*/ ) { next }
    ( $nform, $wid, $mform ) = split ( "\t" );
    if ( $nform ne '' && $mform ne "--" ) {
        $reg{$lang}{$nform}{$mform}++;
    }
}
close FILE;

$filename = shift;

if ( $filename eq '' ) {
    while ( ( $lang, $val ) = each ( %reg ) ) {
        while ( ( $nform, $val2 ) = each ( %{$val} ) ) {
            while ( ( $mform, $num ) = each ( %{$val2} ) ) {
                print $lang, $nform, $mform, $num;
            }
        }
    }
    exit;
}

$parser = XML::LibXML->new();
eval {
    $tmpdoc = $parser->load_xml(location => $filename, load_ext_dtd => 0);
};
if ( !$tmpdoc ) { print " - Unable to parse"; exit; }
$tmpdoc->setEncoding('UTF-8');

#$lang = "ES";

print "Treating $filename - language: $lang";

foreach $node ($tmpdoc->findnodes("//tok")) {
    $nform = $node->getAttribute('nform');
    $id = $node->getAttribute('id');
    if ( $nform eq '' ) { $nform = $node->getAttribute('nform') }
    if ( $nform eq '' ) { $nform = $node->textContent }

    $omform = $node->getAttribute('mform');
    if ( $omform eq '' ) { $omform = $node->getAttribute('nform') }
    if ( $omform eq '' ) { $omform = $node->getAttribute('nform') }
    if ( $omform eq '' ) { $omform = $node->textContent }

    $allmform = join ( "|", keys %{$reg{$lang}{$nform}} );
    $m = 0; $mform = "";
    while ( ( $a, $b ) = each ( %{$reg{$lang}{$nform}} ) ) {
        if ( $b > $m ) { $mform = $a; $m = $b }
    }

    if ( $nform eq '' ) { $yn = ""; $ignore++ }
    elsif ( $omform eq $mform ) { $yn = '+'; $yes++; if ( $mform eq $nform ) { $gmod++ } }
    elsif ( $mform eq '' ) { $yn = '?'; $uk++ }
    else { $yn = '-'; $no++ }

    if ( $mform eq $nform ) { $nomod++ } else {
        $mod++;
        if ( $node->getAttribute('mform') eq '' && $node->getAttribute('nform') ne $mform ) {
            # Modify the 'mform' value before setting it in the XML
            $node->setAttribute('mform', $mform);
        }
    }

    # Remove '&amp;#13;' and '&#13;' from the 'mform' attribute
    my $mform_value = $node->getAttribute('mform');
    $mform_value =~ s/(&amp;#13;|&#13;)//g;
    $node->setAttribute('mform', $mform_value);

    print $id, $nform, $yn, $mform, $m, $omform, $allmform;
}

print "$yes good, $no bad, $uk new, $ignore empty";
print "$mod modernized, $nomod not, $gmod correctly modernized";

$text = $tmpdoc->toString;

# Remove '&amp;#13;' and '&#13;' from the entire XML content
$text =~ s/(&amp;#13;|&#13;)//g;

open FILE, ">$filename";
print FILE $text;
close FILE;
