#!/bin/bash

typeset -g MIRROR_DIR="/opt/mirror/rx3"

export   GPG_TTY=""
export GNUPGHOME=/root/.gnupg



#gpg --list-secret-keys --with-keygrip
KEYGRIP="9F66B40A26DB46C673694A7A8842437821B33AEE"
KEY_ID="291269f4"

PASSPHRASE_FILE="/root/.gnupg/pp.txt"


# Load passphrase into gpg-agent
#PASSPHRASE=$(<"$PASSPHRASE_FILE")
PASSPHRASE="$(grep package-master@rx3.net ~/.gnupg/pp.txt | sed -e 's/"$//' -e 's/.*"//')"

/usr/libexec/gnupg2/gpg-preset-passphrase --preset --passphrase "$PASSPHRASE" "$KEYGRIP"

unset PASSPHRASE





# Distribution selection
if [[ "$1" == "-f" ]]; then
    DISTRIB_LIST="{old,9}"
else
    DISTRIB_LIST="9"
fi



# Parallel signing
#find ${MIRROR_DIR}/distrib/${DISTRIB_LIST} -name '*.rpm' | parallel -j 4 'rpm -q --qf "%{SIGPGP:pgpsig}\n" -p {} | grep -q "Key ID" || rpm --addsign {}'


find ${MIRROR_DIR}/distrib/${DISTRIB_LIST} -name '*.rpm' | parallel -j $(nproc) '
if rpm -Kv {} 2>&1 | grep -q "key ID '${KEY_ID}': OK"; then
        echo "Skipping {}..."
    else
        rpm --addsign {}
    fi
'

gpgconf --kill gpg-agent
