#!/usr/bin/perl -w

=head1 NAME

dh_phppear - calculates PHP PEAR dependencies

=cut

use strict;
use Config;
use Debian::Debhelper::Dh_Lib;
use Text::Wrap;

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<dh_phppear> is a debhelper program that is responsible for generating
the B<${phppear:*}> substitutions and adding them to substvars files.

The program will look at F<package.xml> in your package,
and will use this information to generate:

=over 2

=item *

B<${phppear:Debian-Depends}>, B<${phppear:Recommends}> and B<${phppear:Breaks}>

=item *

B<${phppear:summary}>, B<${phppear:description}> and B<${phppear:channel}>

=back

Those variables will be substituted into your package's F<control>
file wherever you place the token B<${phppear:*}>.

=head1 CONFORMS TO

Debian policy, version 3.9.2

PHP PEAR policy, version yet-to-be-written

=cut
sub _shell_exec {
	my $command=shift;
	my $output = `$command`;
	if ($? == -1) {
		error("$command failed to execute: $!");
	}
	elsif ($? & 127) {
		error("$command died with signal ".($? & 127));
	}
	elsif ($? != 0) {
		error("$command returned exit code ".($? >> 8));
	}
	return $output;
}

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $deps = _shell_exec('/usr/share/pkg-php-tools/scripts/phppkginfo -d debian_deps .');
	for (split /^/, $deps) {
		/^(\w+):\s*(.*)$/;
		addsubstvar($package, "phppear:Debian-".$1, $2);
	}
	my $summary = _shell_exec('/usr/share/pkg-php-tools/scripts/phppkginfo -d summary .');
	$summary =~ s/\.$//;
	addsubstvar($package, "phppear:summary", $summary);
	my $description = _shell_exec('/usr/share/pkg-php-tools/scripts/phppkginfo -d description .');
	local($Text::Wrap::separator) = '${Newline}';
	# Wrap and replace empty lines with a dot
	$description = join('${Newline}.${Newline}', split('${Newline}${Newline}', wrap("", "", $description)));
	addsubstvar($package, "phppear:description", $description);
	addsubstvar($package, "phppear:channel", _shell_exec('/usr/share/pkg-php-tools/scripts/phppkginfo -d channel .'));
}

=head1 SEE ALSO

L<debhelper(7)>, /usr/share/doc/pkg-php-tools/README.Debian

This program is not part of debhelper.

=head1 AUTHOR

Mathieu Parent <sathieu@debian.org>

=cut
