#!/bin/bash


version=$1
if [ -z "${version}" ]; then
	echo "usage: update <version>"
	exit 1
fi
set -eux
url="http://ftpmirror.gnu.org/parallel/parallel-${version}.tar.bz2"
file="/tmp/parallel-${version}.tgz"
wget "$url" -O "${file}"
wget "$url.sig" -O "${file}.sig"

# verify published signature comes from expected GPG key
gpg -n --verify "${file}.sig" "${file}" 2>&1 | tee "${file}.gpglog"
set +x
KEYID="$(grep '^Primary key fingerprint:' "${file}.gpglog" | tr -d ' ' | cut -f 2 -d : )"
fingerprint="CDA01A4208C4F74506107E7BD1AB451688888888"
if [ "$KEYID" != "$fingerprint" ]; then
	echo "Signature verification FAILED:"
	cat "${file}.gpglog"
	exit 1
fi

set -x

( cd "$(dirname "$0")" && \
	0install run http://0install.net/2006/interfaces/0publish \
		--add-version ${version} \
		--archive-url="${url}" \
		--archive-file="${file}" \
		--archive-extract="parallel-${version}" \
		--set-released=today \
		--xmlsign parallel.xml ) && \
	git commit -am "0install: added version $version" -e