#!/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.
# When called with "-u" option, it will automatically update the revision
# for the translated document to the revision number for the original doc.

# Note: only use the "-u" option after you have made sure there are no
#       changes in content for the listed documents.

# SYNOPSIS:
#             ./doc-check [-d] [-u] [-v] [-V] [lang]
#
#	(uses $lang set below if lang is not given on commandline)

use Getopt::Std;
use File::Find;
$opt_d = $opt_u = $opt_v = $opt_V = 0;
getopts('duvV');
# You may set this to your default language code
$lang = shift || "pl";

sub convert
{
	my ($plfname, $plrev) = (@_);
	my $transtext = '';
	open (FILE, "< $plfname");
	while (<FILE>) {
		if (/<!--\s*\$Id: \S+ (\d+) /) {
			s/(<!--)(.*)(-->)/$1 original version: $plrev untranslated $3/;
		}
		$transtext .= $_;
	}
	close (FILE);
	warn "Writing $plfname\n" if $opt_v;
	open (FILE, "> $plfname");
	print FILE $transtext;
	close (FILE);
}

sub update
{
	my ($plfname, $plrev, $enrev) = (@_);
	my $transtext = '';
	open (FILE, "< $plfname");
	while (<FILE>) {
		if (/<!--\s*original version/) {
			s/(<!--.*\s)($plrev)(\s.*-->)/$1$enrev$3/;
		}
		$transtext .= $_;
	}
	close (FILE);
	warn "Writing $plfname\n" if $opt_v;
	open (FILE, "> $plfname");
	print FILE $transtext;
	close (FILE);
}

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

	warn "checking $plfname:\n" if $opt_v;
	open FILE, $plfname or warn "$plfname: $!\n" and return;
	while (<FILE>) {
		if (/<!--\s*original version\D*(\d+)\s*-->/) {
			$plrev = $1;
			last;
		}
		if (/<!--\s*original version\D*(\d+)\s*untranslated\s*-->/) {
			$plrev = $1;
			$untrans = 1;
			last;
		}
		# Also check for revision marks of original documents
		if (/<!--\s*\$Id: \S+ (\d+) /) {
			$plrev = $1;
			$notconverted = 1;
			$untrans = 1;
			last;
		}
	}
	warn "checking $enfname:\n" if $opt_v;
	open FILE, $enfname or warn "$enfname: $!\n" and return;
	while (<FILE>) {
		if (/<!--\s*\$Id: \S+ (\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;
	if ($notconverted) {
		warn "converting revision remark for $plfname; document marked untranslated\n";
		convert($plfname, $plrev);
	}
	return ($plrev, $enrev, $untrans);
}

sub checkdiff
{
	my ($plfname, $enfname) = (@_);
	my ($plrev, $enrev, $untrans) = getrev($plfname, $enfname);
	$plrev and $enrev or return;
	if ( "$plrev" ne "$enrev" ) {
		if ($opt_d) {
			my $s = "svn diff -r $plrev:$enrev $enfname";
			warn "running $s:\n" if ($opt_V);
			system($s);
		} else {
			if ($untrans) {
				print "$enfname : $plrev -> $enrev (untranslated)\n";
			} else {
				print "$enfname : $plrev -> $enrev\n";
			}
			if ($opt_u) {
				update($plfname, $plrev, $enrev);
				print "$plfname : revision updated\n";
			}
		}
	}
}

sub process
{
	my $enfname = $File::Find::name;
	return unless $enfname =~ m/\.xml$/;
	my $plfname = $enfname;
	$plfname =~ s,^en/,$lang/,;
	checkdiff($plfname, $enfname);
}
File::Find::find({ wanted => \&process, no_chdir => 1 }, 'en');
checkdiff("build/install.$lang.xml", "build/install.en.xml");
#checkdiff("release-notes.$lang.sgml","release-notes.sgml");
#checkdiff("index.$lang.html.m4","index.en.html.m4");
#checkdiff("dselect-beginner.$lang.sgml","dselect-beginner.sgml");
