#!/usr/bin/perl

use strict;
use warnings;

use Debian::Debhelper::Dh_Lib;

=head1 NAME

dh_raku_build -- automatically build Raku module packages

=head1 SYNOPSIS

B<dh_raku_build>

=head1 DESCRIPTION

This script calls C<install-dist.p6> to build a Raku module package.

Then it injects rakudo dependencies information in C<${raku:Depends}>
variable.

In other word, your control file B<must> have a line like:

    Depends: ${raku:Depends}

=head1 SEE ALSO

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

=cut

init();

foreach my $pkg (getpackages()) {
    nonquiet_print("Pre-compiling $pkg");

    my @cmd = qw!raku /usr/share/perl6/tools/install-dist.p6 --for=vendor
                 --from=. --to=debian/tmp/pre-compiled!;
    doit({
        update_env => {
            HOME => "/nonexistent",
            RAKUDO_RERESOLVE_DEPENDENCIES => 0,
        }
    },@cmd);

    open(my $rakudo, '-|', 'raku -V') or die "Couldn't open a pipe into raku: $!";
    my $raku_version;
    while (my $line = <$rakudo>) {
        if ($line =~ /^Raku::version=([\d.]+)$/) {
            $raku_version = $1;
        }
    }

    die "Could not find raku version from 'raku -V' output\n" unless defined $raku_version;

    addsubstvar($pkg, 'raku:Depends', "raku-api-$raku_version");
}
