#!/usr/bin/perl

=head1 NAME

dh_rtems - Generate RTEMS dependency information

=cut

use strict;
use Debian::Debhelper::Dh_Lib;
use Parse::DebControl;

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This helper uses the 'X-Rtems-Build-Depends' tag in the source section of 'debian/control'
to create a list of binary package dependencies for RTEMS libraries using the
correct bsp name.  This list is used to define the substvar '${rtems:Depends}'
for each package.

The list of names given in 'X-Rtems-Build-Depends' are of the form 'rtems-modulename'
or 'rtems' for RTEMS itself.  These names are expanded to the full
'rtems-modulename-bspname'.  This list will almost always include 'rtems'.

Since RTEMS does not use ELF shared libraries the usual methods for automatically
generating binary package dependencies will not work.  Instead this program assumes
that the depended packages are compatible in the range VER-CUR through VER-9999.
If the current version of the package 'rtems-mvme3100' is 4.9.3-4, then this becomes:
4.9.3-4 through 4.9.3-9999.

This assumes that every upstream change is ABI incompatible.  While not
always true, absent significant auditing, it is the only safe default.

Note: For compatibility 'Rtems-Depends' is also recognized.

=cut

init();

my $parser = new Parse::DebControl;

my $control = $parser->parse_file('./debian/control', { useTieIxHash => 'true' });

my @rdeps;
my %srcfields = %{$control->[0]};

while (my ($k,$v) = each(%srcfields)) {
	next unless( $k =~ m/^(?:X[BCS]*-)?Rtems-(?:Build-)?Depends$/ );
	@rdeps = split(",",$v);
	foreach my $rdep (@rdeps) {
		chomp($rdep);
	}
	last;
}

foreach my $pkg(getpackages()) {
	verbose_print("Package: $pkg\n");

	# Skip packages not named 'rtems-modulename-bspname'
	if( $pkg !~ /^rtems-[^-]+-[^-]+$/) {
		next;
	}

	my ($mod, $bsp) = ($pkg =~ /^rtems-(.+)-([^-]+)$/);

	# for each listed dependency
	foreach my $rdep (@rdeps) {
		$rdep =~ s/^\s+//;
		$rdep =~ s/\s+$//;

		# Find version of <module>--<bspname> (eg. rtems-mvme3100 or rtems-cexp-mvme3100)
		my $ver = `dpkg-query -W --showformat='\${Version}' $rdep-$bsp`;

		# The following requires perl >= 5.10
		if( ${^CHILD_ERROR_NATIVE} != 0 ) {
			warning("Package name $rdep-$bsp not installed");
			next;
		}

		# construct condition for direct dependency
		if( $ver =~ m/^(.*)-[^-]*$/m ) {
			# Non-native package
			verbose_print("Dep: $rdep-$bsp (>= $ver), $rdep-$bsp (<= $1-9999)\n");
			addsubstvar($pkg, "rtems:Depends", "$rdep-$bsp", ">= $ver");
			addsubstvar($pkg, "rtems:Depends", "$rdep-$bsp", "<= $1-9999");
		} else {
			# Native Debian package
			verbose_print("Dep: $rdep-$bsp\n");
			addsubstvar($pkg, "rtems:Depends", "$rdep-$bsp", ">= $ver");
			# this should cover any binary rebuilds...
			addsubstvar($pkg, "rtems:Depends", "$rdep-$bsp", "<= ${ver}++");
		}

		my $depends = `dpkg-query -W --showformat='\${Depends}\n' $rdep-$bsp`;

		# copy conditions for indirect dependencies
		if(length $depends) {
			my @deps = split(",", $depends);
			my @res = ();
			for my $dep (@deps) {
				# only those involving bsp specific packages
				unshift(@res,$dep) if( $dep =~ m/$bsp/);
			}
			addsubstvar($pkg, "rtems:Depends", join(", ", @res) );
		}
	}

}

=head1 SEE ALSO

L<debhelper(7)>, L<epics-debhelper(7)>

This program is a not part of the official debhelper package.

=head1 AUTHOR

Michael Davidsaver <mdavidsaver@gmail.com>

=cut
