Repository navigation
Expand file tree
/
Copy pathInstallControllerBuddy.sh
More file actions
executable file
·1059 lines (959 loc) · 36.7 KB
/
Copy pathInstallControllerBuddy.sh
File metadata and controls
executable file
·1059 lines (959 loc) · 36.7 KB
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env 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/>.
'
log() {
local -r message="$1"
local target_fd=1
if [[ "$message" == [eE]rror* ]]
then
target_fd=2
fi
echo "$message" >&"$target_fd"
if [ -n "$log_file" ]
then
echo "$(date -R): $message" | grep . >> "$log_file"
fi
}
confirm_exit() {
local -r exit_code="$1"
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 "$exit_code"
}
check_retval() {
local -r retval=$?
local -r error_message="$1"
local -r exit_on_error="${2:-true}"
if [ "$retval" -eq 0 ]
then
log 'Done!'
echo
return 0
fi
log "$error_message"
if [ "$exit_on_error" = true ]
then
confirm_exit 1
fi
echo
local response
while true
do
read -rp 'Would you like to continue anyway? [y/N] ' response
case $response in
[Yy]*)
log 'Continuing...'
echo
break
;;
[Nn]* | '')
log 'Aborted!'
exit 1
;;
*)
log "Invalid input. Please answer with 'yes' or 'no'."
echo
;;
esac
done
}
check_sudo_privileges() {
if [ "$has_sudo_privileges" = true ]
then
return 0
fi
if ! command -v sudo >/dev/null 2>&1
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
}
install_package() {
local -r apt_package="$1"
local -r rpm_package="$2"
local -r pacman_package="$3"
local -r zypper_package="$4"
if command -v apt-get >/dev/null 2>&1
then
check_sudo_privileges
sudo -- sh -c "apt-get update && apt-get install -y $apt_package"
elif command -v dnf >/dev/null 2>&1
then
check_sudo_privileges
sudo dnf -y install "$rpm_package"
elif command -v yum >/dev/null 2>&1
then
check_sudo_privileges
sudo yum -y install "$rpm_package"
elif command -v pacman >/dev/null 2>&1
then
check_sudo_privileges
sudo pacman -S --noconfirm "$pacman_package"
elif command -v zypper >/dev/null 2>&1
then
check_sudo_privileges
sudo zypper --non-interactive install "$zypper_package"
else
false
fi
}
verify_signature() {
local -r file="$1"
local -r signature_url="$2"
if [ "$os" != windows ]
then
log 'Checking if GnuPG is installed...'
if command -v gpg >/dev/null 2>&1
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" -fL "$signature_url" &&
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
w2G9cOG5C10Su7Pf0H8o4BzHLWdeNgxlcaHTZHg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TRjwB7AbRSPAEoPX48PDPE+x5DOnqr4x2JI3gh8KXs
iKo9SaRlMPWL8xd5yhSPk/z/tGuVnSBF1ogiP62O6lVCSGOzlUDvvmTaqxpIbnIL
iGsrV+qqCKTtTImibU1rg6Hmzsu8Mr70LrmCpiUUJS9u5cxyaM+kJxg+tAtVDvAJ
IlWPbF3mEZEs9TGS/7hFHD9XFb2S9Bs5JrRriGpzLQNsSO78ve/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" "$file" >/dev/null 2>&1
check_retval 'Error: Bad signature'
}
check_cb_installed_version() {
if [ -f "$modules_path" ]
then
cb_installed_version=$(grep -aoP -m 1 '(?<=ControllerBuddy )\d+\.\d+\.\d+-[a-z0-9]+' "$modules_path")
fi
}
remove_controller_buddy() {
if [ ! -d "$cb_dir" ]
then
return 0
fi
if [ "$os" = windows ]
then
local -r stop_command=(taskkill -F -IM "$cb_exe")
else
log 'Checking if killall is installed...'
if command -v killall >/dev/null 2>&1
then
log 'Yes'
else
log 'No - installing psmisc...'
install_package 'psmisc' 'psmisc' 'psmisc' 'psmisc'
check_retval 'Error: Failed to install psmisc. Please restart this script after manually installing psmisc.'
fi
echo
local -r stop_command=(killall ControllerBuddy)
fi
log 'Stopping any old ControllerBuddy process...'
if "${stop_command[@]}" >/dev/null 2>&1
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'
}
create_shortcut() {
local -r name="$1"
local -r target="$2"
local -r arguments="$3"
local -r work_dir="$4"
if [ "$os" = windows ]
then
local -r shortcut_path="$cb_shortcuts_dir\\$name.lnk"
else
local -r shortcut_path="$cb_shortcuts_dir/$name.desktop"
fi
log "Creating '$name' shortcut..."
if [ "$os" = windows ]
then
mkdir -p "$cb_shortcuts_dir" && create-shortcut --arguments "$arguments" --work-dir "$work_dir" "$target" "$shortcut_path"
else
local exec_value="\"${target//%/%%}\""
if [ -n "$arguments" ]
then
exec_value="$exec_value $arguments"
fi
readonly exec_value
if [ "$name" = ControllerBuddy ]
then
local -r icon_value="$cb_lib_dir/ControllerBuddy.png"
local -r terminal_value=false
else
local -r icon_value='text-x-script'
local -r terminal_value=true
fi
mkdir -p "$cb_shortcuts_dir" && printf '%s\n' \
'[Desktop Entry]' \
'Type=Application' \
"Name=$name" \
"Icon=$icon_value" \
"Exec=$exec_value" \
"Path=$work_dir" \
"Terminal=$terminal_value" \
'Categories=Game' > "$shortcut_path"
fi
check_retval "Error: Failed to create '$name' shortcut"
}
check_vjoy_installed() {
log "Checking if vJoy $vjoy_desired_version is installed..."
local -r 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
}
get_vjoy_config_value() {
local -r vjoy_config="$1"
local -r value_name="$2"
grep "$value_name" <<< "$vjoy_config" | cut -d : -f 2 | sed 's/^[ \t]*//;s/[ \t]*$//' | xargs
}
check_vjoy_configured() {
log 'Checking if vJoy is configured correctly...'
local -r vjoy_config=$("$vjoy_config_exe_path" -t 1)
local -r vjoy_config_device=$(get_vjoy_config_value "$vjoy_config" Device)
local -r vjoy_config_buttons=$(get_vjoy_config_value "$vjoy_config" Buttons)
local -r vjoy_config_descrete_povs=$(get_vjoy_config_value "$vjoy_config" 'Descrete POVs')
local -r vjoy_config_continuous_povs=$(get_vjoy_config_value "$vjoy_config" 'Continous POVs')
local -r vjoy_config_axes=$(get_vjoy_config_value "$vjoy_config" Axes)
local -r 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
}
add_environment_variable() {
local -r name="$1"
local -r value="$2"
log "Adding $name environment variable..."
setx "$name" "$value"
check_retval "Error: Failed to add $name environment variable"
}
remove_environment_variable() {
local -r name="$1"
if REG QUERY 'HKCU\Environment' //V "$name" >/dev/null 2>&1
then
log "Removing $name environment variable..."
REG DELETE 'HKCU\Environment' //F //V "$name"
check_retval "Error: Failed to remove $name environment variable"
fi
}
install_dcs_integration() {
local -r dcs_user_dir="$1"
if [ ! -d "$dcs_user_dir" ]
then
return 0
fi
log "Found DCS World user directory '$dcs_user_dir'"
local -r dcs_scripts_dir="$dcs_user_dir\\Scripts"
local -r cb_dcs_integration_dir="$dcs_scripts_dir\\ControllerBuddy-DCS-Integration"
if [ -d "$cb_dcs_integration_dir" ]
then
if [ "$uninstall" = true ]
then
log "Removing '$cb_dcs_integration_dir'..."
rm -rf "$cb_dcs_integration_dir"
check_retval "Error: Failed to remove '$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' false
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 -r export_lua_path="$dcs_scripts_dir\\Export.lua"
if [ "$uninstall" != true ] || [ -f "$export_lua_path" ]
then
log "Updating '$export_lua_path' for ControllerBuddy-DCS-Integration..."
if [ "$uninstall" = true ]
then
sed -i '/\bControllerBuddy\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 -r 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
fi
if [ "$uninstall" != true ]
then
if ! REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_EXECUTABLE >/dev/null 2>&1
then
add_environment_variable CONTROLLER_BUDDY_EXECUTABLE "$cb_exe_path"
fi
if ! REG QUERY 'HKCU\Environment' //V CONTROLLER_BUDDY_PROFILES_DIR >/dev/null 2>&1
then
add_environment_variable CONTROLLER_BUDDY_PROFILES_DIR "$cb_profiles_dir"
fi
fi
}
uinput_accessible() {
[ -r /dev/uinput ] && [ -w /dev/uinput ]
}
queue_file_update() {
local -r file="$1"
local -r content="$2"
if [ "$(cat "$file" 2>/dev/null)" != "$content" ]
then
pending_updates+=("$file" "$content")
fi
}
banner=$(
cat << 'EOF'
╔═════════════════════════════════════════════════════════════╗
║ █▀▀ █▀█ █▀█ ▀█▀ █▀▄ █▀█ █ █ █▀▀ █▀▄ █▀▄ █ █ █▀▄ █▀▄ █ █ ║
║ █ █ █ █ █ █ █▀▄ █ █ █ █ █▀▀ █▀▄ █▀▄ █ █ █ █ █ █ █ ║
║ ▀▀▀ ▀▀▀ ▀ ▀ ▀ ▀ ▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ▀▀ ▀▀▀ ▀▀ ▀▀ ▀ ║
║ ▀█▀ █▀█ █▀▀ ▀█▀ █▀█ █ █ █▀▀ █▀▄ ║
║ █ █ █ ▀▀█ █ █▀█ █ █ █▀▀ █▀▄ ║
║ ▀▀▀ ▀ ▀ ▀▀▀ ▀ ▀ ▀ ▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ║
║ © 2022-2026 Matteo Hausner ║
╚═════════════════════════════════════════════════════════════╝
EOF
)
readonly banner
if command -v tput >/dev/null 2>&1 && [ "$(tput colors 2>/dev/null || echo 0)" -ge 256 ]
then
start_color=72
end_color=75
diff=$(( end_color - start_color ))
total_lines=0
while IFS= read -r _
do
total_lines=$(( total_lines + 1 ))
done <<<"$banner"
interior_lines=$(( total_lines - 3 ))
current_line=0
while IFS= read -r line
do
current_line=$(( current_line + 1 ))
if [ "$current_line" -eq 1 ] || [ "$current_line" -eq "$(( total_lines - 1 ))" ] || [ "$current_line" -eq "$total_lines" ]
then
printf '%s\n' "$line"
continue
fi
pos=$(( current_line - 2 ))
if [ "$interior_lines" -gt 1 ]
then
step=$(( diff * pos / (interior_lines - 1) ))
else
step=0
fi
color_code=$(( start_color + step ))
first_char=${line:0:1}
last_char=${line: -1}
middle_part=${line:1:${#line}-2}
printf '%s\e[38;5;%dm%s\e[0m%s\n' "$first_char" "$color_code" "$middle_part" "$last_char"
done <<<"$banner"
printf '\n'
else
printf '%s\n\n' "$banner"
fi
if [ -z "${BASH_SOURCE[0]}" ]
then
log 'Error: Cannot determine script source'
confirm_exit 1
fi
script_name=$(basename "${BASH_SOURCE[0]}")
readonly script_name
case "$OSTYPE" in
cygwin | msys)
readonly os=windows
readonly log_file="$TMP\\InstallControllerBuddy.log"
readonly vjoy_desired_version='2.2.2.0'
readonly cb_parent_dir="$LOCALAPPDATA\\Programs"
readonly cb_dir="$cb_parent_dir\\ControllerBuddy"
readonly cb_bin_dir="$cb_dir"
readonly cb_exe=ControllerBuddy.exe
readonly cb_exe_path="$cb_bin_dir\\$cb_exe"
readonly modules_path="$cb_dir\\runtime\\lib\\modules"
readonly cb_profiles_dir="$USERPROFILE\\Documents\\ControllerBuddy-Profiles"
readonly cb_shortcuts_dir="$APPDATA\\Microsoft\\Windows\\Start Menu\\Programs\\ControllerBuddy"
readonly saved_games_dir="$USERPROFILE\\Saved Games"
readonly dcs_stable_user_dir="$saved_games_dir\\DCS"
readonly dcs_open_beta_user_dir="$saved_games_dir\\DCS.openbeta"
;;
linux*)
readonly os=linux
readonly log_file="/tmp/InstallControllerBuddy.log"
readonly data_home_dir="${XDG_DATA_HOME:-$HOME/.local/share}"
readonly cb_parent_dir="$data_home_dir"
readonly cb_dir="$cb_parent_dir/ControllerBuddy"
readonly cb_bin_dir="$cb_dir/bin"
readonly cb_lib_dir="$cb_dir/lib"
readonly cb_exe=ControllerBuddy
readonly cb_exe_path="$cb_bin_dir/$cb_exe"
readonly modules_path="$cb_dir/lib/runtime/lib/modules"
if command -v xdg-user-dir >/dev/null 2>&1
then
cb_profiles_dir="$(xdg-user-dir DOCUMENTS)/ControllerBuddy-Profiles"
else
cb_profiles_dir="$HOME/ControllerBuddy-Profiles"
fi
readonly cb_profiles_dir
readonly cb_shortcuts_dir="$data_home_dir/applications/ControllerBuddy"
readonly udev_rules_file=/etc/udev/rules.d/60-controllerbuddy.rules
readonly module_conf_file=/etc/modules-load.d/controllerbuddy.conf
;;
*)
log 'Error: This script must either be run in a Git Bash for Windows or a GNU/Linux Bash environment.'
confirm_exit 1
;;
esac
if [ -z "$CB_INSTALL_SCRIPT_RESTARTED" ]
then
rm -f "$log_file"
fi
unset CB_INSTALL_SCRIPT_RESTARTED
log 'Determining system architecture...'
arch=$(uname -m)
check_retval 'Error: Failed to determine system architecture'
readonly arch
if ! { [ "$arch" = x86_64 ] || { [ "$os" = linux ] && [ "$arch" = aarch64 ]; }; }
then
log "Error: This script is not intended to be run on ${os^} $arch systems"
confirm_exit 1
fi
log 'Creating temporary directory...'
tmp_dir=$(mktemp -d -q)
check_retval 'Error: Failed to create temporary directory'
readonly tmp_dir
trap 'rm -rf "$tmp_dir"' EXIT
readonly cmd_argument="$1"
case "$cmd_argument" in
'')
;;
skip-self-update)
skip_self_update=true
;;
uninstall)
uninstall=true
;;
*)
log "Error: Invalid command-line argument '$cmd_argument'"
exit 1
;;
esac
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 [ "$os" = windows ]
then
remove_environment_variable CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS
remove_environment_variable CONTROLLER_BUDDY_EXECUTABLE
remove_environment_variable CONTROLLER_BUDDY_PROFILES_DIR
install_dcs_integration "$dcs_stable_user_dir"
install_dcs_integration "$dcs_open_beta_user_dir"
else
if [ -f "$udev_rules_file" ]
then
log 'Removing udev rules file...'
check_sudo_privileges
sudo rm -f "$udev_rules_file"
check_retval "Error: Failed to remove udev rules file '$udev_rules_file'"
fi
if [ -f "$module_conf_file" ]
then
log 'Removing module configuration file...'
check_sudo_privileges
sudo rm -f "$module_conf_file"
check_retval "Error: Failed to remove module configuration file '$module_conf_file'"
fi
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
fi
if [ "$os" != windows ]
then
log 'Checking if cURL is installed...'
if command -v curl >/dev/null 2>&1
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
if [ "$skip_self_update" != true ]
then
log 'Checking for the latest install script...'
if install_script_json=$(curl -fL 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" -fL "$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...'
mv "$tmp_install_script_file" "${BASH_SOURCE[0]}" &&
chmod +x "${BASH_SOURCE[0]}"
check_retval 'Error: Failed to update install script'
rm -rf "$tmp_dir"
CB_INSTALL_SCRIPT_RESTARTED=1 exec bash "${BASH_SOURCE[0]}" skip-self-update
fi
else
log 'Warning: Failed to obtain latest install script from GitHub'
echo
fi
fi
if [ "$os" = windows ]
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" -fL https://github.com/BrunnerInnovation/vJoy/releases/download/v2.2.2.0/vJoySetup_v2.2.2.0_Win10_Win11.exe &&
echo "ef569a3105cd301b89580f18f60c66b339e95296acf2c0dfcaf4b4bbf8ab68fe $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 "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
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 -NoProfile -Command "Start-Process '$vjoy_config_exe_path' '1 -f -a x y z rx ry rz sl0 sl1 -b 128 -p 0 -s 0 -e' -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 'Checking if Git is installed...'
if command -v git >/dev/null 2>&1
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
if ! uinput_accessible
then
pending_updates=()
if [ ! -e /dev/uinput ]
then
queue_file_update "$module_conf_file" uinput
fi
queue_file_update "$udev_rules_file" 'KERNEL=="uinput", SUBSYSTEM=="misc", TAG+="uaccess", OPTIONS+="static_node=uinput"'
if [ "${#pending_updates[@]}" -gt 0 ]
then
log 'Configuring access to /dev/uinput...'
check_sudo_privileges
sudo sh -c '
while [ "$#" -gt 0 ]
do
mkdir -p "$(dirname "$1")" &&
printf "%s\n" "$2" > "$1" || exit 1
shift 2
done
modprobe uinput &&
udevadm control --reload &&
udevadm trigger --action=add --subsystem-match=misc --sysname-match=uinput &&
udevadm settle
' sh "${pending_updates[@]}"
check_retval 'Error: Failed to configure access to /dev/uinput'
if ! uinput_accessible
then
reboot_required=true
fi
else
log 'Warning: Cannot access /dev/uinput. Please reboot your system and make sure you are logged in to a local session.'
echo
fi
fi
fi
check_cb_installed_version
if [ -n "$cb_installed_version" ]
then
cb_not_installed=false
auto_exit=true
else
cb_not_installed=true
fi
log 'Checking for the latest ControllerBuddy release...'
cb_json=$(curl -fL https://api.github.com/repos/bwRavencl/ControllerBuddy/releases/latest)
check_retval 'Error: Failed to obtain ControllerBuddy release information from GitHub' "$cb_not_installed"
if [ -z "$cb_json" ]
then
log 'Skipping ControllerBuddy update due to missing release information'
else
cb_latest_version=$(grep tag_name <<< "$cb_json" | cut -d : -f 2 | tr -d \",' ')
readonly cb_latest_version
if [ -z "$cb_latest_version" ]
then
log 'Error: Failed to determine latest ControllerBuddy version'
confirm_exit 1
fi
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 -vF .sig | cut -d : -f 2,3 | tr -d \",' ')
readonly archive_url
[ -n "$archive_url" ]
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" -fL "$archive_url"
check_retval "Error: Failed to obtain ControllerBuddy $cb_latest_version from GitHub"
verify_signature "$tmp_archive_file" "$archive_url.sig"
remove_controller_buddy
log 'Decompressing archive...'
if [ "$os" = windows ]
then
mkdir -p "$cb_parent_dir" && unzip -o -d "$cb_parent_dir" "$tmp_archive_file"
else
mkdir -p "$cb_parent_dir" && tar xzf "$tmp_archive_file" -C "$cb_parent_dir"
fi
check_retval 'Error: Failed to decompress archive'
fi
fi
create_shortcut ControllerBuddy "$cb_exe_path" '-autostart local -tray' "$cb_dir"
readonly 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 [ "$os" = windows ]
then
script_command="$script_path"
update_arguments=''
script_work_dir=%TMP%
else
script_command=/usr/bin/env
update_arguments="bash \"${script_path//%/%%}\""
script_work_dir=/tmp
fi
readonly script_command update_arguments script_work_dir
create_shortcut 'Update ControllerBuddy' "$script_command" "$update_arguments" "$script_work_dir"
create_shortcut 'Uninstall ControllerBuddy' "$script_command" "${update_arguments:+$update_arguments }uninstall" "$script_work_dir"
if [ "$os" = linux ]
then
if command -v xdg-desktop-menu >/dev/null 2>&1
then
xdg-desktop-menu forceupdate >/dev/null 2>&1
fi
for kbuildsycoca in kbuildsycoca6 kbuildsycoca5
do
if command -v "$kbuildsycoca" >/dev/null 2>&1
then
"$kbuildsycoca" >/dev/null 2>&1
break
fi
done
fi
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")
readonly cb_profiles_branch
if [ -d "$cb_profiles_dir/.git" ]
then
pull_profiles=true
if ! git -C "$cb_profiles_dir" diff --quiet || ! git -C "$cb_profiles_dir" diff --cached --quiet
then
log 'Warning: ControllerBuddy-Profiles repository has local modifications'
while true
do
read -rp 'Permanently discard all local modifications to allow pull? [y/N] ' response
case $response in
[Yy]*)
log 'Checking out ControllerBuddy-Profiles repository...'
git -C "$cb_profiles_dir" checkout -- .
check_retval 'Error: Failed to checkout ControllerBuddy-Profiles repository' false
break
;;
[Nn]* | '')
pull_profiles=false
log 'Skipping pull of ControllerBuddy-Profiles repository'
echo
break
;;
*)
log "Invalid input. Please answer with 'yes' or 'no'."
echo
;;
esac
done
fi
if [ "$pull_profiles" = true ]
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' false
fi
elif [ -d "$cb_profiles_dir" ]
then
log "Error: '$cb_profiles_dir' exists but is not a Git repository. Please move or delete it and restart this script."
confirm_exit 1
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 [ "$os" = windows ]
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)
if [ "$run_config_scripts" = true ]
then
export CONTROLLER_BUDDY_RUN_CONFIG_SCRIPTS=true
fi
fi
if [ "$run_config_scripts" != true ] && [ "$run_config_scripts" != false ]
then
echo The ControllerBuddy-Profiles configuration scripts can automatically adjust the input settings of the following applications for use with the official profiles:
find "$cb_profiles_dir/configs" -mindepth 2 -maxdepth 2 -name 'Configure.ps1' -printf '%h\n' | sed 's#.*/##' | tr _ ' ' | xargs -I name echo - name
echo
echo If you intend to use the official profiles, it is recommended to allow the scripts to apply the required changes.
echo Warning: To avoid losing your current input configurations, back them up before running the scripts.
echo "Note: Choosing 'always' will allow all scripts to run automatically whenever ControllerBuddy is updated, without further 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