#!/bin/sh

# Copyright (c) 2012 Jonathan McDowell <noodles@earth.li>
# GNU GPL; v2 or later
# Send an entire keyring to a keyserver

set -e

if [ -z "$1" ]; then
	echo "Usage: send-keyring keyring" >&2
	exit 1
fi

# avoid gnupg touching ~/.gnupg
GNUPGHOME=$(mktemp -d -t jetring.XXXXXXXX)
export GNUPGHOME
touch $GNUPGHOME/options
trap cleanup exit
cleanup () {
	rm -rf "$GNUPGHOME"
}

keyring=$(readlink -f "$1") # gpg works better with absolute keyring paths

basename=$(basename "$keyring")

for key in $(gpg --no-auto-check-trustdb --options /dev/null --no-default-keyring --keyring "$keyring" --list-keys --keyid-format long | grep '^pub' | sed -e 's!.*/!!' -e 's/ .*//'); do
	gpg --no-auto-check-trustdb --options $GNUPGHOME/options \
		--no-default-keyring --keyring "$keyring" \
		--keyserver the.earth.li \
		--send-key $key
	gpg --no-auto-check-trustdb --options $GNUPGHOME/options \
		--no-default-keyring --keyring "$keyring" \
		--keyserver pgp.mit.edu \
		--send-key $key
done
