use strict;
use warnings;
use v5.20;

use Test::More;
use Path::Tiny;
use autodie qw/:all/;
use File::Copy::Recursive qw(fcopy rcopy dircopy);
use DhMakeRaku qw/setup_debian_files/;


my $email = $ENV{DEBEMAIL} = 'jb@team.org';
my $name = $ENV{DEBFULLNAME} = 'Joe Bar';
my @l = localtime;
my $year = $l[5]+1900;
my $oldyear = $year - 1;

my $test_target = shift @ARGV;

my %tests = (
    'raku-file-find' => {
        args => [ 'https://github.com/tadzik/File-Find.git', 'HEAD' ] ,
        check => [
            'control binary:raku-file-find Synopsis' => 'File::Find for Raku',
            'changelog' => qr/0\.1\.1-1/,
            'examples:raku-file-find content:0' => 'examples/*',
        ]
    },
    'raku-log' => {
        args => [ 'https://git.tyil.nl/raku/Log', 'v0.3.1' ] ,
        check => [
            'control binary:raku-log Synopsis' => 'interface for logging mechanisms to adhere to',
            'changelog' => qr/0\.3\.1-1/,
        ]
    },
);

my $wr_root = path('wr_root')->absolute;

my $source_dir = path('t/samples')->absolute;

while (my ($module, $data) = each %tests) {
    next if $test_target and $module !~ qr/$test_target/;
    my $target = $wr_root->child($module);
    note("Running tests in $target");
    $target->remove_tree;
    $target->mkpath;

    dircopy(
        $source_dir->child($module)->stringify,
        $target->stringify
    ) || die "dircopy -> $target failed: $!";

    chdir($target);

    my $root = setup_debian_files (
        $target->absolute,
        $data->{args}[0],
        $module,
        $data->{args}[1],
        1,
    );

    is($root->grab_value('control source Homepage'), $data->{args}[0],"check Homepage");

    while ($data->{check}->@*) {
        my ($path, $to_check) = splice ($data->{check}->@*, 0, 2);
        my $value = $root->grab_value($path);
        if (ref($to_check) eq 'Regexp' ) {
            like($value, $to_check,"check path '$path'");
        }
        else {
            is($value, $to_check,"check path '$path'");
        }
    }
}

done_testing;
