gpg-agent.plugin.zsh 1.19 KB
Newer Older
Florian Walch's avatar
Florian Walch committed
1
2
local GPG_ENV=$HOME/.gnupg/gpg-agent.env

3
function start_agent_nossh {
4
5
    eval $(/usr/bin/env gpg-agent --quiet --daemon --write-env-file ${GPG_ENV} 2> /dev/null)
    chmod 600 ${GPG_ENV}
6
7
8
9
    export GPG_AGENT_INFO
}

function start_agent_withssh {
10
11
    eval $(/usr/bin/env gpg-agent --quiet --daemon --enable-ssh-support --write-env-file ${GPG_ENV} 2> /dev/null)
    chmod 600 ${GPG_ENV}
12
13
14
    export GPG_AGENT_INFO
    export SSH_AUTH_SOCK
    export SSH_AGENT_PID
Florian Walch's avatar
Florian Walch committed
15
16
}

17
18
19
20
21
# check if another agent is running
if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
    # source settings of old agent, if applicable
    if [ -f "${GPG_ENV}" ]; then
        . ${GPG_ENV} > /dev/null
22
        export GPG_AGENT_INFO
23
    fi
Florian Walch's avatar
Florian Walch committed
24

Olaf Conradi's avatar
Typo    
Olaf Conradi committed
25
    # check again if another agent is running using the newly sourced settings
26
27
28
29
30
31
32
33
34
35
    if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
        # check for existing ssh-agent
        if ssh-add -l > /dev/null 2> /dev/null; then
            # ssh-agent running, start gpg-agent without ssh support
            start_agent_nossh;
        else
            # otherwise start gpg-agent with ssh support
            start_agent_withssh;
        fi
    fi
36
fi
Florian Walch's avatar
Florian Walch committed
37
38
39

GPG_TTY=$(tty)
export GPG_TTY