#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;

my $perl  = -x $^X           ? $^X : "/usr/bin/env $^X";
my $sep   = $^O eq 'MSWin32' ? ';' : ':';
my $root  = path('.')->absolute;
my $bin   = $root->child('bin');
my $lib   = $root->child('lib');
my $share = $root->child('share');
my $t     = $root->child('t');

my @scripts = @_ ? @_ : $bin->children;

foreach my $script (@scripts) {
    my $t_script = $t->child( $script->basename );

    $t_script->spew_utf8(<<"EOF");
#!$perl
use strict;
use warnings;
use lib "$lib";

BEGIN {
    \$ENV{PATH} = "$t" .'$sep'.\$ENV{PATH};
    \$ENV{SHARE_DIR} //= "$share";
}
my \$ret = do "$script";
my \$err = \$@;
if (not defined \$ret) {
    die \$err if \$err;
    die "do: \$!" if \$!;
}
EOF
    $t_script->chmod('a+x');
    print 't/', $script->basename, " created\n";
}
