#!/usr/bin/perl

# Copyright (C) 2014 Andreas Rönnquist
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#
# show_versions
#
# Shows the different CVS versions of a file in the webwml CVS repository,
# For all languages that it has been translated to.
# 
# Options:
# --no-color		Don't use any color in the output
# --help				Show this help text
#
#

sub show_help()
{
		
	print "\n";
	print "show_versions [OPTIONS] [FILE]\n";
	print "\n";
	print "Shows the different CVS versions of a file in the webwml CVS repository,\n";
	print "For all languages that it has been translated to.\n";
	print "\n";
	print "Options:\n";
	print "--no-color		Don't use any color in the output\n";
	print "--help				Show this help text\n";
	print "\n";
	print "\n";

}

use File::Path;
use File::Basename;
use Getopt::Std;

use Term::ANSIColor;

use Cwd;

use Cwd 'abs_path';

die "ERROR: Wrong amount of arguments!\n\n"
	if ( @ARGV > 2 ) ;

if ( @ARGV == 0 ) {
	&show_help;
	exit 1;
}

if (( @ARGV == 1 ) && ( ($ARGV[0]) eq "--help" )) {
	&show_help;
	exit 1;
};

if ( @ARGV == 2) {
	
	if ($ARGV[0] eq "--no-color") {
		$no_color = 1;
		$string = $ARGV[1];
	}
	
	if ($ARGV[1] eq "--no-color") {
		$no_color = 1;
		$string = $ARGV[0];
	}
	
} elsif ( @ARGV == 1) {
	$string = shift(@ARGV);
}


# Get the complete path, and not just the filename
$string = abs_path($string);

# Check if the file exists
die "ERROR: File $string doesn't exist.\n\n"
	if ! -e $string;

$string =~ s/english/*/;

$short_filename = fileparse($string);
$short_filename =~s/\R//g;
print "Filename: $short_filename\n";


@lines = `ls $string`;

foreach (@lines)
{
	$revision = "";
	
	$current_filename = "$_";
	$current_filename =~s/\R//g;
	
	$directory = "$current_filename";
	
	$directory =~ s[(.*/).*][$1];			# Destination directory (trailing /)
	
	# Remove newline
	$directory =~s/\R//g;
	
	$cvsfile = "$directory\/CVS\/Entries";
		
	$temp_path = "";
	$langauge = "";

	open TEMPFILE, "$directory\/CVS\/Repository";
	
	while (<TEMPFILE>) {
		$temp_path = $_;
		$temp_path =~s/\R//g;
	}
	
	if ($temp_path =~ /webwml\/(\w+)/) {
		$language = "$1";
	}
	
	close TEMPFILE;

	open CVS, $cvsfile
		or die "Could not read $cvsfile ($!)\n";

	while (<CVS>)
	{
		if (m[^/$short_filename/([0-9]*\.[0-9]*)/]o)
		{
			$revision = $1;
			
			$language_version{$language} = $revision;
		}
	}
		
	close CVS;

	open CURRENT, $current_filename
		or die "Couldn not read $current_filename ($!)\n";
	
	while ($line = <CURRENT>)
	{
		chomp $line;
		
		# Check that the line starts with #use, which rules out if it starts
		# with only one #.
		if ( substr($line, 0, 4) eq '#use' ) {
			if ($line =~ /translation-check translation="([0-9]*\.[0-9]*[0-9]*)"/) {
				
				$original_revision = $1;

				$temp_original_language = "english";
				
				if ($line =~ /original\="(\w+)\"/) {
					$temp_original_language = $1;
				}
				
				$original_version_number{$language} = $original_revision;
				$original_language{$language} = $temp_original_language;
			}
		}
	}
	
	close CURRENT;
}

print "------------------\n";

foreach $key (sort keys %language_version) {
	
	$value = $language_version{$key};
	printf "%12s: %5s", $key, $value;
	
	if ( defined($original_language{$key})) {
		
		$temp_lang = $original_language{$key};
		
		print " - ";
		printf "%12s, ", $original_language{$key};
		# print "$original_language{$key}, ";

		if ($original_version_number{$key} eq $language_version{$temp_lang}) {
			print color("green") if (!defined $no_color);
		} else {
			print color("red") if (!defined $no_color);
		}
		
		print "$original_version_number{$key}";
		
		print color("reset") if (!defined $no_color);
	}
	print "\n";
}
