-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallControllerBuddy.sh
executable file
·839 lines (766 loc) · 31.9 KB
/
InstallControllerBuddy.sh
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#!/bin/bash
: '
Copyright (C) 2022 Matteo Hausner
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, either version 3 of the License, or
(at your option) any later version.
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 <https://www.gnu.org/licenses/>.
'
set +o history
cat << 'EOF'
╔═════════════════════════════════════════════════════════════╗
║ █▀▀ █▀█ █▀█ ▀█▀ █▀▄ █▀█ █ █ █▀▀ █▀▄ █▀▄ █ █ █▀▄ █▀▄ █ █ ║
║ █ █ █ █ █ █ █▀▄ █ █ █ █ █▀▀ █▀▄ █▀▄ █ █ █ █ █ █ █ ║
║ ▀▀▀ ▀▀▀ ▀ ▀ ▀ ▀ ▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ▀▀ ▀▀▀ ▀▀ ▀▀ ▀ ║
║ ▀█▀ █▀█ █▀▀ ▀█▀ █▀█ █ █ █▀▀ █▀▄ ║
║ █ █ █ ▀▀█ █ █▀█ █ █ █▀▀ █▀▄ ║
║ ▀▀▀ ▀ ▀ ▀▀▀ ▀ ▀ ▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ║
║ © 2022 Matteo Hausner ║
╚═════════════════════════════════════════════════════════════╝
EOF
function log() {
echo "$1"
if [ -n "$log_file" ]
then
echo "$(date -R): $1" | grep . >> "$log_file"
fi
}
function confirm_exit() {
echo
if [ "$reboot_required" = true ]
then
log 'IMPORTANT: System configuration has been modified. Please reboot your system!'
fi
read -rp 'Press enter to exit'
exit "$1"
}
if [ -z "${BASH_SOURCE[0]}" ]
then
log 'Error: Cannot determine script source'
confirm_exit 1
fi
script_name=$(basename "${BASH_SOURCE[0]}")
case "$OSTYPE" in
msys)
os=windows
log_file="$TMP\\InstallControllerBuddy.log"
vjoy_desired_version='2.1.9.1'
cb_parent_dir="$LOCALAPPDATA\\Programs"
cb_dir="$cb_parent_dir\\ControllerBuddy"
cb_bin_dir="$cb_dir"
cb_exe=ControllerBuddy.exe
cb_exe_path="$cb_bin_dir\\$cb_exe"
cb_app_dir="$cb_dir\\app"
cb_profiles_dir="$USERPROFILE\\Documents\\ControllerBuddy-Profiles"
cb_shortcuts_dir="$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\ControllerBuddy"
saved_games_dir="$USERPROFILE\\Saved Games"
dcs_stable_user_dir="$saved_games_dir\\DCS"
dcs_open_beta_user_dir="$saved_games_dir\\DCS.openbeta"
;;
linux*)
os=linux
log_file="/tmp/InstallControllerBuddy.log"
cb_parent_dir="$HOME"
cb_dir="$cb_parent_dir/ControllerBuddy"
cb_bin_dir="$cb_dir/bin"
cb_lib_dir="$cb_dir/lib"
cb_app_dir="$cb_lib_dir/app"
cb_exe=ControllerBuddy
cb_exe_path="$cb_bin_dir/$cb_exe"
if which xdg-user-dir >/dev/null 2>/dev/null
then
cb_profiles_dir="$(xdg-user-dir DOCUMENTS)/ControllerBuddy-Profiles"
else
cb_profiles_dir="$HOME/ControllerBuddy-Profiles"
fi
cb_shortcuts_dir="$HOME/.local/share/applications/ControllerBuddy"
udev_rules_file=/etc/udev/rules.d/99-controllerbuddy.rules
;;
*)
log 'Error: This script must either be run in a Git Bash for Windows or a GNU/Linux Bash environment'
confirm_exit 1
;;
esac
rm -rf "$log_file"
function check_retval() {
if [ "$?" -eq 0 ]
then
log 'Done!'
echo
else
log "$1"
confirm_exit 1
fi
}
log 'Determining system architecture...'
arch=$(uname -m)
check_retval 'Error: Failed to determine system architecture'
if ! { [ "$arch" = x86_64 ] || { [ "$os" = linux ] && [ "$arch" = aarch64 ]; }; }
then
log "Error: This script is not intended to be run on $(tr '[:lower:]' '[:upper:]' <<< ${os:0:1})${os:1} $arch systems"
confirm_exit 1
fi
tmp_dir=$(mktemp -d -q)
trap 'rm -rf $tmp_dir' EXIT
function check_sudo_privileges() {
if [ "$has_sudo_privileges" != true ]
then
if ! which sudo >/dev/null 2>/dev/null
then
log 'Error: sudo is not installed. Please restart this script after manually installing sudo.'
confirm_exit 1
fi
if sudo -v
then
has_sudo_privileges=true
else
log 'Error: User does not have sudo privileges. Please restart this script after manually adding the necessary permissions.'
confirm_exit 1
fi
fi
}
function install_package() {
if which apt-get >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo -- sh -c "apt-get update && apt-get install -y $1"
elif which yum >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo yum -y install "$2"
elif which pacman >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo pacman -S --noconfirm "$3"
elif which zypper >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo zypper --non-interactive install "$4"
else
false
fi
}
function verify_signature() {
if [ "$OSTYPE" != msys ]
then
log 'Checking if GnuPG is installed...'
if which gpg >/dev/null 2>/dev/null
then
log 'Yes'
else
log 'No - installing GnuPG...'
install_package 'gnupg' 'gnupg2' 'gnupg' 'gpg2'
check_retval 'Error: Failed to install GnuPG. Please restart this script after manually installing GnuPG.'
fi
echo
fi
log 'Verifying signature...'
local tmp_signature_file &&
tmp_signature_file=$(mktemp -p "$tmp_dir" -q) &&
curl -o "$tmp_signature_file" -L "$2" &&
local keyring_asc_content &&
keyring_asc_content=$(cat << 'EOF'
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBFTLqOYBD/9fq4bD86GtCcxYZcBeSLW7ndP5siAvxNm5NGlHBdBftfdv47XD
+oYdmL9Ypzt50SytgRhlFhcLRPPJkFpV72USSHe3+n4SYEs8N9/6YcGwWXUEiwgn
fb+O8C1vQGYouM2HUtHxQi30JAzyqbbxhwQyMGmr+anINIvk+KF28fX47g93pah0
574+KlrsoR0WQmhLmg+bSOJuzHxBnySbbFjUQh73bvOIPBqzynTQ0IDo2DjhxWdB
cJSqb8zwhT0wYxDmcc6uOkpezrBKIJK5F8ck+lmBDC6q9O0yLAwtm8eci7EfHN7n
zQTwpsB3JmlxYJD1KOLRrbSkYpEuvxrdEtXCM9wG59AsYSmA7Bg+OSYpPBhjFpMY
w2G9cOG5C10Su7Pf0H8o4BzHLWdeNgxlcaHTZHgeHDUtXgFsfBHxsCFDmDUAcCXM
odE9I9lFOWIUW8n4Po9fRa6GuNxiErzo3/2Sw2aLdRJUqdC8EQigzfcjorruxlTj
UOxo0G29vvo9+U2WFxP0uVoP2ULF7TCdt/K6dO58udLaUUDmLM9CGyG5l/q/Jxim
926xXv8h4htFGeZAk7pm2BJ1k/O5ITwZ5alUoPaizWTJ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FHD9XFb2S9Bs5JrRriGpzLQNsSO78ve/I60ZwnHRqbRp8
jBV1UXYq5Ai52Giun0fi6MtaB+s2R/FBapjHHrRfr4yfuu5zJJkoJhldA/Jjp1dJ
J6WZfcB+yNWnYIkWAMcSU0XtlXlZOZuAdfYPcj4Ph1D7rddcoo/peU485QWOlnDU
0bRqKZcRNwhkJJ5jVFMulH7w3/daLGZUPjpIeUbXI78YpaZSOgBBrHcuPWffqbTB
v/g=
=GnJF
-----END PGP PUBLIC KEY BLOCK-----
EOF
) &&
local tmp_keyring_file &&
tmp_keyring_file=$(mktemp -p "$tmp_dir" -q) &&
echo "$keyring_asc_content" | gpg --dearmor > "$tmp_keyring_file" 2>/dev/null &&
gpgv --keyring "$tmp_keyring_file" "$tmp_signature_file" "$1" >/dev/null 2>/dev/null
check_retval 'Error: Bad signature'
}
case "$1" in
'')
;;
skip-self-update)
skip_self_update=true
;;
uninstall)
uninstall=true
;;
*)
log "Error: Invalid command-line argument '$1'"
exit 1
;;
esac
if [ "$skip_self_update" != true ] && [ "$uninstall" != true ]
then
if [ "$OSTYPE" != msys ]
then
log 'Checking if cURL is installed...'
if which curl >/dev/null 2>/dev/null
then
log 'Yes'
else
log 'No - installing cURL...'
install_package 'curl' 'curl' 'curl' 'curl'
check_retval 'Error: Failed to install cURL. Please restart this script after manually installing cURL.'
fi
echo
fi
log 'Checking for the latest install script...'
if install_script_json=$(curl https://api.github.com/repos/bwRavencl/ControllerBuddy-Install-Script/releases/latest) &&
tag_name=$(grep tag_name <<< "$install_script_json" | cut -d : -f 2 | tr -d \",' ') &&
install_script_url=https://github.com/bwRavencl/ControllerBuddy-Install-Script/releases/download/$tag_name/InstallControllerBuddy.sh &&
tmp_install_script_file=$(mktemp -p "$tmp_dir" -q) &&
curl -o "$tmp_install_script_file" -L "$install_script_url"
then
if cmp -s "${BASH_SOURCE[0]}" "$tmp_install_script_file"
then
log 'Install script is already up-to-date!'
echo
else
log 'New install script available!'
echo
verify_signature "$tmp_install_script_file" "$install_script_url.sig"
log 'Updating and restarting install script...'
bash -c "mv '$tmp_install_script_file' '${BASH_SOURCE[0]}' && chmod +x '${BASH_SOURCE[0]}' && exec '${BASH_SOURCE[0]}' $1"
check_retval 'Error: Failed to update and restart install script'
exit 0
fi
else
log 'Warning: Failed to obtain latest install script from GitHub'
echo
fi
fi
function check_vjoy_installed() {
log "Checking if vJoy $vjoy_desired_version is installed..."
local vjoy_uninstall_registry_key='HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{8E31F76F-74C3-47F1-9550-E041EEDC5FBB}_is1'
vjoy_dir=$(REG QUERY "$vjoy_uninstall_registry_key" //V InstallLocation 2>/dev/null | grep InstallLocation | sed -n -e 's/^.*REG_SZ //p' | sed 's/\\*$//')
vjoy_config_exe_path="$vjoy_dir\\x64\\vJoyConfig.exe"
vjoy_current_version=$(REG QUERY "$vjoy_uninstall_registry_key" //V DisplayVersion 2>/dev/null | grep DisplayVersion | sed -n -e 's/^.*REG_SZ //p')
if [ -n "$vjoy_dir" ] &&
[ -d "$vjoy_dir" ] &&
[ -f "$vjoy_config_exe_path" ] &&
[ "$vjoy_current_version" = "$vjoy_desired_version" ]
then
vjoy_installed=true
fi
}
function get_vjoy_config_value() {
grep "$2" <<< "$1" | cut -d : -f 2 | sed 's/^[ \t]*//;s/[ \t]*$//' | xargs
}
function check_vjoy_configured() {
log 'Checking if vJoy is configured correctly...'
local vjoy_config
vjoy_config=$("$vjoy_config_exe_path" -t 1)
local vjoy_config_device
vjoy_config_device=$(get_vjoy_config_value "$vjoy_config" Device)
local vjoy_config_buttons
vjoy_config_buttons=$(get_vjoy_config_value "$vjoy_config" Buttons)
local vjoy_config_descrete_povs
vjoy_config_descrete_povs=$(get_vjoy_config_value "$vjoy_config" 'Descrete POVs')
local vjoy_config_continuous_povs
vjoy_config_continuous_povs=$(get_vjoy_config_value "$vjoy_config" 'Continous POVs')
local vjoy_config_axes
vjoy_config_axes=$(get_vjoy_config_value "$vjoy_config" Axes)
local vjoy_config_ffb_effects
vjoy_config_ffb_effects=$(get_vjoy_config_value "$vjoy_config" 'FFB Effects')
if [ "$vjoy_config_device" = 1 ] &&
[ "$vjoy_config_buttons" = 128 ] &&
[ "$vjoy_config_descrete_povs" = 0 ] &&
[ "$vjoy_config_continuous_povs" = 0 ] &&
[ "$vjoy_config_axes" = 'X Y Z Rx Ry Rz Sl0 Sl1' ] &&
[ "$vjoy_config_ffb_effects" = 'None' ]
then
vjoy_configured=true
fi
}
function check_cb_installed_version {
if [ -d "$cb_dir" ]
then
cb_installed_version=$(find "$cb_app_dir" -iname 'controllerbuddy-*.jar' -maxdepth 2 -print0 2>/dev/null | xargs -0 -I filename basename -s .jar filename | cut -d - -f 2,3)
auto_exit=true
fi
}
function remove_controller_buddy() {
if [ -d "$cb_dir" ]
then
log 'Stopping any old ControllerBuddy process...'
if { [ "$OSTYPE" = msys ] && taskkill -F -IM $cb_exe >/dev/null 2>/dev/null ; } ||
{ [ "$OSTYPE" = linux-gnu ] && killall ControllerBuddy 2>/dev/null ; }
then
sleep 2
if [ "$uninstall" != true ]
then
restart=true
fi
fi
log 'Done!'
echo
log 'Removing ControllerBuddy'
find "$cb_dir" -mindepth 1 -not -name "$script_name" -not -path "$cb_bin_dir" -delete
check_retval 'Error: Failed to remove ControllerBuddy'
fi
}
function add_line_if_missing() {
if [ ! -f "$1" ] || ! grep -qxF "$2" "$1"
then
log "Adding missing line '$2' to file '$1'..."
check_sudo_privileges
echo "$2" | sudo tee -a "$1" >/dev/null 2>/dev/null
check_retval "Error: Failed to write $1"
reboot_required=true
fi
}
function create_shortcut() {
if [ "$OSTYPE" = msys ]
then
local shortcut_path="$cb_shortcuts_dir\\$1.lnk"
else
local shortcut_path="$cb_shortcuts_dir/$1.desktop"
fi
if [ ! -f "$shortcut_path" ]
then
log "Creating '$1' shortcut..."
if [ "$OSTYPE" = msys ]
then
mkdir -p "$cb_shortcuts_dir" && create-shortcut --arguments "$3" --work-dir "$4" "$2" "$shortcut_path"
else
local exec_value=$2
if [ -n "$3" ]
then
exec_value="$exec_value $3"
fi
if [ "$1" = ControllerBuddy ]
then
local icon_value="$cb_lib_dir/ControllerBuddy.png"
local terminal_value=false
else
local icon_value='text-x-script'
local terminal_value=true
fi
mkdir -p "$cb_shortcuts_dir" && echo -e "[Desktop Entry]\nType=Application\nName=$1\nIcon=$icon_value\nExec=$exec_value\nPath=$4\nTerminal=$terminal_value\nCategories=Game" > "$shortcut_path"
fi
check_retval "Error: Failed to create '$1' shortcut"
fi
}
function add_environment_variable() {
log "Adding $1 environment variable..."
setx "$1" "$2"
check_retval "Error: Failed to add $1 environment variable"
}
function install_dcs_integration() {
if [ -d "$1" ]
then
log "Found DCS World user directory $1"
local dcs_scirpts_dir="$1\\Scripts"
local cb_dcs_integration_dir="$dcs_scirpts_dir\\ControllerBuddy-DCS-Integration"
if [ -d "$cb_dcs_integration_dir" ]
then
if [ "$uninstall" = true ]
then
rm -rf "$cb_dcs_integration_dir"
else
log 'Pulling ControllerBuddy-DCS-Integration repository...'
git -C "$cb_dcs_integration_dir" pull origin master --rebase
check_retval 'Error: Failed to pull ControllerBuddy-DCS-Integration repository'
fi
elif [ "$uninstall" != true ]
then
log 'Cloning ControllerBuddy-DCS-Integration repository...'
git clone https://github.com/bwRavencl/ControllerBuddy-DCS-Integration.git "$cb_dcs_integration_dir"
check_retval 'Error: Failed to clone ControllerBuddy-DCS-Integration repository'
fi
local export_lua_path="$dcs_scirpts_dir\\Export.lua"
log "Updating $export_lua_path for ControllerBuddy-DCS-Integration"
if [ "$uninstall" = true ]
then
sed -i "/\ControllerBuddy\b/d" "$export_lua_path"
check_retval "Error: Failed to remove ControllerBuddy-DCS-Integration from $export_lua_path"
else
touch -a "$export_lua_path"
local export_lua_line='dofile(lfs.writedir()..[[Scripts\ControllerBuddy-DCS-Integration\ControllerBuddy.lua]])'
# shellcheck disable=SC1003
grep -qxF "$export_lua_line" "$export_lua_path" || { sed -i '$a\' "$export_lua_path" && echo "$export_lua_line" >> "$export_lua_path" && unix2dos -q "$export_lua_path" ; }
check_retval "Error: Failed to add ControllerBuddy-DCS-Integration to $export_lua_path"
fi
if REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_EXECUTABLE >/dev/null 2>/dev/null
then
if [ "$uninstall" = true ]
then
log 'Removing CONTROLLER_BUDDY_EXECUTABLE environment variable...'
REG DELETE 'HKCU\Environment' //F //V CONTROLLER_BUDDY_EXECUTABLE
check_retval 'Error: Failed to remove CONTROLLER_BUDDY_EXECUTABLE environment variable'
fi
elif [ "$uninstall" != true ]
then
add_environment_variable CONTROLLER_BUDDY_EXECUTABLE "$cb_exe_path"
fi
if REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_PROFILES_DIR >/dev/null 2>/dev/null
then
if [ "$uninstall" = true ]
then
log 'Removing CONTROLLER_BUDDY_PROFILES_DIR environment variable...'
REG DELETE 'HKCU\Environment' //F //V CONTROLLER_BUDDY_PROFILES_DIR
check_retval 'Error: Failed to remove CONTROLLER_BUDDY_PROFILES_DIR environment variable'
fi
elif [ "$uninstall" != true ]
then
add_environment_variable CONTROLLER_BUDDY_PROFILES_DIR "$cb_profiles_dir"
fi
fi
}
if [ "$uninstall" = true ]
then
while true;
do
read -rp 'Are you sure you want to uninstall ControllerBuddy? [y/N] ' response
case $response in
[Yy]*)
echo
remove_controller_buddy
if [ -d "$cb_shortcuts_dir" ]
then
log 'Removing ControllerBuddy shortcuts...'
rm -rf "$cb_shortcuts_dir"
check_retval 'Error: Failed to remove ControllerBuddy shortcuts'
fi
if [ "$OSTYPE" = msys ]
then
if REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS >/dev/null 2>/dev/null
then
log 'Removing CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS environment variable...'
REG DELETE 'HKCU\Environment' //F //V CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS
check_retval 'Error: Failed to remove CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS environment variable'
fi
install_dcs_integration "$dcs_stable_user_dir" uninstall
install_dcs_integration "$dcs_open_beta_user_dir" uninstall
elif [ "$OSTYPE" = linux-gnu ]
then
log 'Removing udev rules...'
check_sudo_privileges
sudo rm -rf "$udev_rules_file"
check_retval "Error: Failed to remove udev rules file '$udev_rules_file'"
fi
rm -rf "$cb_dir" 2>/dev/null
log 'All done!'
exit 0
;;
[Nn]* | '')
log 'Bye-bye!'
exit 0
;;
*)
log "Invalid input. Please answer with 'yes' or 'no'."
echo
;;
esac
done
else
if [ "$OSTYPE" = msys ]
then
check_vjoy_installed
if [ "$vjoy_installed" != true ]
then
log "No valid vJoy $vjoy_desired_version installation was found - downloading installer..."
if tmp_vjoy_setup=$(mktemp -p "$tmp_dir" -q --suffix=.exe) &&
curl -o "$tmp_vjoy_setup" -L https://github.com/jshafer817/vJoy/releases/download/v2.1.9.1/vJoySetup.exe &&
echo "f103ced4e7ff7ccb49c8415a542c56768ed4da4fea252b8f4ffdac343074654a $tmp_vjoy_setup" | sha256sum --check --status
then
log "Installing vJoy $vjoy_desired_version..."
"$tmp_vjoy_setup" //VERYSILENT
check_vjoy_installed
else
log 'Error: Failed to obtain vJoy from GitHub'
confirm_exit 1
fi
fi
if [ "$vjoy_installed" = true ]
then
log "Found vJoy $vjoy_current_version in $vjoy_dir"
check_vjoy_configured
if [ "$vjoy_configured" = true ]
then
log 'Yes'
else
log 'No - starting elevated vJoyConfig process...'
powershell -Command "Start-Process '$vjoy_config_exe_path' '1 -f -b 128' -Verb Runas -Wait"
check_retval 'Error: Failed to start elevated vJoyConfig process'
check_vjoy_configured
if [ "$vjoy_configured" != true ]
then
log 'Error: Failed to configure vJoy device'
confirm_exit 1
fi
fi
else
log "Error: Still failed to find vJoy $vjoy_desired_version. Please restart this script after downloading and installing vJoy $vjoy_desired_version manually."
confirm_exit 1
fi
else
log 'Checking if Git is installed...'
if which git >/dev/null 2>/dev/null
then
log 'Yes'
else
log 'No - installing Git...'
install_package 'git' 'git' 'git' 'git'
check_retval 'Error: Failed to install Git. Please restart this script after manually installing Git.'
fi
echo
cb_group=controllerbuddy
log "Checking if the '$cb_group' group exists..."
if getent group "$cb_group" >/dev/null
then
log 'Yes'
else
log "No - creating the '$cb_group' group"
check_sudo_privileges
sudo /usr/sbin/groupadd -f "$cb_group"
check_retval "Error: Failed to create the '$cb_group' group"
reboot_required=true
fi
echo
log "Checking if user '$USER' is in the '$cb_group' group..."
if id -nGz "$USER" | grep -qzxF "$cb_group"
then
log 'Yes'
else
log "No - adding user '$USER' to the '$cb_group' group"
check_sudo_privileges
sudo gpasswd -a "$USER" "$cb_group"
check_retval "Error: Failed to add user '$USER' to the '$cb_group' group"
reboot_required=true
fi
echo
add_line_if_missing "$udev_rules_file" 'KERNEL=="uinput", SUBSYSTEM=="misc", MODE="0660", GROUP="controllerbuddy"'
add_line_if_missing '/etc/modules-load.d/uinput.conf' 'uinput'
fi
log 'Checking for the latest ControllerBuddy release...'
cb_json=$(curl https://api.github.com/repos/bwRavencl/ControllerBuddy/releases/latest)
check_retval 'Error: Failed to obtain ControllerBuddy release information from GitHub'
cb_latest_version=$(grep tag_name <<< "$cb_json" | cut -d : -f 2 | tr -d \",' ')
if [ -z "$cb_latest_version" ]
then
log 'Error: Failed to determine latest ControllerBuddy version'
confirm_exit 1
fi
check_cb_installed_version
if [ "$cb_installed_version" = "$cb_latest_version" ]
then
log "ControllerBuddy $cb_installed_version is up-to-date!"
echo
else
log "Determining download URL..."
archive_url=$(grep browser_download_url <<< "$cb_json" | grep "$os-${arch//_/-}" | grep -v .sig | cut -d : -f 2,3 | tr -d \",' ')
check_retval "Error: Failed to determine download URL for $cb_latest_version"
log "Downloading ControllerBuddy $cb_latest_version..."
tmp_archive_file=$(mktemp -p "$tmp_dir" -q) &&
curl -o "$tmp_archive_file" -L "$archive_url"
check_retval "Error: Failed to obtain ControllerBuddy $cb_latest_version from GitHub"
verify_signature "$tmp_archive_file" "$archive_url.sig"
if [ -d "$cb_dir" ]
then
remove_controller_buddy
fi
log 'Decompressing archive...'
if [ "$OSTYPE" = msys ]
then
mkdir -p "$cb_parent_dir" && unzip -d "$cb_parent_dir" "$tmp_archive_file"
else
mkdir -p "$cb_parent_dir" && tar xzf "$tmp_archive_file" -C "$cb_parent_dir"
fi
# shellcheck disable=SC2181
if [ "$?" -eq 0 ]
then
log 'Done!'
echo
else
log 'Error: Failed to decompress archive'
confirm_exit 1
fi
fi
create_shortcut ControllerBuddy "$cb_exe_path" '-autostart local -tray' "$cb_dir"
check_cb_installed_version
if [ -z "$cb_installed_version" ]
then
log 'Error: Failed to determine installed ControllerBuddy version'
confirm_exit 1
fi
cb_profiles_branch=$(cut -d '.' -f -2 <<< "$cb_installed_version")
if [ -d "$cb_profiles_dir" ]
then
log 'Pulling ControllerBuddy-Profiles repository...'
git -C "$cb_profiles_dir" fetch origin &&
git -C "$cb_profiles_dir" checkout "$cb_profiles_branch" &&
git -C "$cb_profiles_dir" pull origin "$cb_profiles_branch" --rebase
check_retval 'Error: Failed to pull ControllerBuddy-Profiles repository'
else
log 'Cloning ControllerBuddy-Profiles repository...'
git clone https://github.com/bwRavencl/ControllerBuddy-Profiles.git "$cb_profiles_dir" --branch "$cb_profiles_branch"
check_retval 'Error: Failed to clone ControllerBuddy-Profiles repository'
fi
if [ "$OSTYPE" = msys ]
then
if reg_query_output=$(REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS 2>/dev/null)
then
run_config_scripts=$(echo "$reg_query_output" | awk '{print tolower($3)}' | xargs)
fi
if [ "$run_config_scripts" != true ] && [ "$run_config_scripts" != false ]
then
echo The ControllerBuddy-Profiles configuration scripts can automatically configure the input settings of the following applications for usage with the official profiles:
find "$cb_profiles_dir/configs" -mindepth 2 -maxdepth 2 -name 'Configure.ps1' | cut -d / -f 3 | tr _ ' ' | xargs -I name echo - name
echo
echo If you plan to use the official profiles, it is recommended that you let the scripts make the necessary changes.
echo 'Warning: You may want to backup your current input settings now, if you do not want to lose them.'
echo Note: If you answer 'always', all scripts will be executed from now on whenever ControllerBuddy is updated, without individual prompts.
echo
while true;
do
read -rp 'Would you like to run the ControllerBuddy-Profiles configuration scripts? [yes/no/always/never] ' response
case $response in
always)
add_environment_variable CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS true
export CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS=true
;&
[Yy]*)
run_config_scripts=true
break
;;
never)
add_environment_variable CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS false
;&
[Nn]*)
run_config_scripts=false
break
;;
*)
log "Invalid input. Please answer with 'yes', 'no', 'always' or 'never'."
echo
;;
esac
done
fi
if [ "$run_config_scripts" = true ]
then
log 'Running ControllerBuddy-Profiles configuration scripts...'
config_scripts_output=$(find "$cb_profiles_dir/configs" -mindepth 2 -maxdepth 2 -name 'Configure.ps1' -exec sh -c "\
if [ \"\$CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS\" != true ]
then
echo
while true;
do
read -rp \"Would you like to run the configuration script for \$(echo \"\$1\" | cut -d / -f 3 | tr _ ' ')? [yes/no] \" response
case \$response in
[Yy]*)
break
;;
[Nn]*)
exit
;;
*)
echo \"Invalid input. Please answer with 'yes' or 'no'.\"
echo
;;
esac
done
fi
echo
powershell -ExecutionPolicy Bypass -File \"\$1\"\
" shell {} \;)
if [ -n "$config_scripts_output" ]
then
log "$config_scripts_output"
fi
log 'Done!'
echo
fi
install_dcs_integration "$dcs_stable_user_dir"
install_dcs_integration "$dcs_open_beta_user_dir"
fi
script_path="$cb_bin_dir/$script_name"
if [ ! "$(dirname "${BASH_SOURCE[0]}")" -ef "$cb_bin_dir" ]
then
log "Updating local copy of $script_name..."
cp "${BASH_SOURCE[0]}" "$script_path"
check_retval "Error: Failed to copy $script_name to $script_path"
fi
if [ "$OSTYPE" = msys ]
then
script_command="$script_path"
script_work_dir=%TMP%
else
script_command="/bin/bash $script_path"
script_work_dir=/tmp
fi
create_shortcut 'Update ControllerBuddy' "$script_command" '' "$script_work_dir"
create_shortcut 'Uninstall ControllerBuddy' "$script_command" uninstall "$script_work_dir"
if [ "$restart" = true ]
then
log 'Launching ControllerBuddy...'
if [ "$OSTYPE" = msys ]
then
start //B "" "$cb_exe_path" '-autostart' 'local' '-tray' &
else
nohup "$cb_exe_path" -autostart local -tray >/dev/null 2>/dev/null &
fi
fi
fi
log 'All done! Have a nice day!'
if [ "$auto_exit" = true ] && [ "$reboot_required" != true ]
then
for i in $(seq 5 -1 1)
do
echo -ne "\rExiting in $i second(s)..."
sleep 1
done
echo
else
confirm_exit 0
fi