#!/usr/bin/perl -w
#
# Usage:
#	mkxpot
#		[--quiet]
#		[--output=<output-dir>]
#		[--sub-dirs=<sub-directories>]
#		<main-file>
#
# Description:
#	mkxpot creates pot files from the xml files for an entire book
#
# Author:
#	Bernd Groh <bgroh@redhat.com>
#
=head
    Copyright (C) 2008 Red Hat, Inc.

    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 2 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, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=cut


use strict;
use warnings;
use Getopt::Long;

my $output = './';
my $quiet = 0;
my $subdirs = undef;

GetOptions(
	'q|quiet' => \$quiet,
	'o|output=s' => \$output,
	's|sub|subs|sub-dir|sub-dirs|sub-directory|sub-directories=s' => \$subdirs
);

if (!@ARGV) {
	print STDERR "Usage: mkxpot\n\t[--quiet]\n\t[--output=<output-dir>]\n\t<main-file>\n";
	exit 0;
}

my $main_file = shift(@ARGV);

if (!(-f $main_file)) {
	print STDERR "$main_file: No such file exists\n";
	exit 0;
}

my $dir = undef;
my $main = undef;

if ($main_file =~ /^(.+\/)([^\/]+\.xml)$/) {
	$dir = $1;
	$main = $2;
} elsif ($main_file =~ /^([^\/]+\.xml)$/) {
	$dir = './';
	$main = $1;
} else {
	print STDERR "$main_file: Not a valid file\n";
	exit 0;
}

if (!(-d $output)) {
	print STDERR "$output: No such directory exists\n";
	exit 0;
} elsif (!($output =~ /\/$/)) {
	$output .= '/';
}

if ($output ne './') {
	chdir $output or die $0;
}

if (!(-d 'pot')) {
	mkdir 'pot' or die $0;
}

my @subs = ();
if ($subdirs) {
	@subs = split(',', $subdirs);
}

foreach my $sub (@subs) {
	if (!(-d "pot/$sub")) {
		mkdir "pot/$sub" or die $0;
	}
}

my $file = undef;
# jfearn: found pwd to be useful
opendir(DIR, $dir) or die "Error: Cannot opendir $dir in " . `pwd`;

while (defined($file = readdir(DIR))) {
	if ($file =~ /\.xml$/) {
		my $potfile = 'pot/' . $file;
		$potfile =~ s/\.xml$/\.pot/;
		if (!$quiet) {
			system("echo -n '$file => $potfile...'");
		}
		system("xml2pot $dir$file > $potfile.raw");
		system("msguniq $potfile.raw > $potfile");
		system("rm -f $potfile.raw");
		if (!$quiet) {
			system("echo done.");
			system("msgfmt -v --output-file=$potfile.mo $potfile");
			system("rm -f $potfile.mo");
		}
	}
}
closedir(DIR);

foreach my $sub (@subs) {
	if (!($sub =~ /\/$/)) { $sub .= '/' };
	opendir(DIR, "$dir$sub") or die "Error: Cannot opendir $dir$sub";
	while (defined($file = readdir(DIR))) {
		if ($file =~ /\.xml$/) {
			my $potfile = 'pot/' . $sub . $file;
			$potfile =~ s/\.xml$/\.pot/;
			if (!$quiet) {
				system("echo -n '$sub$file => $potfile...'");
			}
			system("xml2pot $dir$sub$file > $potfile.raw");
			system("msguniq $potfile.raw > $potfile");
			system("rm -f $potfile.raw");
			if (!$quiet) {
				system("echo done.");
				system("msgfmt -v --output-file=$potfile.mo $potfile");
				system("rm -f $potfile.mo");
			}
		}
	}
	closedir(DIR);
}
