#!/bin/sh
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

# Dual Linux or msys64

prog=$( basename $0 .bash )
ROLE=proxy

PREFIX=/usr/local
[ -n "$MSYSTEM" ] && EXET=msys || EXET=sh

[ -f $PREFIX/etc/testforge/testforge.bash ] \
    && . /usr/local/etc/testforge/testforge.bash

# Dual Linux or msys64

PROXIES=""
if [ -d /etc/pacman.d/gnupg ] ; then
    ROLE=msys64
    HOMEDIR=/etc/pacman.d/gnupg
    # proxy or striaght through
    PROXIES="10.152.152.12"
  elif [ -d /q/Pg64/Msys64/etc/pacman.d/gnupg ] ; then
    ROLE=q
    HOMEDIR=/q/Pg64/Msys64/etc/pacman.d/gnupg
    ppl=/usr/local/bin/proxy_ping_lib.bash
    # /sbin/ifconfig on Debian morons and /bin/ifconfig on Gentoo
    [ "$USER" != root ] && export PATH=/sbin:$PATH
    [ -z "$MODE" ] && MODE=$( $ppl proxy_ping_mode )
    if [ "$MODE" = tor ] ; then
	PROXIES="127.0.0.1:3128"
      elif [ "$MODE" = whonix ] ; then
	PROXIES="10.0.2.15:9128"
      elif [ "$MODE" = gateway ] ; then
	PROXIES="10.0.2.15:9128"
      elif ps ax | grep -q polipo ; then
	PROXIES="127.0.0.1:3128"
    else
	echo ERROR: unknown proxy
	exit 2
    fi

  fi

if [ ! -f /etc/dirmngr/dirmngr.conf ] || \
       grep ^keyserver /etc/dirmngr/dirmngr.conf ; then
    echo ERROR: no ^keyserver in /etc/dirmngr/dirmngr.conf
    exit 1
fi

GPG="gpg --verbose --home $HOMEDIR"
$GPG --refresh-keys --verbose

ps ax | grep /usr/bin/dirmngr.bin|grep -v grep|sed -e 's/ .*//'|xargs kill

grep '^keyserver hkp' /etc/dirmngr/dirmngr.conf | \
    sed -e 's@keyserver hkp://@@' | \
    while read elt ; do
	for proxy in $PROXIES; do
            echo 1 | http_proxy=$proxy $GPG --yes \
			       --debug-level guru \
			       --keyserver hkp://$elt \
			       --search-keys abcdefghij  || exit 3$?
	    echo INFO: $proxy $elt
	done
    done

/usr/local/bin/proxy_ping_test.bash dirmngr
