#!/bin/bash

in=CHANGELOG.md

# Replace them with Markdown references
sed -i -e 's/(\(#[0-9]\+\))/([\1])/g' "$in"

# Populate references
cat "$in" | sponge | sed -n -e 's/.*(\[#\([0-9]\+\)\]).*/\1/gp' | sort | uniq \
| while read issue; do
    # remove old one if exists
    sed -i -e "/^\[#$issue\]:.*/d" "$in"
    echo "[#$issue]: https://github.com/datalad/datalad/issues/$issue" >> "$in";
done