#
# If gpg is configured and gpg-agent is wanted set "yes"
#
: ${usegpg:=yes}
: ${GNUPGHOME:=$HOME/.gnupg}
: ${XDG_RUNTIME_DIR:=/run/user/$UID}

#
# No gpg-agent if a gpg session is already provided by an other agent.
#
if test "$usegpg" = yes -a -n "$GNOME_KEYRING_PID" ; then
    # gnome-keyring provides a gpg agent starting with GNOME 3
    if test -d "$GNOME_KEYRING_CONTROL"; then
	usegpg=no
    fi
fi

#
# No gpg-agent if a gpg session is already provided by an other agent.
#
if test "$usegpg" = yes -a -n "$GNOME_KEYRING_PID" ; then
    case "${WINDOWMANAGER##*/}" in
    gnome*)
	# gnome-keyring provides a gpg agent starting with GNOME 3 (the
	# gnome-version.xml file only exists in GNOME 3)
        if test -d "$GNOME_KEYRING_CONTROL" -a -f "/usr/share/gnome/gnome-version.xml"; then
            usegpg=no
        fi
    esac
fi

# Run gpg-agent only if gpg service is configured and avaliable.
# Check if there is already a running gpg-agent and if use this.

if test "$usegpg" = yes -a -d "$GNUPGHOME" && gpgagent=$(type -p gpg-agent)
then
    found=false
    version=($($gpgagent --version))
    if [[ ${version[2]} =~ [0-9]+\.[0-9]+\.[0-9]+ ]]
    then
	version=(${version[2]//\./ })
    else
	version=(0 0 0)
    fi

    if test ${version[0]} -lt 2 -o \( ${version[0]} -eq 2 -a ${version[1]} -le 0 \)
    then
	for GPG_AGENT_FILE in "$GNUPGHOME/agent.info" \
	    "$GNUPGHOME/agent.info-${HOSTNAME:-$(hostname -f)}:${DISPLAY#*:}"
	do
	    test -s "$GPG_AGENT_FILE" && . "$GPG_AGENT_FILE"
	    if test -n "$GPG_AGENT_INFO"
	    then
		GPG_AGENT_PID=${GPG_AGENT_INFO#*:}
		GPG_AGENT_PID=${GPG_AGENT_PID%%:*}
		if test -n "$GPG_AGENT_PID" && checkproc -p $GPG_AGENT_PID $gpgagent
		then
		    found=true
		    break
		fi
	    fi
	    agtfile="$GPG_AGENT_FILE"
	done
    else
	test -S $GNUPGHOME/S.gpg-agent && rm -f $GNUPGHOME/S.gpg-agent
	if ! test -e $GNUPGHOME/S.gpg-agent
	then
	    cat>$GNUPGHOME/S.gpg-agent<<-EOF
		%Assuan%
		socket=$XDG_RUNTIME_DIR/S.gpg-agent
		EOF
	    chmod 0600 $GNUPGHOME/S.gpg-agent
	fi
	GPG_AGENT_PID=$(fuser $XDG_RUNTIME_DIR/S.gpg-agent 2>/dev/null)
	if test -n "$GPG_AGENT_PID"
	then
	    GPG_AGENT_INFO=$XDG_RUNTIME_DIR/S.gpg-agent:${GPG_AGENT_PID%% }
	    found=true
	fi
	agtfile=""
    fi

    if $found
    then
	export GPG_AGENT_INFO
    else
	unset GPG_AGENT_INFO
    fi

# Use the file ~/.myagents to overide the defaults
# and if this does not exist check for enable-ssh-support
# in ~/.gnupg/gpg-agent.conf
#
# Be warned: If usessh=gpg is used then read the manual
# page gpg-agent below the option --enable-ssh-support
# and do not forget to use
#
#   gpg-connect-agent /bye
#
# as otherwise none of the commands ssh/slogin/scp will
# work!
    if [ "$usessh" == yes ]; then
	if test -s "$HOME/.myagents" ; then
	    eval $(grep -E '^use.*=.*' "$HOME/.myagents")
	elif test -s "$GNUPGHOME/gpg-agent.conf" ; then
	    grep -Eq '^enable-ssh-support' "$GNUPGHOME/gpg-agent.conf" && usessh=gpg
	fi
    fi

    if [ -z "$GPG_AGENT_INFO" ]; then
	if test "$usessh" = gpg; then
	    set -- $gpgagent --sh --daemon --enable-ssh-support --keep-display ${agtfile:+--write-env-file "$agtfile"} ${1+"$@"}
	else
	    set -- $gpgagent --sh --daemon --keep-display ${agtfile:+--write-env-file "$agtfile"} ${1+"$@"}
	fi
    fi
fi
