#!/bin/sh
set -eu

mkdir -p public

PROJECT="$1"
MAILING_LIST="$2"
TLD="$(cat aux/tld.txt)"

EXPECTED="$(mktemp)"
cat <<EOF >> "$EXPECTED"

For running the extra development-only checks, run:

\`\`\`shell
$ make dev-check
\`\`\`

and for generating the documentation HTML and website, run:

\`\`\`shell
$ make public
\`\`\`


## Links

- [home page](https://$TLD/$PROJECT/)
- [source code](https://git.euandreh.xyz/$PROJECT/)
- [bug tracking](https://$TLD/$PROJECT/TODOs.html)
- [mailing list](https://lists.sr.ht/~euandreh/$MAILING_LIST?search=%5B$PROJECT%5D)
- [CI logs](https://$TLD/$PROJECT/ci.html)
- [CHANGELOG](https://$TLD/$PROJECT/CHANGELOG.html)
EOF

RELEASES_LIST="$(mktemp)"
for version in $(git tag | perl -e 'print reverse <>'); do
	echo "- version [$version](https://git.euandreh.xyz/$PROJECT/snapshot/$PROJECT-$version.tar.gz) ([sig](https://git.euandreh.xyz/$PROJECT/snapshot/$PROJECT-$version.tar.gz.asc)), released in $(git log -1 --format=%cd --date=short "$version")" >> "$RELEASES_LIST"
done

if [ -s "$RELEASES_LIST" ]; then
	printf '\n\n## Releases\n\n' >> "$EXPECTED"
	cat "$RELEASES_LIST" >> "$EXPECTED"
fi

cat <<EOF >> "$EXPECTED"


## License

The code is licensed under [GNU Affero General Public License v3.0 or later][AGPL-3.0-or-later] (AGPL-3.0-or-later).

[AGPL-3.0-or-later]: https://www.gnu.org/licenses/agpl-3.0.en.html
EOF

if ! tail -n "$(wc -l < "$EXPECTED")" README.md | diff - "$EXPECTED"; then
	echo 'Missing metadata at the end of README.md file'
	exit 1
fi
