#!/usr/bin/env perl

# Copyright (c) 2019 Christian Jaeger, copying@christianjaeger.ch
# This is free software. See the file COPYING.md that came bundled
# with this file.

use strict;
use warnings;
use warnings FATAL => 'uninitialized';

# find modules from functional-perl working directory (not installed)
use Cwd 'abs_path';
our ($mydir, $myname);

BEGIN {
    my $location = (-l $0) ? abs_path($0) : $0;
    $location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
    ($mydir, $myname) = ($1, $2);
}
use lib "$mydir/../lib";

sub usage {
    print STDERR map {"$_\n"} @_ if @_;
    print "$myname

  Update the autogenerated POD parts in some of the modules.

";
    exit(@_ ? 1 : 0);
}

use Getopt::Long;
our $verbose = 0;
GetOptions(
    "verbose" => \$verbose,
    "help"    => sub {usage},

    #"dry-run" => \$opt_dry,
) or exit 1;
usage if @ARGV;

use FunctionalPerl ":all";

my $newpart = FunctionalPerl::export_desc2pod;

sub is_see_also {
    $_[0] =~ /^=head1 SEE ALSO/
}

sub is_head1 {
    $_[0] =~ /^=head1 /
}

my $file = "lib/FunctionalPerl.pm";

xfile_replace_lines $file, sub {
    my ($lines) = @_;
    my ($top, $rest)
        = Keep($lines)->take_while_and_rest(complement * is_see_also);
    $rest->ref(2) eq
        "This is the list of supported import tags and the modules and other tags that they import:\n"
        or die "no match";
    my ($top2, $rest2) = $rest->split_at(4);
    $top2->last eq "\n" or die "no match";
    my ($_drop, $remainder)
        = $rest2->take_while_and_rest(complement * is_head1);
    my $res = stream_append($top, $top2, cons $newpart, $remainder);

    #repl;
    $res
};

