#!/usr/bin/env perl
# Generated by App::prepare4release — do not edit by hand. Re-run: prepare4release ...
use strict;
use warnings;
use utf8;

sub _readme_managed_badge_line_re {
	return qr{^\[!\[(?:License|Perl|CI|MetaCPAN package|CPAN version|CPAN testers)\]};
}

sub _strip_readme_badge_markdown_block {
	my ($text) = @_;
	$text =~ s{
		\s*<!--\s*PREPARE4RELEASE_BADGES\s*-->\s*
		.*?
		<!--\s*/PREPARE4RELEASE_BADGES\s*-->\s*
	}{}msx;

	my $re = _readme_managed_badge_line_re();
	my @lines = split /\n/, $text, -1;
	my $i = 0;
	while ( $i < @lines ) {
		if ( $lines[$i] =~ $re ) {
			my $start = $i;
			$i++;
			while ( $i < @lines && $lines[$i] =~ $re ) {
				$i++;
			}
			splice @lines, $start, $i - $start;
			$i = $start;
			next;
		}
		$i++;
	}
	return join "\n", @lines;
}

sub _insert_readme_badges_after_regen {
	my ( $text, $block ) = @_;
	if ( $text =~ /\A\x{FEFF}/ ) {
		$text =~ s/\A\x{FEFF}//;
	}
	my @lines = split /\n/, $text, -1;
	my $i = 0;
	while ( $i < @lines && $lines[$i] =~ /^\s*$/ ) {
		$i++;
	}
	if ( $i >= @lines ) {
		return $block . $text;
	}
	if ( $lines[$i] =~ /^#\s+NAME\s*$/i ) {
		my $prefix = join "\n", @lines[ 0 .. $i - 1 ];
		my $rest   = join "\n", @lines[ $i .. $#lines ];
		my $out = '';
		$out .= $prefix if length $prefix;
		$out .= "\n" if length $prefix;
		$out .= $block;
		$out .= $rest;
		return $out;
	}
	if ( $lines[$i] =~ /^#\s+/ ) {
		while ( $i < @lines && $lines[$i] =~ /^#\s+/ ) {
			$i++;
		}
		while ( $i < @lines && $lines[$i] =~ /^\s*$/ ) {
			$i++;
		}
		my $before = join "\n", @lines[ 0 .. $i - 1 ];
		my $after  = join "\n", @lines[ $i .. $#lines ];
		my $out = $before . "\n\n" . $block;
		$out .= $after if length $after;
		return $out;
	}
	return $block . $text;
}

sub badge_block {
	return <<'__P4R_BADGE_INNER__';
[![License](https://img.shields.io/badge/license-Perl%205-blue.svg)](https://github.com/neo1ite/Modern-OpenAPI-Generator/blob/main/LICENSE)
[![Perl](https://img.shields.io/badge/perl-5.26%2B-blue.svg)](https://www.perl.org/)
[![CI](https://github.com/neo1ite/Modern-OpenAPI-Generator/actions/workflows/ci.yml/badge.svg)](https://github.com/neo1ite/Modern-OpenAPI-Generator/actions/workflows/ci.yml)
[![MetaCPAN package](https://repology.org/badge/version-for-repo/metacpan/perl%3Amodern-openapi-generator.svg)](https://repology.org/project/perl%3Amodern-openapi-generator/versions)
[![CPAN version](https://badge.fury.io/pl/Modern-OpenAPI-Generator.svg)](https://metacpan.org/pod/Modern/OpenAPI/Generator)
[![CPAN testers](https://cpants.cpanauthors.org/dist/Modern-OpenAPI-Generator.svg)](https://cpants.cpanauthors.org/dist/Modern-OpenAPI-Generator)
__P4R_BADGE_INNER__
}

sub run {
	my $readme = 'README.md';
	open my $fh, '<:encoding(UTF-8)', $readme
		or die "Cannot open README.md: $!";
	local $/;
	my $text = <$fh> // '';
	close $fh;

	$text = _strip_readme_badge_markdown_block($text);
	my $inner = badge_block();
	my $block = $inner . "\n\n";
	my $new_text = _insert_readme_badges_after_regen( $text, $block );
	return if $new_text eq $text;

	open my $out, '>:encoding(UTF-8)', $readme
		or die "Cannot write README.md: $!";
	print {$out} $new_text;
	close $out;
	return;
}

run() if !caller;

1;
