#!/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.
#
# Copyright 2018-2023 Russ Allbery <rra@cpan.org>
#
# SPDX-License-Identifier: MIT

use 5.018;
use autodie;
use warnings;

use lib 'blib/lib';

use App::DocKnot;
use App::DocKnot::Generate;
use App::DocKnot::Spin;
use Path::Tiny qw(path);
use Pod::Thread;

# For each subdirectory of t/data/generate, 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 = path('t', 'data', 'generate');
my @packages = map { $_->basename() } grep { $_->is_dir() } $data->children();
for my $package (@packages) {
    my $metadata;
    if ($package eq 'docknot') {
        $metadata = path('docs', 'docknot.yaml');
    } else {
        $metadata = $data->child($package, 'docknot.yaml');
    }
    my $output = $data->child($package, 'output');
    for my $template (map { $_->basename() } $output->children()) {
        my $docknot = App::DocKnot::Generate->new({ metadata => $metadata });
        $docknot->generate_output($template, $output->child($template));
    }
}

# Regenerate the README and README.md for DocKnot itself.
my $docknot = App::DocKnot::Generate->new();
$docknot->generate_all();

# The test of spinning a tree of files uses a reference to App::DocKnot's own
# POD documentation.  Regenerate the expected output in case the POD has
# changed.
my $source = path('lib', 'App', 'DocKnot.pm');
my $podthread = Pod::Thread->new(navbar => 1);
my $spin = App::DocKnot::Spin::Thread->new();
my $thread;
$podthread->output_string(\$thread);
$podthread->parse_file("$source");
my $html = $spin->spin_thread($thread);

# Add the additional metadata that should be added by spin.
my $links = <<'EOD';
  <link rel="stylesheet" href="/~eagle/styles/pod.css" type="text/css" />
  <link rel="next" href="app-docknot-command.html"
        title="App::DocKnot::Command" />
  <link rel="up" href="../" title="DocKnot" />
  <link rel="top" href="../../../" />
EOD
my $comment = '<!-- Spun from DocKnot.pm by DocKnot %VERSION% on %DATE% -->';
my $navbar = <<'EOD';
<table class="navbar"><tr>
  <td class="navleft"></td>
  <td>
    <a href="../../../">Russ Allbery</a>
    &gt; <a href="../../">Software</a>
    &gt; <a href="../">DocKnot</a>
  </td>
  <td class="navright"><a href="app-docknot-command.html">App::DocKnot::Command</a>&nbsp;&gt;</td>
</tr></table>
EOD
my $address = <<'EOD';
<address>
  Last <a href="https://www.eyrie.org/~eagle/software/docknot/">spun</a>
  %DATE% from thread modified %DATE%
</address>
EOD
$html =~ s{ (</head>) }{$links$1}xms;
$html =~ s{ <!-- [ ] Spun .*? [ ] --> }{$comment}xms;
$html =~ s{ (<body> \n) }{$1$navbar}xms;
$html =~ s{ (</body>) }{$navbar\n$address$1}xms;

# Replace the expected data file.
my $output = path(
    't', 'data', 'spin', 'output', 'software', 'docknot', 'api',
    'app-docknot.html',
);
$output->spew_utf8($html);
