#!/usr/bin/env perl
# clmdbb: ChangeLog in MarkDown to BBcode
#
# Extract the top "Version" section from ChangeLog.md and convert it
# to BBCode for pasting into a forum release announcement.
#
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: © 2026 Warren Young <https://tangentsoft.com/rpn>

use strict;
use warnings;

use File::Spec;
use FindBin qw($RealBin);

my $repo_root = File::Spec->rel2abs(File::Spec->catdir($RealBin, '..'));
my $cl = File::Spec->catfile($repo_root, 'ChangeLog.md');

# Extract from first "## Version" through the line before the next "## Version"
open my $fh, '<', $cl or die "clmdbb: $cl: $!\n";
my $found = 0;
my $section = '';
while (my $line = <$fh>) {
    if ($line =~ /^## Version/) {
        if (!$found) {
            $found = 1;
            $section .= $line;
        } else {
            last;
        }
    } elsif ($found) {
        $section .= $line;
    }
}
close $fh;

# Strip date stamp from header; redundant on forums, which track this
# per post and show it.
$section =~ s/^(## Version \d+\.\d+\.\d+), \S+\n/$1\n/m;

open my $pandoc, '|-', 'pandoc', '-f', 'markdown', '-t', 'bbcode'
    or die "clmdbb: pandoc: $!\n";
print $pandoc $section;
close $pandoc;
