#!/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

# changes adding the usage() option and some cosmetic changes - jfs
# copied from quick-reference (Osamu's version)
# copied from boot-floppies and s/pl/xx/g and modified
#
# SYNOPSIS:
#             ./doc-check [-d] [-v] [-V] [-e] [lang [subdoc ...]]
#	-d: diff
#	-v: verbose on reading each file header
#	-V: verbose on diff
#	-e: verbose on equal rev file (default=quiet)
#	
#
#	(will not work if language is not given in the command line)
#	(checks all files if no subdoc is specified)
#	(-d option enables to check diff)
#	Make sure to update top few lines when proofreading

use Getopt::Std;
$opt_d = $opt_v = $opt_V = $opt_e = 0;
getopts('devV');
# You may set this to your default language code
$lang = shift;
if ( ! defined ($lang) ) {
	usage();
	exit 1;
}

sub checkdiff
{
	my ($xxfname, $enfname) = (@_);
	my ($xxrev, $enrev) = getrev($xxfname, $enfname);
	if ($opt_d) {
		if ( "$xxrev" ne "$enrev" ) {
			my $s = "cvs diff -u -r $xxrev -r $enrev $enfname";
			warn "running $s:\n" if ($opt_V);
			system($s);
		}
	} else {
		if ( ("$xxrev" ne "$enrev") or $opt_e) {
			print "$enfname : $xxrev -> $enrev\n";
		}
	}
}

sub getrev
{
	my ($xxfname, $enfname) = (@_);
	my ($xxrev, $enrev) = ("1.1.1.1", "1.1.1.1"); # I'd rather fake it.
	#my ($xxrev, $enrev) = (0, 0); 

	warn "checking $xxfname:\n" if $opt_v;
	open FILE, $xxfname or die "$xxfname: $!";
	while (<FILE>) {
		if (/<!--\s*CVS revision of original english document\s*\"([\d\.]+)\"\s*-->/) {
			$xxrev = $1;
			last;
		}
	}
	warn "checking $enfname:\n" if $opt_v;
	open FILE, $enfname or die "$enfname: $!";
	while (<FILE>) {
		if (/<!--\s*CVS revision of this document\s*\"[\$]Revision:\s*([\d\.]+)\s*\$\"\s*-->/) {
			# [] around \$ is needed to keep cvs away from changing text here ;) 
			$enrev = $1;
			last;
		}
	}
	close FILE;
	warn "failed to find revision for $xxfname" unless $xxrev;
	warn "failed to find revision for $enfname" unless $enrev;
	return ($xxrev, $enrev);
}
@subdocs = (); # Initialize
push @subdocs, @ARGV; # take options

# SubDocument definitions should probably be in a separate file to
# allow doc-check to be used for all DDP documents - jfs
if ($#subdocs < 0) {
	push @subdocs, (
"after-compromise",
"after-install",
"appendix",
"automatic",
"before-begin",
"before-compromise",
"before-install",
"copyleft",
"developer",
"faq",
"infrastructure",
"intro",
"sec-tools",
"services",
"titletoc"
	);
}

foreach $doc (@subdocs) {
	my $xxfname = "$lang/" . "$doc" . ".sgml";
	my $enfname = "en/" . "$doc" . ".sgml";
	checkdiff($xxfname, $enfname);
}

exit 0;

sub usage {
# Print the usage of the program

	print "Doc-check: compare translations against original versions (using CVS)\n";
	print "doc-check [-d] [-v] [-V] language [subdoc ...]\n";
	print "\tlanguage:\tlanguage to check for\n";
	print "\t-d:\tdiff\n";
	print "\t-v:\tverbose on reading each file header\n";
	print "\t-V:\tverbose on diff\n";
	print "\t-e:\tverbose on equal rev file (default=quiet)\n";
	return;
}
