#!/usr/bin/perl -w

# This script is used during build of English documents to check if
# translations are up-to date.

# This script takes full path to a original.wml file.
# For every language defined in @langs, the script:
#	- checks if a translated file exists for such language
#	- checks if the translated file is at least N revisions old
#	  (N is any number defined in @stages)
#	- if it is, and it hasn't been touched because of this particular
#	  "N", it is touched and a marker file is created
# This allows the file to be rebuilt _exactly_ the number of times it should
# (i.e. $#stages times)

# (C) 2000 by Marcin Owsiany <porridge@pandora.info.bielsko.pl>

# TODOs:
#	- compare both major and minor revision number
#	- think of a better way to check when the file has been rebuilt last


# This should contain all languages (except Enlish :-)
@langs = ("arabic", "chinese", "croatian", "danish", "dutch",
	 "esperanto", "finnish", "french", "german", "hungarian",
	 "italian", "japanese", "korean", "norwegian", "polish",
	 "portuguese", "romanian", "russian", "spanish", "swedish",
	 "turkish");

# This one element until "severities" are implemented in
# "translation-check.wml"
@stages = (5, 10, 15);

# Set this to 1 for debugging
$debug = 0;

sub rebuild {
    my $file = shift;
    $now = time;
    print "touching $file\n";
    utime $now, $now, $file or die "$file: $!";
}

sub mark_forced {
    my $file = shift;
    my $val = shift;
    my $foo = "$file".".forced";
    open LOG, ">$foo" or die "$foo: $!";
    print LOG "$val";
    close LOG;
    print "Created $file.forced with $val inside\n" if $debug;
}

sub was_forced {
    my $file = shift;
    if (open LOG, "<$file.forced") {
        close LOG;
        print "$file.forced exists\n" if $debug;
        return 1;
    } else {
        print "$file.forced does not exists\n" if $debug;
        return 0;
    }
}

sub when_forced {
    my $file = shift;
    if (open LOG, "<$file.forced") {
        $_ = <LOG>;
        chomp($_);
        print "$file.forced contains $_"."\n" if $debug;
        close LOG;
        return $_;
    } else {
        print "$file.forced : $!\n" if $debug;
        return 0;
    }
}

$argfile = $ARGV[0];
$argfile =~ m+(.*)/(.*)\.wml+ or die "pattern does not match";
($path, $file) = ($1, $2);
open FILE, "${path}/CVS/Entries" or die "${path}/CVS/Entries: $!";
while (<FILE>) {
    if (m,^/$file.wml/([^/]+)/,) {
        $origrev = $1;
        last;
    }
}
foreach $lang (@langs) {
    $pfile = $argfile;
    $pfile =~ s+english+$lang+ or die "wrong argument: pattern does not match";
    open FILE, "$pfile" or next;
    while (<FILE>) { 
        if (/translation(\s+|=")([.0-9]*)("|\s*-->)\s*$/oi) {
            $langrev = $2;
            last;
        }
    }
    close FILE;
    # TODO - would cause unspecified results if 1. changed to 2.
    $origrev =~ s/1\.//;
    $langrev =~ s/1\.//;
    print "Orig: $origrev, lang: $langrev\n" if $debug;
    $difference = $origrev-$langrev;
    if ($difference < $stages[0]) {
        unlink "$pfile.forced";
        next;
    }
    $i = 0;
    while (defined($stages[$i])) {
        print "i = $i\n" if $debug;
        if ($stages[$i] == $difference) {
            if (when_forced($pfile) != $stages[$i]) {
                print "difference matches stage\n" if $debug;
                rebuild($pfile);
                mark_forced($pfile, $stages[$i]);
                last;
            }
        } elsif (defined($stages[$i+1])) {
            if ($difference < $stages[$i+1]) {
                if (was_forced($pfile)) {
                    if (when_forced($pfile) < $stages[$i]) {
                        print "delayed rebuild\n" if $debug;
                        rebuild($pfile);
                        mark_forced($pfile, $stages[$i]);
                        last;
                    }
                } else {
                    print "delayed rebuild (creating $pfile.forced)\n" if $debug;
                    rebuild($pfile);
                    mark_forced($pfile, $stages[$i]);
                    last;
                }
            }
        }
        $i = $i+1;
    }
}
