#!/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:
#             ./doc-check [-d] [lang]
#
#	(uses $lang set below if lang is not given on commandline)


# You may set this to your default language code
my $lang = "pl";
my $dodiff = 0;

if ($#ARGV >= 0) {
	if ($ARGV[0] eq '-d') {
		$dodiff = 1;
		if ($#ARGV > 0) {
			$lang = $ARGV[1];
		}
	} else {
		$lang = $ARGV[0];
	}
}

sub checkdiff
{
	my ($plfname, $enfname) = (@_);
	my ($plrev, $enrev) = getrev($plfname, $enfname);
	if ( "$plrev" ne "$enrev" ) {
		if ($dodiff) {
			system("cvs diff -u -r $plrev -r $enrev $enfname");
		} else {
			print "$enfname : $plrev -> $enrev\n";
		}
	}
}

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

	open FILE, $plfname or die "$plfname: $!";
	while (<FILE>) {
		if (/<!--\s*original version\D*([\d\.]+)\s*-->/) {
			$plrev = $1;
			last;
		}
	}
	open FILE, $enfname or die "$enfname: $!";
	while (<FILE>) {
		if (/\$Id: [^\s]+ ([\d\.]+) .* Exp \$/) {
			$enrev = $1;
			last;
		}
	}
	close FILE;
	warn "failed to find revision for $plfname" unless $plrev;
	warn "failed to find revision for $enfname" unless $enrev;
	return ($plrev, $enrev);
}

@subdocs = ("administrivia", "appendix", "dbootstrap", "hardware", "inst-methods", "partitioning", "post-install", "preparing", "rescue-boot", "tech-info", "welcome");

foreach $doc (@subdocs) {
	my $plfname = "$lang/" . "$doc" . ".sgml";
	my $enfname = "en/" . "$doc" . ".sgml";
	checkdiff($plfname, $enfname);
}
checkdiff("install.$lang.sgml", "install.sgml");
checkdiff("release-notes.$lang.sgml","release-notes.sgml");
checkdiff("index.$lang.html.m4","index.en.html.m4");
