#!/usr/bin/perl

use strict;
use warnings;

use Debian::Debhelper::Dh_Lib;
use Debian::Control;

=head1 NAME

dh_perl6_depsfile -- create rakudo helper dependency file for perl 6 module packages

=head1 SYNOPSIS

B<dh_perl6_depsfile>

=head1 DESCRIPTION

This debhelkper script will infer all perl6 module dependencies of the 
package currently built, and place these, one per line, in 
/usr/share/perl6/debian-sources/<package>/<package>.p6deps. rakudo-helper.pl 
from the rakudo package, typically called from postinst and postrm, uses this 
file to understand dependencies of packages and use them to order 
precompilation file generation and removal.

=head1 SEE ALSO

L<dh_perl6_maintscript>(1), L<dh_perl6_test>(1), L<debhelper>(7), L<dh>(1)

=cut

my $control = Debian::Control->new();
$control->read("debian/control");

foreach my $binary_package (sort keys %{$control->{binary}}) {
    my $target_dir = tmpdir($binary_package) . "/usr/share/perl6/debian-sources/$binary_package";
    install_dir($target_dir);
    open(my $fh, '>', "$target_dir/$binary_package.p6deps") or die "Could not open file: $!";
    foreach my $dep (sort map { $_->pkg } @{$control->{binary}->{$binary_package}->Depends}) {
        if ($dep =~ /^perl6-/) {
            print $fh $dep . "\n";
        }
    }
    close($fh);
}
