From d66a0998d4ff4584b1dd67f5e795e45d5c5fb429 Mon Sep 17 00:00:00 2001
From: Vonfry <3413119+Vonfry@users.noreply.github.com>
Date: Sat, 26 Jan 2019 11:00:37 +0800
Subject: [PATCH 1/6] Fix gpg module condition

Fix gpg module grep regex expression, because of matching failed when `GPG_AGENT_INFO` is empty.

And use gpgconf to get socket file instead of `$GNUPGHOME`.

fix: sorin-ionescu/prezto#1681
---
 modules/gpg/init.zsh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/modules/gpg/init.zsh b/modules/gpg/init.zsh
index 7976fd7d..60d59820 100644
--- a/modules/gpg/init.zsh
+++ b/modules/gpg/init.zsh
@@ -13,14 +13,16 @@ fi
 # Set the default paths to gpg-agent files.
 _gpg_agent_conf="${GNUPGHOME:-$HOME/.gnupg}/gpg-agent.conf"
 _gpg_agent_env="${TMPDIR:-/tmp}/gpg-agent.env.$UID"
+_gpg_agent_socket="$(gpgconf --list-dir | grep "agent-socket:.*" | sed "s/agent-socket://")"
 
 # Load environment variables from previous run
 source "$_gpg_agent_env" 2> /dev/null
 
+
 # Start gpg-agent if not started.
