#!/usr/bin/env perl

use strict;
use warnings;

use Capture::Tiny qw(capture);
use File::Basename qw(dirname);
use File::Spec;
use FindBin qw($RealBin);

my $repo_root = File::Spec->catdir( $RealBin, File::Spec->updir );
my $source    = File::Spec->catfile( $repo_root, 'lib', 'Developer', 'Dashboard.pm' );
my $header    = File::Spec->catfile( $repo_root, 'docs', 'readme-header.md' );
my $target    = File::Spec->catfile( $repo_root, 'README.md' );
my $to_stdout = ( scalar grep { defined $_ && $_ eq '--stdout' } @ARGV ) ? 1 : 0;

die "Missing POD source: $source\n" if !-f $source;
die "Missing README header: $header\n" if !-f $header;
die "pod2markdown is required on PATH to sync the checkout manual\n" if !_command_on_path('pod2markdown');

my ( $stdout, $stderr, $exit ) = capture {
    system( 'pod2markdown', $source );
    return $? >> 8;
};

die "pod2markdown failed while generating the checkout manual: $stderr\n" if $exit != 0;

# The header carries the title, banner, badges and reader routing, so the
# generated NAME and VERSION sections would only repeat it. Keep the manual
# from INTRODUCTION onward.
my $manual = $stdout;
$manual =~ s/\A.*?(?=^# INTRODUCTION\n)//ms;
die "Generated manual has no INTRODUCTION section to keep after the header\n"
  if $manual eq $stdout;

my $header_text = do {
    open my $fh, '<', $header or die "Unable to read $header: $!\n";
    local $/;
    <$fh>;
};
$header_text =~ s/\s+\z/\n/;

my $generated = join "\n",
  '<!-- Generated from lib/Developer/Dashboard.pm POD by script/sync-readme-from-pod. Do not edit manually. -->',
  '',
  $header_text . "\n" . $manual;

if ($to_stdout) {
    print $generated;
    exit 0;
}

open my $fh, '>', $target or die "Unable to write $target: $!\n";
print {$fh} $generated;
close $fh or die "Unable to close $target: $!\n";

exit 0;

sub _command_on_path {
    my ($name) = @_;
    return 0 if !defined $name || $name eq '';

    # File::Spec->path splits PATH on the platform's own separator. Splitting
    # on a colon can never find a command on Windows, where PATH is joined
    # with semicolons, so the README sync checks skipped there even with
    # pod2markdown installed. On Windows the name is also tried with each
    # PATHEXT extension, because a command there is a file such as
    # pod2markdown.bat and Perl's -x goes by that extension.
    my $is_windows = $^O eq 'MSWin32';
    my @extensions = $is_windows
      ? grep { length } split /;/, ( $ENV{PATHEXT} || '.COM;.EXE;.BAT;.CMD' )
      : (q{});
    for my $dir ( File::Spec->path ) {
        next if !defined $dir || $dir eq '';
        for my $extension (@extensions) {
            my $candidate = File::Spec->catfile( $dir, $name . $extension );
            next if !-f $candidate;
            return 1 if $is_windows || -x $candidate;
        }
    }

    return 0;
}

__END__

=pod

=head1 NAME

sync-readme-from-pod - regenerate the checkout manual from the main Developer::Dashboard POD

=head1 SYNOPSIS

  script/sync-readme-from-pod
  script/sync-readme-from-pod --stdout

=head1 DESCRIPTION

This checkout-only helper regenerates the project manual used by Markdown
consumers from the canonical POD in C<Developer::Dashboard>. It exists to keep
the top-level manual and the main module documentation in exact sync and to
avoid hand-edited drift between the two copies.

It prepends the reader-facing header from F<docs/readme-header.md> (title,
banner, badges, routing table, quick start and repository map) to the
generated manual, and drops the generated NAME and VERSION sections, which
the header already covers. The manual itself is kept from INTRODUCTION
onward, unchanged.

=head1 PURPOSE

Use this helper whenever the main module POD changes and the checkout manual
must be refreshed before tests, build, or release gates.

=head1 INPUTS

=over 4

=item * no arguments

Rewrite the checkout manual in place.

=item * C<--stdout>

Print the generated manual to standard output instead of rewriting the file.

=back

=head1 OUTPUT

Either the rewritten checkout manual on disk or the generated Markdown text on
standard output.

=head1 EXAMPLES

  script/sync-readme-from-pod
  script/sync-readme-from-pod --stdout > /tmp/manual-preview

=head1 WHAT USES IT

Developers use this helper during documentation updates, and the release
metadata tests use its C<--stdout> mode to verify that the checked-in checkout
manual still matches the generated output.

=cut
