#!/bin/sh
set -eu

PROJECT="$1"

SIGNATURES="$(git notes --ref=refs/notes/signatures/tar.gz list | cut -d\  -f2)"
for tag in $(git tag); do
	COMMIT="$(git rev-list -n1 "$tag")"
	if ! echo "$SIGNATURES" | grep -qF "$COMMIT"; then
		echo "Adding missing signature to $tag" >&2
		git notes --ref=refs/notes/signatures/tar.gz add -f -C "$(
			git archive --format tar.gz --prefix "$PROJECT-${tag#v}/" "$tag" |
			gpg --output - --armor --detach-sign |
			git hash-object -w --stdin
		)" "$tag"
	fi
done
