#!@l_prefix@/lib/openpkg/bash

#   enter the SKS run-time directory as all
#   other SKS commands work relatively to it
cd @l_prefix@/var/sks || exit $?

#   create dump directory
test -d dump || mkdir dump

#   fetch and load the latest dumps
url_root="$1"
if [ ".$url_root" = . ]; then
    url_root="http://ftp.prato.linux.it/pub/keyring/dump-latest/"
fi
i=0
while true; do
    dump_file=`printf "sks-dump-%04d.pgp" $i`
    dump_url="$url_root$dump_file"
    echo "++ downloading $dump_url"
    @l_prefix@/lib/openpkg/curl -s -k -R -o dump/$dump_file $dump_url
    if [ $# -ne 0 ]; then
        rm -f dump/$dump_file*
        exit 0
    fi
    echo "++ merging into KDB $dump_file"
    @l_prefix@/sbin/sks merge dump/$dump_file
    if [ $# -ne 0 ]; then
        echo "$0:ERROR: failed to merge dump file"
        rm -f dump/$dump_file*
        exit 1
    fi
    rm -f dump/$dump_file*
    i=$[$i + 1]
done

