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

=head1 NAME

dh_shlibdeps - calculate shared library dependencies

=cut

use strict;
use Cwd;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_shlibdeps> [S<I<debhelper options>>] [B<-L>I<package>] [B<-l>I<directory>] [B<-X>I<item>] [S<B<--> I<params>>]

=head1 DESCRIPTION

dh_shlibdeps is a debhelper program that is responsible for calculating
shared library dependencies for packages.

This program is merely a wrapper around L<dpkg-shlibdeps(1)> that calls it
once for each package listed in the control file, passing it
a list of ELF executables and shared libraries it has found.

=head1 OPTIONS

=over 4

=item B<-X>I<item>, B<--exclude=>I<item>

Exclude files that contain "item" anywhere in their filename from being
passed to dpkg-shlibdeps. This will make their dependencies be ignored.
This may be useful in some situations, but use it with caution. This option
may be used more than once to exclude more than one thing.

=item B<--> I<params>

Pass "params" to L<dpkg-shlibdeps(1)>.

=item B<-u>I<params>, B<--dpkg-shlibdeps-params=>I<params>

This is another way to pass I<params> to L<dpkg-deb(1)>.
It is deprecated; use B<--> instead.

=item B<-l>I<directory>[:directory:directory:..]

With recent versions of dpkg-shlibdeps, this option is generally not
needed.

Before dpkg-shlibdeps is run, LD_LIBRARY_PATH will have added to it the 
specified directory (or directories -- separate with colons). With recent
versions of dpkg-shlibdeps, this is mostly only useful for packages that
build multiple flavors of the same library, or other situations where
the library is installed into a directory not on the regular library search
path.

=item B<-L>I<package>, B<--libpackage=>I<package>

With recent versions of dpkg-shlibdeps, this option is generally not
needed, unless your package builds multiple flavors of the same library.

It tells dpkg-shlibdeps (via its -S parameter) to look first in the package
build directory for the specified package, when searching for libraries,
symbol files, and shlibs files.

=back

=head1 EXAMPLES

Suppose that your source package produces libfoo1, libfoo-dev, and
libfoo-bin binary packages. libfoo-bin links against libfoo1, and should
depend on it. In your rules file, first run dh_makeshlibs, then dh_shlibdeps:

	dh_makeshlibs
	dh_shlibdeps

This will have the effect of generating automatically a shlibs file for
libfoo1, and using that file and the libfoo1 library in the
debian/libfoo1/usr/lib directory to calculate shared library dependency
information.

If a libbar1 package is also produced, that is an alternate build of
libfoo, and is installed into /usr/lib/bar/, you can make libfoo-bin depend
on libbar1 as follows:

	dh_shlibdeps -Llibbar1 -l/usr/lib/bar
	
=cut

init(options => {
	"L|libpackage=s" => \$dh{LIBPACKAGE},
	"dpkg-shlibdeps-params=s", => \$dh{U_PARAMS},
	"l=s", => \$dh{L_PARAMS},
});

if ($dh{L_PARAMS}) {
	my @paths=();
	# Add to existing paths, if set.
	push @paths, $ENV{'LD_LIBRARY_PATH'}
		if exists $ENV{'LD_LIBRARY_PATH'};
	foreach (split(/:/, $dh{L_PARAMS})) {
		# Force the path absolute.
		if (m:^/:) {
			push @paths, $_;
		}
		else {
			push @paths, "/$_";
		}
	}
	$dh{L_PARAMS}=join(':', @paths);
}

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

	# dpkg-shlibdeps expects this directory to exist
	if (! -d "$tmp/DEBIAN") {
		doit("install","-o",0,"-g",0,"-d","$tmp/DEBIAN");
	}

	my @filelist;
	my $ff;

	# Generate a list of ELF binaries in the package, ignoring any
	# we were told to exclude.
	my $find_options='';
	if (defined($dh{EXCLUDE_FIND}) && $dh{EXCLUDE_FIND} ne '') {
		$find_options="! \\( $dh{EXCLUDE_FIND} \\)";
	}
	foreach my $file (split(/\n/,`find $tmp -type f \\( -perm +111 -or -name "*.so*" -or -name "*.cmxs" \\) $find_options -print`)) {
		# Prune directories that contain separated debug symbols.
		next if $file=~m!^\Q$tmp\E/usr/lib/debug/(lib|lib64|usr|bin|sbin|opt|dev|emul)/!;
		# TODO this is slow, optimize. Ie, file can run once on
		# multiple files..
		$ff=`file "$file"`;
		if ($ff=~m/ELF/ && $ff!~/statically linked/) {
			push @filelist,$file;
		}
	}

	if (@filelist) {
		my @opts;
		if (defined $dh{LIBPACKAGE} && length $dh{LIBPACKAGE}) {
			@opts=("-S".tmpdir($dh{LIBPACKAGE}));
		}
		
		push @opts, "-tudeb" if is_udeb($package);
		
		my $ld_library_path_orig=$ENV{LD_LIBRARY_PATH};
		if ($dh{L_PARAMS}) {
			$ENV{LD_LIBRARY_PATH}=$dh{L_PARAMS};
			verbose_print("LD_LIBRARY_PATH=$dh{L_PARAMS}");
		}
		
		doit("dpkg-shlibdeps","-Tdebian/${ext}substvars",
			@opts,@{$dh{U_PARAMS}},@filelist);

		if ($dh{L_PARAMS}) {
			if (defined $ld_library_path_orig) {
				$ENV{LD_LIBRARY_PATH}=$ld_library_path_orig;
			}
			else {
				delete $ENV{LD_LIBRARY_PATH};
			}
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>, L<dpkg-shlibdeps(1)>

This program is a part of debhelper.

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
