#!/bin/sh

# Copyright (c) 2014 Sorokin Alexei <sor.alexei@meowr.ru>
# You can redistribute this program and/or modify under the terms of 2-clause BSDL.

gpg_list() {
    rpm -qa --nodigest --nosignature --qf "$1%{VERSION}-%{RELEASE}\n$2" 'gpg-pubkey*'
    return $?
}

gpg_del() {
    if [ "$(id -u)" != 0 ]; then
        echo 'Root privileges are required.' >&2
        exit 1
    fi
    rpm -e "gpg-pubkey-$1"
    return $?
}

if [ "$1" = "list" ]; then
    gpg_list 'pub\t' 'uid \t%{SUMMARY}\n\n'
elif [ "$1" = "del" ]; then
    shift
    for key in "$@"; do
        gpg_del "$key" && echo "OK"
    done
elif [ "$1" = "del-all" ]; then
    for key in $(gpg_list '' ''); do
        echo "$key"
        gpg_del "$key" && echo "OK"
    done
else
    echo "Usage: zypp-key [command]"; echo
    echo "Manage zypper's list of trusted keys"; echo
    echo "  zypp-key del <keyid>    - remove the key <keyid>"
    echo "  zypp-key del-all        - remove all keys"
    echo "  zypp-key list           - list keys"
fi
