-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsynergyp
executable file
·226 lines (205 loc) · 4.3 KB
/
synergyp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# A mechanism to pass arbitrary condiments
# and launch synergys with different configs
# Copyright (c) 2015 haxwithaxe
# License: GPLv3
#
# Link this to synergyps for server or synergypc for client
set -e
synergys=/usr/bin/synergys
config_dir=$HOME/.config/synergy
default_profile=default
this_script=$(basename $0)
synergy=synergys
client=
DEBUG=/dev/null
if ! [ -d $config_dir ] ;then
mkdir -p $config_dir
fi
usage(){
cat - >&2 <<EOM
$(basename $0) [-p|--profile <profile>] [-d|--default <profile>] [-c|--config <config file>] [-h|--help]
EOM
}
set_profile(){
echo Creating profile: $profile
mv $config $config_dir/${profile}.conf
}
set_default(){
echo Set default profile to: $profile
ln -sf $config_dir/${profile}.conf $default_profile
}
kill_synergy(){
(killall $synergy || killall -9 $synergy || echo $synergy was not running) 2>/dev/null
}
kill_client(){
ssh $client -- "killall synergyc || killall -9 synergyc || echo synergyc was not running on $client" 2>/dev/null || echo "Could not connect to $client"
}
use_profile(){
kill_synergy > $DEBUG
$synergy -c $config_dir/${profile}.conf > $DEBUG && echo "synergys is running with profile \"$profile\""
start_client
}
use_default(){
profile=$default_profile
use_profile
}
list_profiles(){
for p in $(ls $config_dir/*.conf); do
echo $(basename $p .conf)
done
}
validate_profile(){
if [ ! -f "$config_dir/${profile}.conf" ]; then
if [ -f "$config_dir/aliases" ]; then
for re in $(cat "$config_dir/aliases"); do
match=$(awk "$(cut -d'=' -f2- <<< $re) {print \$0}" <<< $profile)
if [[ "$match" != "" ]]; then
client=$profile
profile=$(cut -d'=' -f1 <<< $re)
validate_profile
return 0
fi
done
fi
echo "ERROR: "${profile}" is not an existing profile." 1>&2
usage
exit 1
fi
}
validate_default(){
if [ "$default" != "default" ] && [ ! -f $config_dir/${default}.conf ] ;then
echo -e "ERROR: The default profile is not set.\nuse: $(basename $0) --default <existing profile name> or $(basename $0) --default --config <config file>" >&2
usage
exit 1
fi
}
validate_config(){
if ! [ -f $config ]; then
echo "ERROR: \"${config}\" is not a file." >&2
usage
exit 1
fi
}
stop_all(){
kill_synergy
kill_client
}
start_client(){
server_ip=$(ip addr show | awk '/inet .* global/ { split($2, a,"/"); print a[1] }')
if [ -z "$client" ]; then
client=$profile
fi
if [ -n "$server_ip" ]; then
kill_client > $DEBUG
stderr=$(ssh $client -- "synergyc --display :0.0 $server_ip" 3>&1 2>&3 1>/dev/null)
if [[ $? -ne 0 ]]; then
echo $stderr
else
echo "synergyc is running on $client"
fi
else
echo "Couldn\'t determine the IP address of this machine." >&2
exit 1
fi
}
while getopts ":-:p:d:c:lkh" opt; do
case "$opt" in
-)
case "$OPTARG" in
profile)
profile="${!OPTIND}"
[[ "$profile" == "" ]] && usage && false
OPTIND=$(( $OPTIND + 1 ))
;;
default)
default="${!OPTIND}"
[[ "$default" == "" ]] && usage && false
case "$default" in
--config)
default=default
;;
-c)
default=default
;;
*)
OPTIND=$(( $OPTIND + 1 ))
;;
esac
;;
config)
config="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
;;
help)
usage
exit 1
;;
host)
client="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
;;
*)
synergy_args="$synergy_args --$OPTARG ${!OPTIND}"
;;
esac
;;
p)
[[ "$OPTARG" == "" ]] && usage && false
profile=$OPTARG
;;
d)
[[ "$OPTARG" == "" ]] && usage && false
default=$OPTARG
;;
c)
[[ "$OPTARG" == "" ]] && usage && false
config=$OPTARG
;;
l)
list_profiles
exit 0
;;
h)
usage
exit 1
;;
k)
stop_all=true
;;
*)
synergy_args="${synergy_args} $OPTARG"
;;
esac
done
if [ -n "$config" ]; then
validate_config
fi
if [ -n "$default" ]; then
validate_default
if [ "$default" = "default" ] && [ -n "$config" ]; then
profile=$default
set_profile
else
profile=$default
set_default
fi
elif [ -n "$profile" ]; then
validate_profile
if [ -n "$config" ]; then
set_profile
elif [ $stop_all ]; then
kill_synergy
kill_client
else
use_profile
fi
elif [ -n "$config" ]; then
profile=tmp
set_profile
use_profile
sleep 5
rm $config_dir/tmp.conf
else
use_default
fi