########################################################################
# install test symlinks for one group derived from tests.yaml.
# group name is taken from $0 (e.g. 20-use.inst => group '20-use').
########################################################################

use v5.40;

use File::Basename        qw( basename dirname );
use File::Copy            qw( copy             );
use File::Path            qw( make_path        );
use File::Spec::Functions qw( catfile rel2abs  );
use YAML::Tiny;

########################################################################

my $group   = basename $0, '.inst';
my $unit_d  = rel2abs dirname $0;           # t/bin/install-tests.d/
my $bin_d   = dirname $unit_d;              # t/bin/
my $test_d  = dirname $bin_d;              # t/
my $yaml_f  = catfile $test_d, 'etc', 'tests.yaml';
my $stub    = catfile $unit_d, '_stub_t';

my $all     = YAML::Tiny->read( $yaml_f )
or die "Cannot read '$yaml_f': $!";

my $tests   = $all->[0]{ $group }
or die "No test group '$group' in '$yaml_f'";

my $dest_d  = catfile $test_d, $group;

make_path $dest_d;

########################################################################
# remove stale .t files
########################################################################

for my $old ( glob catfile( $dest_d, '*.t' ) )
{
    unlink $old
    or die "unlink '$old': $!";

    say "Removed: $old";
}

########################################################################
# copy _stub_t as a real file for each test in this group so that
# abs_path($0) resolves to the test's own directory, not _stub_t's.
########################################################################

for my $test ( sort keys %$tests )
{
    my $dest = catfile $dest_d, "$test.t";

    copy $stub, $dest
    or die "copy '$stub' -> '$dest': $!";

    say "$group/$test";
}
