#!/bin/bash

# This is the best place to launch gpg-agent.
# Note: it should be launched BEFORE dbus. 
# Why? xfrun4 will not work properly, for example.
# Leonid 'darkk' Evdokimov

gpgagent="`which gpg-agent 2>/dev/null`"
if [ -n "$gpgagent" ] && [ -x "$gpgagent" ] && [ -z "$GPG_AGENT_INFO" ]; then
    if ! test -r "$HOME"/.gpg-agent-info || 
       ! kill -0 $(cut -d: -f 2 "$HOME"/.gpg-agent-info) 2>/dev/null
    then
        # should I add --batch option ?
        "$gpgagent" --daemon --write-env-file "$HOME"/.gpg-agent-info
    fi
    GPG_AGENT_INFO=$(cut -d= -f 2- "$HOME"/.gpg-agent-info)
    export GPG_AGENT_INFO
fi