gpg-agent.plugin.zsh 1.16 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
22
# 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
    fi
Florian Walch's avatar
Florian Walch committed
23

24
25
26
27
28
29
30
31
32
33
34
    # check again if another agent is running using the newly sources settings
    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
35
fi
Florian Walch's avatar
Florian Walch committed
36
37
38

GPG_TTY=$(tty)
export GPG_TTY