#!/bin/bash
#
CMD=$(basename $0)

set -e -u

stat=0

for chan in $(cat $CMD.keylist)
do
	channel=$(echo "$chan" | cut -d',' -f1)
	url=$(echo "$chan" | cut -d',' -f2)
	key=$(echo "$chan" | cut -d',' -f3)

	[ -f $channel.GPG-KEY ] || {
		echo -n " getting key $url ... "
		if wget -O tmp $url >tmp.out 2>&1
		then
			mv tmp $channel.GPG-KEY
			echo "ok"
		else
			echo "ERROR"
			cat tmp.out
			stat=1
		fi
		rm -f tmp.out
	}
	[ "$key" != "" ] && {
		[ -f $channel.KEY ] || {
			echo -n " recording key $key into $channel.KEY"
			echo "$key" >$channel.KEY
		}
	}
done