-if [[ -z "$GPG_AGENT_INFO" && ! -S "${GNUPGHOME:-$HOME/.gnupg}/S.gpg-agent" ]]; then
+if [[ -z "$GPG_AGENT_INFO" && ! -S ${_gpg_agent_socket}" ]]; then
   # Start gpg-agent if not started.
-  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "${${${(s.:.)GPG_AGENT_INFO}[2]}:--1} gpg-agent"; then
+  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "^[0-9]* gpg-agent$"; then
     eval "$(gpg-agent --daemon | tee "$_gpg_agent_env")"
   fi
 fi

From 5ebe88bfe0cf71cbbf9645bb5c2ea3247ea18e15 Mon Sep 17 00:00:00 2001
From: Vonfry <vonfry314@gmail.com>
Date: Sat, 26 Jan 2019 11:02:42 +0800
Subject: [PATCH 2/6] gpg-module: unset socket variable

---
 modules/gpg/init.zsh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/modules/gpg/init.zsh b/modules/gpg/init.zsh
index 60d59820..1e245898 100644
--- a/modules/gpg/init.zsh
+++ b/modules/gpg/init.zsh
@@ -20,7 +20,7 @@ source "$_gpg_agent_env" 2> /dev/null
 
 
 # Start gpg-agent if not started.
-if [[ -z "$GPG_AGENT_INFO" && ! -S ${_gpg_agent_socket}" ]]; then
+if [[ -z "$GPG_AGENT_INFO" && ! -S "${_gpg_agent_socket}" ]]; then
   # Start gpg-agent if not started.
   if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "^[0-9]* gpg-agent$"; then
     eval "$(gpg-agent --daemon | tee "$_gpg_agent_env")"
@@ -49,7 +49,7 @@ if grep '^enable-ssh-support' "$_gpg_agent_conf" &> /dev/null; then
 fi
 
 # Clean up.
-unset _gpg_agent_{conf,env}
+unset _gpg_agent_{conf,env,socket}
 
 # Disable GUI prompts inside SSH.
 if [[ -n "$SSH_CONNECTION" ]]; then

From a170d5b2b2160d4222aa61598812a28b89469535 Mon Sep 17 00:00:00 2001
From: Vonfry <vonfry314@gmail.com>
Date: Sat, 26 Jan 2019 11:11:31 +0800
Subject: [PATCH 3/6] gpg module: grep conditon fix

---
 modules/gpg/init.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/gpg/init.zsh b/modules/gpg/init.zsh
index 1e245898..3bac09b1 100644
--- a/modules/gpg/init.zsh
+++ b/modules/gpg/init.zsh
@@ -22,7 +22,7 @@ source "$_gpg_agent_env" 2> /dev/null
 # Start gpg-agent if not started.
 if [[ -z "$GPG_AGENT_INFO" && ! -S "${_gpg_agent_socket}" ]]; then
   # Start gpg-agent if not started.
-  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "^[0-9]* gpg-agent$"; then
+  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "^ *[0-9]* gpg-agent$"; then
     eval "$(gpg-agent --daemon | tee "$_gpg_agent_env")"
   fi
 fi

From 2e1b45c0484ff3d6760c78e160c0c341f14520da Mon Sep 17 00:00:00 2001
From: Vonfry <vonfry314@gmail.com>
Date: Sat, 26 Jan 2019 11:17:20 +0800
Subject: [PATCH 4/6] gpg modules: use gpgconf for other variable

---
 modules/gpg/init.zsh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/modules/gpg/init.zsh b/modules/gpg/init.zsh
index 3bac09b1..9350af0f 100644
--- a/modules/gpg/init.zsh
+++ b/modules/gpg/init.zsh
@@ -11,9 +11,9 @@ if (( ! $+commands[gpg-agent] )); then
 fi
 
 # Set the default paths to gpg-agent files.
-_gpg_agent_conf="${GNUPGHOME:-$HOME/.gnupg}/gpg-agent.conf"
+_gpg_agent_conf="$(gpgconf --list-dir homedir)/gpg-agent.conf"
 _gpg_agent_env="${TMPDIR:-/tmp}/gpg-agent.env.$UID"
-_gpg_agent_socket="$(gpgconf --list-dir | grep "agent-socket:.*" | sed "s/agent-socket://")"
+_gpg_agent_socket="$(gpgconf --list-dir agent-socket)"
 
 # Load environment variables from previous run
 source "$_gpg_agent_env" 2> /dev/null

From 7d32a08454c2c018bd63b0f652d2f29088581d2f Mon Sep 17 00:00:00 2001
From: Vonfry <vonfry314@gmail.com>
Date: Sat, 26 Jan 2019 14:59:21 +0800
Subject: [PATCH 5/6] remove export after ln, because some tools will check the
 socket path such as pam ssh auth on NixOS.

---
 modules/ssh/init.zsh | 1 -
 1 file changed, 1 deletion(-)

diff --git a/modules/ssh/init.zsh b/modules/ssh/init.zsh
index 08edce57..6542f8b2 100644
--- a/modules/ssh/init.zsh
+++ b/modules/ssh/init.zsh
@@ -33,7 +33,6 @@ fi
 # Create a persistent SSH authentication socket.
 if [[ -S "$SSH_AUTH_SOCK" && "$SSH_AUTH_SOCK" != "$_ssh_agent_sock" ]]; then
   ln -sf "$SSH_AUTH_SOCK" "$_ssh_agent_sock"
-  export SSH_AUTH_SOCK="$_ssh_agent_sock"
 fi
 
 # Load identities.

From 4c7ac6ff2ebdfa0afa5fbcb2f57889786f610f90 Mon Sep 17 00:00:00 2001
From: Vonfry <mail@vonfry.name>
Date: Sun, 28 Jun 2020 15:29:50 +0800
Subject: [PATCH 6/6] gpg module: the original grep expression cannot work on
 macos, make it simple for checking

---
 modules/gpg/init.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/gpg/init.zsh b/modules/gpg/init.zsh
index d547d401..565f21d5 100644
--- a/modules/gpg/init.zsh
+++ b/modules/gpg/init.zsh
@@ -22,7 +22,7 @@ source "$_gpg_agent_env" 2> /dev/null
 # Start gpg-agent if not started.
 if [[ -z "$GPG_AGENT_INFO" && ! -S "${_gpg_agent_socket}" ]]; then
   # Start gpg-agent if not started.
-  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "${${${(s.:.)GPG_AGENT_INFO}[2]}:--1} gpg-agent"; then
+  if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q "gpg-agent"; then
     mkdir -p "$_gpg_agent_env:h"
     eval "$(gpg-agent --daemon | tee "$_gpg_agent_env")"
   fi