#!/bin/sh
set -eu

TLD="$(cat aux/tld.txt)"
. aux/lib.sh

IN_PLACE=false
while getopts 'l:n:m:i' flag; do
	case "$flag" in
		n)
			PROJECT="$OPTARG"
			;;
		m)
			MAILING_LIST="$OPTARG"
			;;
		i)
			IN_PLACE=true
			;;
		l)
			LANGS="$OPTARG"
			;;
		*)
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

assert_arg() {
	if [ -z "$1" ]; then
		echo "Missing $2" >&2
		exit 2
	fi
}

assert_arg "${PROJECT:-}"      '-n PROJECT'
assert_arg "${MAILING_LIST:-}" '-m MAILING_LIST'
assert_arg "${LANGS:-}"        '-l LANGS'


EXPECTED_EN="$(mkstemp)"
cat <<EOF | sed 's|-|\\-|g' >> "$EXPECTED_EN"


.SH AUTHORS

.MT eu@euandre.org
EuAndreh
.ME
and contributors.


.SH BUGS

.IP \(bu
Report bugs to the
.MT ~euandreh/$MAILING_LIST@lists.sr.ht
mailing list
.ME .
Use the subject "\f(CR[$PROJECT] BUG or TASK:
<description>\fR".
.IP \(bu
Browse bugs
.UR https://$TLD/$PROJECT/TODOs.html
online
.UE .
.IP \(bu
.UR https://$TLD/$PROJECT/en/
Homepage
.UE .
.IP \(bu
.UR https://lists.sr.ht/~euandreh/$MAILING_LIST?search=%5B$PROJECT%5D
Comments and discussions
.UE .
EOF

EXPECTED_PT="$(mkstemp)"
cat <<EOF | sed 's|-|\\-|g' >> "$EXPECTED_PT"


.SH AUTORES

.MT eu@euandre.org
EuAndreh
.ME
e colaboradores.


.SH BUGS

.IP \(bu
Relate bugs na
.MT ~euandreh/$MAILING_LIST@lists.sr.ht
lista de discussão
.ME .
Use o assunto "\f(CR[$PROJECT] BUG ou TASK:
<descrição>\fR".
.IP \(bu
Veja os bugs
.UR https://$TLD/$PROJECT/TODOs.html
online
.UE .
.IP \(bu
.UR https://$TLD/$PROJECT/pt/
Página inicial
.UE .
.IP \(bu
.UR https://lists.sr.ht/~euandreh/$MAILING_LIST?search=%5B$PROJECT%5D
Comentários e discussões
.UE .
EOF

EXPECTED_FR="$(mkstemp)"
cat <<EOF | sed 's|-|\\-|g' >> "$EXPECTED_FR"


.SH AUTEURS

.MT eu@euandre.org
EuAndreh
.ME
et les contributeurs.


.SH BUGS

.IP \(bu
Soumettre un bogue dans la
.MT ~euandreh/$MAILING_LIST@lists.sr.ht
liste
de diffusion
.ME .
Utilise le sujet "\f(CR[$PROJECT] BUG ou TASK:
<description>\fR".
.IP \(bu
Parcourir les bogues
.UR https://$TLD/$PROJECT/TODOs.html
en
ligne
.UE .
.IP \(bu
.UR https://$TLD/$PROJECT/fr/
Page d'accueil
.UE .
.IP \(bu
.UR https://lists.sr.ht/~euandreh/$MAILING_LIST?search=%5B$PROJECT%5D
Commentaires et discussions
.UE .
EOF

EXPECTED_EO="$(mkstemp)"
cat <<EOF | sed 's|-|\\-|g' >> "$EXPECTED_EO"


.SH AŬTOROJ

.MT eu@euandre.org
EuAndreh
.ME
kaj la kontribuuloj.


.SH MISFUNKCIOJ

.IP \(bu
Raportu misfunkcioj al la
.MT ~euandreh/$MAILING_LIST@lists.sr.ht
dissendolisto
.ME .
Uzu la subjekton "\f(CR[$PROJECT] BUG aŭ TASK:
<priskribo>\fR".
.IP \(bu
Foliumu misfunkcioj
.UR https://$TLD/$PROJECT/TODOs.html
rete
.UE .
.IP \(bu
.UR https://$TLD/$PROJECT/eo/
Ĉefpaĝo
.UE .
.IP \(bu
.UR https://lists.sr.ht/~euandreh/$MAILING_LIST?search=%5B$PROJECT%5D
Komentoj kaj diskutoj
.UE .
EOF


for from_f in "$@"; do
	for lang in $LANGS; do
		case "$lang" in
			en)
				EXPECTED="$EXPECTED_EN"
				;;
			pt)
				EXPECTED="$EXPECTED_PT"
				;;
			fr)
				EXPECTED="$EXPECTED_FR"
				;;
			eo)
				EXPECTED="$EXPECTED_EO"
				;;
			*)
				printf 'Unsupported lang: %s\n' "$lang" >&2
				exit 2
				;;
		esac

		f="$(echo "$from_f" | sed "s/\.en\./.$lang./")"
		if ! tail -n "$(wc -l < "$EXPECTED")" "$f" |
				diff - "$EXPECTED"; then
			echo "Missing metadata at the end of \"$f\" file"
			if [ "$IN_PLACE" = true ]; then
				cat "$EXPECTED" >> "$f"
			else
				exit 1
			fi
		fi
	done
done