#!/usr/bin/perl

=head1 NAME

dh_gentdeb - build debian TDeb translation packages

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
# need to remove this module once debhelper recognises tdebs
use Parse::DebControl;

=head1 SYNOPSIS

B<dh_gentdeb> [S<I<debhelper options>>]

=head1 DESCRIPTION

dh_gentdeb prepares localisation content for a debian Tdeb
package.

Only the -p debhelper option is handled by dh_gentdeb.

=cut

=head1 DESCRIPTION

dh_gentdeb is a debhelper add-on created by Emdebian to create
translation packages (tdebs). dh_gentdeb is intended to separate
out the individual translation files from the current Debian packages
into packages without any translation files and a single TDeb package,
one per source package.

If a second tdeb is supported by one source package, the $srcpackage-tdeb
package must contain any debconf templates used by any of the binary
packages. The second tdeb is then used for translations of optional
content.

Once a package uses dh_gentdeb, translation files must be removed
from all packages in the normal build. This includes all translated
manpages and other translated content. Original, untranslated, content
should remain.

dh_gentdeb runs as a part of the normal package build - simply add
the call to the binary-indep target of debian/rules, usually after
dh_install and before dh_builddeb. dh_gentdeb handles locating the
relevant files, a .install file is not normally necessary.

Support for a tdeb diff1.gz will be added as dh_gentdeb develops. 
The extra diff is used by translators to build updated or new tdeb
packages. Tdeb packages depend on the source:Version of the mainpackage
but no packages may depend upon the tdeb. (Not even other TDebs). The
mainpackage can be specified using the -p option.

Use of diff1.gz should remove the need to create a customised source
with a debian/rules stub etc. by allowing Emdebian TDebs to be created
during an Emdebian build and Debian TDebs in a Debian build.
Translators would then be able to use:

=cut

=head1 OPTIONS

The default action is to process all available po files and all
identifiable translated content.

=cut

use vars qw/ %package_types $mainpackage $source @cmds $build $tdeb_build /;

&check_debian();
&init();
&getpackages();
# need data from debian/control
my $parser = new Parse::DebControl;
my $options = { stripComments => 1};
my $xcontrol = $parser->parse_file('./debian/control', $options);

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	} elsif (/^-p(.*)$/) {
		$mainpackage = ($1 ne "") ? $1 : shift;
	}
}

if (not defined $mainpackage) {
	for my $stanza (@$xcontrol) {
		my $type = $stanza->{'XC-Package-Type'};
		$source = $stanza->{'Source'} if (defined $stanza->{'Source'});
		if (defined $type) {
			$package_types{$stanza->{'Package'}}=$type;
		}
	}
}

foreach my $tdeb (keys %package_types) {
	push @cmds, "dpkg-gentdeb -p$tdeb";
}

if (! $dh{NO_ACT}) {
	system(@_) == 0 || _error_exitcode(join(" ", @_));
}

exit 0;

=head1 SEE ALSO

debhelper (7)

This program is based on debhelper.

=head1 AUTHOR

Neil Williams <codehelp@debian.org>

=cut

=head1 Copyright and Licence

 Copyright (C) 2007-2008  Neil Williams <codehelp@debian.org>

 This package 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/>.

=cut
