#!/bin/sh
#
#    byobu-reconnect-sockets - source this file to re-establish
#                              GPG_AGENT_INFO and DBUS_SESSION_BUS_ADDRESS,
#                              useful when reconnecting to an existing
#                              byobu session.
#
#    Copyright (C) 2009 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


if ! echo $- | grep -qs i; then
	echo
	echo "ERROR: You must source this file, rather than execute it."
	echo "  . $0"
	echo
	#exit 1
fi

# Establish gpg-agent socket, helps when reconnecting to a detached session
# Sorry, ls -t is needed here, to sort by time
for i in $(ls -t "$HOME/.gnupg/"gpg-agent-info-*); do
	. "$i" || continue
	export GPG_AGENT_INFO && break
done

# Reconnect dbus, source the most recently touched session-bus
# Sorry, ls -t is needed here, to sort by time
for i in $(ls -t "$HOME/.dbus/session-bus/" 2>/dev/null); do
	. "$HOME/.dbus/session-bus/$i" || continue
	export DBUS_SESSION_BUS_ADDRESS && break
done
