gpg-agent.plugin.zsh 1.25 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
24
        export SSH_AUTH_SOCK
        export SSH_AGENT_PID
25
    fi
Florian Walch's avatar
Florian Walch committed
26

Olaf Conradi's avatar
Typo    
Olaf Conradi committed
27
    # check again if another agent is running using the newly sourced settings
28
29
30
31
32
33
34
35
36
37
    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
38
fi
Florian Walch's avatar
Florian Walch committed
39
40
41

GPG_TTY=$(tty)
export GPG_TTY