#!/usr/bin/perl -w

=head1 NAME

dh_xulrunner - sets xulrunner dependencies

=cut

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

dh_xulrunner adds the xulrunner binary package corresponding to the installed
xulrunner-dev package to the B<shlibs:Depends> substvar. It only does so for
packages containing an executable or a library that it detects as containing
the XPCOM glue (please note the detection is really hackish and error prone,
though most likely accurate in most cases).

Using dh_xulrunner requires a build dependency on xulrunner-dev (>= 1.9.1.3-2).

=cut

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
	# The following is mostly stolen from dh_shlibdeps
	my $tmp=tmpdir($package);
	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/) {
			my @strings = ( "GRE_HOME", "MOZ_GRE_CONF", "USE_LOCAL_GRE", "/etc/gre.d", "%s/libxul.so" );
			open FILE, "readelf -p .rodata \"$file\" |";
			while (<FILE>) {
				for(my $i = 0; $i <= $#strings; $i++) {
					if (m{$strings[$i]}) {
						splice @strings, $i, 1;
						last;
					}
				}
				if ($#strings < 0) {
					addsubstvar($package, "shlibs:Depends", "xulrunner-1.9.1");
					last;
				}
			}
			close FILE;
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of xulrunner-dev.

=head1 AUTHOR

Mike Hommey <glandium@debian.org>

=cut
