#!/usr/bin/perl -w

# This script reads the ChangeLog.txt file and outputs it to stdout
# in the format for the blog post.

use strict;
use FindBin;

sub version_type {
  return $_[0] =~ 'a' ? 'alpha' : 'release';
}

my ($changelog, $current_tbversion, $last_tbversion);

if (!open(CHANGELOG, '<', "$FindBin::Bin/../ChangeLog-TBB.txt")) {
  print STDERR "Error opening changelog file\n";
  exit 1;
}

foreach (<CHANGELOG>) {
  if (m/^Tor Browser ([^\s]+) -/) {
    if ($current_tbversion) {
      $last_tbversion = $1;
      last if version_type($current_tbversion) eq version_type($last_tbversion);
      next;
    }
    $current_tbversion = $1;
    next;
  }

  next if $last_tbversion;

  # Remove one space at the begining of all lines
  s/^\s//;

  # Replace '*' by '-'
  s/^(\s*)\*/$1-/;

  s/&/&amp;/; s/</&lt;/; s/>/&gt;/;

  # Change bug numbers to links
  s|Bug (\d+): ([^\[]+) \[([^\]]+)\]|[Bug $3#$1](https://gitlab.torproject.org/tpo/applications/$3/-/issues/$1): $2|;

  $changelog .= $_;
}

my $changelog_branch = 'main';
if (! ( $current_tbversion =~ m/a/ ) ) {
  my @v = split(/\./, $current_tbversion);
  $changelog_branch = "maint-$v[0].$v[1]";
}
print "The full changelog since [Tor Browser $last_tbversion](https://gitlab.torproject.org/tpo/applications/tor-browser-build/-/raw/$changelog_branch/projects/browser/Bundle-Data/Docs-TBB/ChangeLog.txt) is:\n\n";

print $changelog;
