#!/usr/bin/perl

use strict;
use warnings;

use Debian::Debhelper::Dh_Lib;
use Path::Tiny;

=head1 NAME

dh_raku_build -- automatically build Raku module packages

=head1 SYNOPSIS

B<dh_raku_build>

=head1 DESCRIPTION

This script will call C<prove6 -l -v> to self-build a Raku module package. It will
only do so if three conditions are met:

=over

=item *

A 't' directory exists

=item *

raku-tap-harness is installed, typically by adding it to the build-depencies
of the package being built

=item *

The debhelper build option 'nocheck' is not set

=back

=head1 SEE ALSO

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

=cut

init();

foreach my $pkg (getpackages()) {
    nonquiet_print("Checking installation of $pkg");
    my $tmp = path(tmpdir($pkg));
    my $precomp = path('debian/tmp/pre-compiled');
    die "Cannot find pre-compiled files\n" unless $precomp->is_dir;
    my $vendor = $tmp->child("usr/lib/perl6/vendor");
    my $iter = $precomp->iterator({ recurse => 1 });

    my @to_do;
    while (my $path = $iter->()) {
        my $subpath = $path->relative($precomp);
        my $target = $vendor->child($subpath);
        push @to_do, sub {
            if ($path->is_dir) {
                install_dir($target->stringify); }
            else {
                nonquiet_print("Installing $path in $target");
                install_file ($path->stringify, $target->stringify);
            }
        };
    }
    log_installed_files($pkg, $precomp->stringify);

    next unless process_pkg($pkg);

    nonquiet_print("Installing $pkg");
    foreach my $sub (@to_do) {
        $sub->();
    }
}
