1#!/bin/sh
2#
3#    byobu-reconnect-sockets - source this file to re-establish
4#                              GPG_AGENT_INFO and DBUS_SESSION_BUS_ADDRESS,
5#                              useful when reconnecting to an existing
6#                              byobu session.
7#
8#    Copyright (C) 2009 Canonical Ltd.
9#    Copyright (C) 2012-2014 Dustin Kirkland <kirkland@byobu.org>
10#
11#    Authors: Dustin Kirkland <kirkland@byobu.org>
12#             Ryan C. Thompson <rct@thompsonclan.org>
13#
14#    This program is free software: you can redistribute it and/or modify
15#    it under the terms of the GNU General Public License as published by
16#    the Free Software Foundation, version 3 of the License.
17#
18#    This program is distributed in the hope that it will be useful,
19#    but WITHOUT ANY WARRANTY; without even the implied warranty of
20#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21#    GNU General Public License for more details.
22#
23#    You should have received a copy of the GNU General Public License
24#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26PKG="byobu"
27[ -r "$HOME/.byoburc" ] && . "$HOME/.byoburc"
28[ -z "${BYOBU_PREFIX}" ] && export BYOBU_PREFIX="@prefix@" || export BYOBU_PREFIX
29. "${BYOBU_PREFIX}/lib/${PKG}/include/common"
30
31case "$-" in
32	*i*)
33		# no-op
34	;;
35	*)
36		echo 2>&1
37		echo "ERROR: You must source this file, rather than execute it." 2>&1
38		echo "  . $0" 2>&1
39		echo 2>&1
40		exit 1
41	;;
42esac
43
44export_and_send () {
45	var="$1"
46	value="$(eval "echo \$$var")"
47	export "$var"
48	case $BYOBU_BACKEND in
49		tmux)
50			tmux setenv "$var" "$value"
51		;;
52		screen)
53			screen -X setenv "$var" "$value"
54		;;
55	esac
56}
57
58screen_update () {
59	# Ensure that screen's environment variables/values get propagated here
60	# Enable word splitting for zsh:
61	[ "x$ZSH_VERSION" != x ] && setopt local_options sh_word_split
62	tempfile=$(mktemp -q ${BYOBU_RUN_DIR}/sockets-XXXXXXXX) && {
63		for var in $VARS_TO_UPDATE; do
64			screen sh -c "echo export $var=\$$var >> \"$tempfile\""
65		done
66		. "$tempfile"
67		rm -f "$tempfile"
68	}
69}
70
71tmux_update () {
72	# Ensure that tmux's environment variables/values get propagated here
73	# Enable word splitting for zsh:
74	[ "x$ZSH_VERSION" != x ] && setopt local_options sh_word_split
75	for var in $VARS_TO_UPDATE; do
76		expr="$(tmux showenv | grep "^$var=")"
77		if [ -n "$expr" ]; then
78			export "$expr"
79		fi
80	done
81}
82
83# Pull environment variables/values from backend and update/export here
84VARS_TO_UPDATE="DISPLAY DBUS_SESSION_BUS_ADDRESS SESSION_MANAGER GPG_AGENT_INFO XDG_SESSION_COOKIE XDG_SESSION_PATH GNOME_KEYRING_CONTROL GNOME_KEYRING_PID GPG_AGENT_INFO SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID WINDOWID UPSTART_JOB UPSTART_EVENTS UPSTART_SESSION UPSTART_INSTANCE"
85
86case $BYOBU_BACKEND in
87	tmux)
88		tmux_update
89	;;
90	screen)
91		screen_update
92	;;
93esac
94
95# Establish gpg-agent socket, helps when reconnecting to a detached session
96newest "$HOME/.gnupg/gpg-agent-info-*" && . "$_RET" && export_and_send GPG_AGENT_INFO
97
98# Reconnect dbus, source the most recently touched session-bus
99# Sorry, ls -t is needed here, to sort by time
100newest "$HOME/.dbus/session-bus/*" && . "$_RET"
101
102[ -r "$BYOBU_RUN_DIR/sockets" ] && . "$BYOBU_RUN_DIR/sockets"
103export_and_send DBUS_SESSION_BUS_ADDRESS
104export_and_send SESSION_MANAGER
105
106# vi: syntax=sh ts=4 noexpandtab
107