#!/usr/bin/perl -w

# This script checks if the translations of the documents are up to date.
# When called with "-d" option, it also prints what has changed in the
# original since last translation

# SYNOPSIS:
#             ./translation-status [-d] [-v] [lang]
#
#	(uses $lang set below if lang is not given on commandline)

use Getopt::Std;
$opt_d = $opt_v = 0;
getopts('dv');
# You may set this to your default language code
$lang = shift || "fr";

sub checkdiff {
    my ($plfname, $enfname) = (@_);
    my ($plrev, $enrev) = getrev($plfname, $enfname);
    $plrev and $enrev or return;
    if ( "$plrev" ne "$enrev" ) {
        if ($opt_d) {
            my $s = "cvs diff -b -u -r $plrev -r $enrev $enfname";
            warn "running $s:\n" if ($opt_v);
            system($s);
        } else {
            print "$enfname : $plrev -> $enrev\n";
        }
    }
}

sub getrev {
    my ($plfname, $enfname) = (@_);
    my ($plrev, $enrev) = (0, 0);

    warn "checking $plfname:\n" if $opt_v;
    open FILE, $plfname or warn "$plfname: $!\n" and return;
    while (<FILE>) {
        if (/<!entity\s*cvs-en-rev\s*"([\d\.]+)"/i) {
            $plrev = $1;
            last;
        }
    }
    warn "checking $enfname:\n" if $opt_v;
    open FILE, $enfname or warn "$enfname: $!\n" and return;
    while (<FILE>) {
        if (/\$Id: [^\s]+ ([\d\.]+) .* Exp \$/) {
            $enrev = $1;
            last;
        }
        if (/\$Revision: ([\d\.]+) \$/) {
            $enrev = $1;
            last;
        }
    }
    close FILE;
    warn "failed to find revision for $plfname\n" unless $plrev;
    warn "failed to find revision for $enfname\n" unless $enrev;
    return ($plrev, $enrev);
}

checkdiff("developers-reference.$lang.sgml", "developers-reference.sgml");
