#!/usr/bin/perl
#
# Development helper program to regenerate test data.
#
# When the templates in the DocKnot package change, all of the tests will fail
# because the output is out of date.  This program regenerates all of the test
# output data using the local instance of App::DocKnot.  It should be run from
# the root of the DocKnot distribution directory.
#
# SPDX-License-Identifier: MIT

use 5.018;
use autodie;
use warnings;

use File::Spec;

use lib 'blib/lib';

use App::DocKnot;

# For each subdirectory of t/data, regenerate each file in the output
# subdirectory using the metadata subdirectory and the template matching the
# output name.  Special-case the docknot subdirectory, which uses DocKnot's
# own metadata.
my $data = File::Spec->catdir('t', 'data');
opendir(my $datadir, $data);
my @packages = grep { -d File::Spec->catdir($data, $_) }
  File::Spec->no_upwards(readdir($datadir));
closedir($datadir);
for my $package (@packages) {
    my $metadata;
    if ($package eq 'docknot') {
        $metadata = File::Spec->catdir('docs', 'metadata');
    } else {
        $metadata = File::Spec->catdir($data, $package, 'metadata');
    }
    my $output = File::Spec->catdir($data, $package, 'output');
    opendir(my $outputdir, $output);
    for my $template (File::Spec->no_upwards(readdir($outputdir))) {
        my $docknot = App::DocKnot->new({ metadata => $metadata });
        my $outpath = File::Spec->catdir($output, $template);
        open(my $outfh, '>', $outpath);
        print {$outfh} $docknot->generate($template)
          or die "$0: cannot write to $outpath: $!\n";
        close($outfh);
    }
    closedir($outputdir);
}
