make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / screenreattach
blob3568af22680ff41aa82c016403de744d446cdd46
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 screenreattach - Reattach to GNU/screen and import environment variables
10 =cut
12 EOF
16 set -e
17 . /usr/lib/tool/bash-utils
18 set +e
20 declare -a argv
21 declare -a screen_names
22 screen_name=''
23 new_screen=0
25 is_dir_empty()
27 local dir=$1
28 local anything=`find "$dir" -mindepth 1 -maxdepth 1 -print 1 -quit`
29 [ -z "$anything" ]
33 if [ $# = 0 ]
34 then
35 argv=(-dR)
36 else
37 argv=("$@")
41 if [ -z "$STY" ]
42 then
43 while [ -n "$1" ]
45 case "${1,,}" in
46 (*r|r)
47 shift
48 if [ ".${1:0:1}" != .- ]
49 then
50 screen_name=$1
51 break
52 fi;;
53 esac
54 shift
55 done
57 if [ -z "$screen_name" ]
58 then
59 screendir=${SCREENDIR:-/var/run/screen/S-$USER}
60 if [ ! -d "$screendir" ] || is_dir_empty "$screendir"
61 then
62 screendir=/tmp/uscreens/S-$USER
65 screen_names=(`find "$screendir" -mindepth 1 -maxdepth 1 ! -name '.*' -printf "%f\n" 2>/dev/null`)
67 if [ ${#screen_names[@]} = 1 ]
68 then
69 argv+=(-r)
70 screen_name=${screen_names[0]}
71 elif [ ${#screen_names[@]} -gt 1 ]
72 then
73 new_screen_str=$'\t'"New Screen"
74 while [ -z "$screen_name" ]
76 IFS0=$IFS
77 IFS=$'\n'
78 select screen_desc in `screen -ls | sed -e '$d' | sed -n -e '/^\S/ w/dev/stderr' -e '/^\s/ p'` "$new_screen_str"
80 if [ "$screen_desc" = "$new_screen_str" ]
81 then
82 tty=`tty`
83 tty=${tty#/dev/}
84 tty=${tty//\//-}
85 screen_name=$tty.${COLUMNS:-$(tput cols)}x${LINES:-$(tput lines)}
86 new_screen=1
87 elif [ -n "$screen_desc" ]
88 then
89 argv+=(-r)
90 screen_name=`sed -e 's/\s*\(\S\+\).*/\1/' <<<"$screen_desc"`
92 break
93 done
94 IFS=$IFS0
95 unset screen_desc
96 argv+=("$screen_name")
97 done
98 unset new_screen_str
100 unset screendir
101 unset screen_names
104 if [ $new_screen != 1 -a -n "$screen_name" ]
105 then
106 set -e
107 for env in DISPLAY SSH_AUTH_SOCK DBUS_SESSION_BUS_ADDRESS XDG_SESSION_COOKIE WINDOWID
109 if bash_defined "$env"
110 then
111 echo "setting $env in $screen_name screen" >&2
112 screen ${screen_name:+-S "$screen_name"} -X setenv "$env" "${!env}"
114 done
115 set +e
116 unset env
118 unset new_screen
119 unset screen_name
122 exec screen "${argv[@]}"