#! /bin/sh

# This file generates NEWS.mw, which we use on vcsn.lrde.epita.fr:
# https://www.lrde.epita.fr/wiki/Vcsn/News_File.

set -e

: "${PANDOC=pandoc}"

: "${src=$1}"
: "${dst=$2}"
: "${srcdir=$3}"

patchdir=$srcdir/doc/NEWS.mw.patches
move_if_change=$srcdir/build-aux/bin/move-if-change

tmp=$dst.tmp

# Unfortunately the syntax of the tables supported by GFM (Gitlab's Markdown)
# is poor.  It does support <table>, but then the text file is hardly
# readable.  We use the richer syntax supported by pandoc (in particular is is
# possible to have multiline cells, something that GFM does not support), but
# indent the tables so that Gitlab renders them as plain text.
#
# We could two NEWS: one for the human reader, and one for gitlab, but gitlab
# does not allow us to specify the path to its news file, so there would be
# two news files in the top-level.  That seems wrong.
#
# Expose the tables to pandoc.
< "$src" \
  perl -p -0777 -e '
    s{!\[Vcsn Logo\]\(share/vcsn/figs/vcsn.png\)\n\n}{};
    s{(^    \+-----.*\n(^    .*\n)*)}
     {
       my $v = $1;
       $v =~ s/^    //gm;
       $v
     }gem' |
  "$PANDOC" --from markdown -t mediawiki -o "$tmp"

perl -pi -0777 -e '
   s/= Vcsn Release Notes =/{{TOC|limit=2}}/;
   s/<!--.*?-->//gs;
' "$tmp"

for p in "$patchdir"/*.patch
do
  echo "$0: applying $p"
  patch <"$p" "$tmp"
done

"$move_if_change" --verbose --color "$tmp" "$dst"
