#!/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/>.

PKG="byobu"

case "$-" in
	*i*)
		# no-op
	;;
	*)
		# screen -X -S "$PKG" at "$USER*" stuff ". $0"; then
		echo 2>&1
		echo "ERROR: You must source this file, rather than execute it." 2>&1
		echo "  . $0" 2>&1
		echo 2>&1
		exit 1
	;;
esac

# 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-* 2>/dev/null); 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
