#!/scratchbox/tools/bin/perl -w

=head1 NAME

dh_perl - calculates perl dependencies and cleans up after MakeMaker

=cut

use strict;
use Config;
use File::Find;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_perl> [S<I<debhelper options>>] [B<-d>] [S<I<library dirs ...>>]

=head1 DESCRIPTION

dh_perl is a debhelper program that is responsible for generating
the ${perl:Depends} substitutions and adding them to substvars files.

The program will look at perl scripts and modules in your package,
and will use this information to generate a dependency on perl or
perlapi. The dependency will be substituted into your package's control
file wherever you place the token "${perl:Depends}".

dh_perl also cleans up empty directories that MakeMaker can generate when
installing perl modules.

=head1 OPTIONS

=over 4

=item B<-d>

In some specific cases you may want to depend on perl-base rather than the
full perl package. If so, you can pass the -d option to make dh_perl generate
a dependency on the correct base package. This is only necessary for some
packages that are included in the base system.

Note that this flag may cause no dependency on perl-base to be generated at
all. perl-base is Essential, so its dependency can be left out, unless a
versioned dependency is needed.

=item B<-V>

By default, scripts and architecture independent modules don't depend
on any specific version of perl.  The -V option causes the current
version of the perl (or perl-base with -d) package to be specified.

=item I<library dirs>

If your package installs perl modules in non-standard
directories, you can make dh_perl check those directories by passing their
names on the command line. It will only check the vendorlib and vendorarch
directories by default.

=back

=head1 CONFORMS TO

Debian policy, version 3.8.3

Perl policy, version 1.20

=cut

init();

my $vendorlib = substr $Config{vendorlib}, 1;
my $vendorarch = substr $Config{vendorarch}, 1;

# Cleaning the paths given on the command line
foreach (@ARGV) {
	s#/$##;
	s#^/##;
}

my $perl = 'perl';
# If -d is given, then the dependency is on perl-base rather than perl.
$perl .= '-base' if $dh{D_FLAG};
my $version;

# dependency types
use constant PROGRAM   => 1;
use constant PM_MODULE => 2;
use constant XS_MODULE => 4;

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp=tmpdir($package);

	# Check also for alternate locations given on the command line
	my @dirs = grep -d, map "$tmp/$_", $vendorlib, $vendorarch, @ARGV;

	# Look for perl modules and check where they are installed
	my $deps = 0;
	find sub {
		return unless -f;
		$deps |= PM_MODULE if /\.pm$/;
		$deps |= XS_MODULE if /\.so$/;
	}, @dirs if @dirs;

	# find scripts
	find sub {
		return unless -f and (-x or /\.pl$/);
		return if $File::Find::dir=~/\/usr\/share\/doc\//;
		
		local *F;
		return unless open F, $_;
		if (read F, local $_, 32 and m%^#!\s*(/usr/bin/perl|/usr/bin/env\s+perl)\s%) {
			$deps |= PROGRAM;
		}
		close F;
	}, $tmp;

	if ($deps) {
		my $version="";
		if ($deps & XS_MODULE or $dh{V_FLAG_SET}) {
			($version) = `dpkg -s $perl` =~ /^Version:\s*(\S+)/m
				unless $version;
			$version = ">= $version";
		}
		
		# no need to depend on an un-versioned perl-base -- it's
		# essential
		addsubstvar($package, "perl:Depends", $perl, $version)
			unless $perl eq 'perl-base' && ! length($version);

		# add perlapi-<ver> for XS modules
		addsubstvar($package, "perl:Depends", "perlapi-$Config{version}")
			if $deps & XS_MODULE;
	}

	# MakeMaker always makes lib and share dirs, but typically
	# only one directory is installed into.
	foreach my $dir ("$tmp/usr/share/perl5", "$tmp/usr/lib/perl5") {
		if (-d $dir) {
			doit("rmdir", "--ignore-fail-on-non-empty", "--parents",
				"$dir");
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Brendan O'Dea <bod@debian.org>

=cut
