-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1391 lines (1207 loc) · 42.3 KB
/
install.sh
File metadata and controls
executable file
·1391 lines (1207 loc) · 42.3 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
# SPDX-License-Identifier: AGPL-3.0-only
set -Eeuo pipefail
APP_NAME="FXRoute"
SERVICE_NAME="fxroute"
PROJECT_DIRNAME="fxroute"
DEFAULT_INSTALL_ROOT="$HOME/$PROJECT_DIRNAME"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="$SCRIPT_DIR"
INSTALL_ROOT="$DEFAULT_INSTALL_ROOT"
LOCAL_PROJECT_MODE=0
ASSUME_YES=0
VALIDATION_RESULTS=()
WARNINGS=()
EASYEFFECTS_MODE="missing"
EASYEFFECTS_SOCKET=""
PACKAGE_MANAGER=""
PACKAGE_INSTALL_CMD=()
SUDO_CMD=()
INSTALL_STATE_FILE="$HOME/.config/fxroute/install-state.json"
FXROUTE_BACKUP_DIR="$HOME/.config/fxroute/backups"
EASYEFFECTS_INSTALLED_BY_FXROUTE=0
EASYEFFECTS_INSTALL_METHOD=""
EASYEFFECTS_AUTOSTART_BACKED_UP=0
EASYEFFECTS_WATCHDOG_SERVICE_BACKED_UP=0
EASYEFFECTS_WATCHDOG_TIMER_BACKED_UP=0
MDNS_HOSTNAME=""
LAN_HOSTNAME_BEFORE=""
LAN_HOSTNAME_AFTER=""
LAN_HOSTNAME_CHANGED_BY_FXROUTE=0
AVAHI_WAS_PRESENT_BEFORE=0
AVAHI_WAS_ACTIVE_BEFORE=0
AVAHI_WAS_ENABLED_BEFORE=0
AVAHI_INSTALLED_BY_FXROUTE=0
AVAHI_ENABLED_BY_FXROUTE=0
CADDY_WAS_PRESENT_BEFORE=0
CADDY_SERVICE_WAS_ACTIVE_BEFORE=0
CADDY_INSTALLED_BY_FXROUTE=0
DEFAULT_CADDY_DISABLED_BY_FXROUTE=0
CADDY_PROXY_ENABLED=0
FIREWALLD_WAS_ACTIVE_BEFORE=0
HTTP_WAS_ALLOWED_BEFORE=0
MDNS_WAS_ALLOWED_BEFORE=0
HTTP_OPENED_BY_FXROUTE=0
MDNS_OPENED_BY_FXROUTE=0
usage() {
cat <<EOF
Usage: ./install.sh [options]
Options:
--target <dir> Install or refresh into this directory (default: $DEFAULT_INSTALL_ROOT)
--local-project Install in-place from the current project directory
--source <dir> Use a different local project source directory
-y, --yes Assume yes for package / Flatpak install prompts
-h, --help Show this help
Pass 1 is a pragmatic local installer. It installs dependencies, prepares the venv,
creates a user service, and can either sync the current project into ~/fxroute
or run directly from the local project tree.
EOF
}
log() { printf '[fxroute] %s\n' "$*"; }
warn() { printf '[fxroute][warn] %s\n' "$*" >&2; WARNINGS+=("$*"); }
die() { printf '[fxroute][error] %s\n' "$*" >&2; exit 1; }
pass() { printf '[pass] %s\n' "$*"; VALIDATION_RESULTS+=("PASS: $*"); }
fail() { printf '[fail] %s\n' "$*"; VALIDATION_RESULTS+=("FAIL: $*"); }
while [[ $# -gt 0 ]]; do
case "$1" in
--target)
[[ $# -ge 2 ]] || die "--target requires a directory"
INSTALL_ROOT="$2"
shift 2
;;
--source)
[[ $# -ge 2 ]] || die "--source requires a directory"
SOURCE_DIR="$2"
shift 2
;;
--local-project)
LOCAL_PROJECT_MODE=1
shift
;;
-y|--yes)
ASSUME_YES=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "Unknown argument: $1"
;;
esac
done
expand_path() {
python3 - <<'PY' "$1"
import os, sys
print(os.path.abspath(os.path.expanduser(sys.argv[1])))
PY
}
SOURCE_DIR="$(expand_path "$SOURCE_DIR")"
INSTALL_ROOT="$(expand_path "$INSTALL_ROOT")"
[[ -f "$SOURCE_DIR/main.py" && -f "$SOURCE_DIR/requirements.txt" && -f "$SOURCE_DIR/.env.example" ]] || die "Source directory does not look like the FXRoute project: $SOURCE_DIR"
if [[ $LOCAL_PROJECT_MODE -eq 1 ]]; then
INSTALL_ROOT="$SOURCE_DIR"
fi
if [[ "$INSTALL_ROOT" == "$SOURCE_DIR" ]]; then
LOCAL_PROJECT_MODE=1
fi
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Required command missing: $1"
}
bt_plugin_present() {
local candidates=(
/usr/lib64/spa-0.2/bluez5/libspa-bluez5.so
/usr/lib/spa-0.2/bluez5/libspa-bluez5.so
/usr/lib/x86_64-linux-gnu/spa-0.2/bluez5/libspa-bluez5.so
/usr/lib/aarch64-linux-gnu/spa-0.2/bluez5/libspa-bluez5.so
/usr/lib/arm-linux-gnueabihf/spa-0.2/bluez5/libspa-bluez5.so
)
local path
for path in "${candidates[@]}"; do
[[ -f "$path" ]] && return 0
done
shopt -s nullglob
local matches=(/usr/lib/*-linux-gnu/spa-0.2/bluez5/libspa-bluez5.so)
shopt -u nullglob
[[ ${#matches[@]} -gt 0 ]]
}
backup_user_file_once() {
local path="$1"
local backup_name="$2"
local backup_path="$FXROUTE_BACKUP_DIR/$backup_name"
[[ -e "$path" || -L "$path" ]] || return 1
[[ -e "$backup_path" || -L "$backup_path" ]] && return 0
mkdir -p "$FXROUTE_BACKUP_DIR"
cp -a "$path" "$backup_path"
return 0
}
valid_local_hostname() {
local value="${1,,}"
[[ "$value" =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]]
}
avahi_is_present() {
case "$PACKAGE_MANAGER" in
apt)
dpkg-query -W -f='${Status}' avahi-daemon 2>/dev/null | grep -q "install ok installed"
;;
dnf|zypper)
rpm -q avahi >/dev/null 2>&1
;;
*)
command -v avahi-daemon >/dev/null 2>&1 \
|| [[ -x /usr/sbin/avahi-daemon ]] \
|| [[ -x /usr/bin/avahi-daemon ]] \
|| systemctl list-unit-files avahi-daemon.service --no-legend 2>/dev/null | grep -q '^avahi-daemon\.service'
;;
esac
}
firewall_cmd_path() {
local path=""
path="$(command -v firewall-cmd 2>/dev/null || true)"
if [[ -z "$path" && -x /usr/bin/firewall-cmd ]]; then
path="/usr/bin/firewall-cmd"
fi
[[ -n "$path" ]] && printf '%s\n' "$path"
}
firewalld_is_active() {
local firewall_cmd=""
firewall_cmd="$(firewall_cmd_path)"
[[ -n "$firewall_cmd" ]] || return 1
"${SUDO_CMD[@]}" "$firewall_cmd" --state >/dev/null 2>&1
}
firewalld_query_service() {
local service="$1"
local firewall_cmd=""
firewall_cmd="$(firewall_cmd_path)"
[[ -n "$firewall_cmd" ]] || return 1
firewalld_is_active || return 1
"${SUDO_CMD[@]}" "$firewall_cmd" --query-service="$service" >/dev/null 2>&1
}
ensure_firewalld_service_open() {
local service="$1"
local purpose="$2"
local firewall_cmd=""
firewall_cmd="$(firewall_cmd_path)"
[[ -n "$firewall_cmd" ]] || return 0
firewalld_is_active || return 0
if "${SUDO_CMD[@]}" "$firewall_cmd" --query-service="$service" >/dev/null 2>&1; then
return 0
fi
log "$firewall_cmd --permanent --add-service=$service"
if ! "${SUDO_CMD[@]}" "$firewall_cmd" --permanent --add-service="$service"; then
warn "Optional LAN comfort could not open firewalld service '$service' for $purpose"
return 0
fi
log "$firewall_cmd --reload"
if ! "${SUDO_CMD[@]}" "$firewall_cmd" --reload; then
warn "Optional LAN comfort opened firewalld service '$service' permanently, but reload failed"
return 0
fi
case "$service" in
http) HTTP_OPENED_BY_FXROUTE=1 ;;
mdns) MDNS_OPENED_BY_FXROUTE=1 ;;
esac
pass "firewalld service opened ($service)"
}
choose_sudo() {
if [[ ${EUID:-$(id -u)} -eq 0 ]]; then
SUDO_CMD=()
elif command -v sudo >/dev/null 2>&1; then
SUDO_CMD=(sudo)
else
die "sudo is required for package installation"
fi
}
capture_lan_comfort_baseline() {
LAN_HOSTNAME_BEFORE="$(hostname 2>/dev/null || true)"
LAN_HOSTNAME_AFTER="$LAN_HOSTNAME_BEFORE"
if avahi_is_present; then
AVAHI_WAS_PRESENT_BEFORE=1
fi
if systemctl is-active avahi-daemon >/dev/null 2>&1; then
AVAHI_WAS_ACTIVE_BEFORE=1
fi
if systemctl is-enabled avahi-daemon >/dev/null 2>&1; then
AVAHI_WAS_ENABLED_BEFORE=1
fi
if command -v caddy >/dev/null 2>&1; then
CADDY_WAS_PRESENT_BEFORE=1
fi
if systemctl is-active caddy.service >/dev/null 2>&1; then
CADDY_SERVICE_WAS_ACTIVE_BEFORE=1
fi
if firewalld_is_active; then
FIREWALLD_WAS_ACTIVE_BEFORE=1
if firewalld_query_service http; then
HTTP_WAS_ALLOWED_BEFORE=1
fi
if firewalld_query_service mdns; then
MDNS_WAS_ALLOWED_BEFORE=1
fi
fi
}
confirm_supported_distro() {
if command -v apt-get >/dev/null 2>&1; then
PACKAGE_MANAGER="apt"
PACKAGE_INSTALL_CMD=("${SUDO_CMD[@]}" apt-get update)
elif command -v dnf >/dev/null 2>&1; then
PACKAGE_MANAGER="dnf"
elif command -v zypper >/dev/null 2>&1; then
PACKAGE_MANAGER="zypper"
else
die "Unsupported distro. Expected apt, dnf, or zypper."
fi
pass "distro detection: $PACKAGE_MANAGER"
}
run_cmd() {
log "$*"
"$@"
}
pkg_install() {
local packages=("$@")
case "$PACKAGE_MANAGER" in
apt)
run_cmd "${SUDO_CMD[@]}" apt-get update
run_cmd "${SUDO_CMD[@]}" apt-get install -y "${packages[@]}"
;;
dnf)
run_cmd "${SUDO_CMD[@]}" dnf install -y "${packages[@]}"
;;
zypper)
run_cmd "${SUDO_CMD[@]}" zypper --non-interactive install --no-recommends "${packages[@]}"
;;
esac
}
ensure_native_packages() {
local core_packages=()
local support_packages=(curl git socat)
local audio_stack_packages=()
local missing_packages=()
local missing_support=()
local missing_audio_stack=()
local need_venv_pkg=0
local need_bt_plugin_pkg=0
case "$PACKAGE_MANAGER" in
apt)
core_packages=(python3 python3-pip python3-venv mpv ffmpeg playerctl flatpak)
audio_stack_packages=(bluez wireplumber pipewire-bin pipewire-pulse pulseaudio-utils libspa-0.2-bluetooth)
;;
dnf)
core_packages=(python3 python3-pip mpv ffmpeg playerctl flatpak)
audio_stack_packages=(bluez wireplumber pipewire-utils pipewire-pulseaudio pulseaudio-utils)
;;
zypper)
core_packages=(python3 python3-pip mpv ffmpeg playerctl flatpak)
audio_stack_packages=(bluez wireplumber pipewire-tools pipewire-pulseaudio pulseaudio-utils pipewire-spa-plugins-0_2)
;;
esac
for cmd in python3 mpv ffmpeg playerctl flatpak; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_packages+=("$cmd")
fi
done
for cmd in "${support_packages[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_support+=("$cmd")
fi
done
for cmd in bluetoothctl wpctl pw-cli pactl; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing_audio_stack+=("$cmd")
fi
done
if ! bt_plugin_present; then
need_bt_plugin_pkg=1
fi
if ! python3 -m venv --help >/dev/null 2>&1; then
need_venv_pkg=1
fi
if [[ ${#missing_packages[@]} -eq 0 && ${#missing_support[@]} -eq 0 && ${#missing_audio_stack[@]} -eq 0 && $need_venv_pkg -eq 0 && $need_bt_plugin_pkg -eq 0 ]]; then
pass "native packages already available"
return
fi
if [[ ${#missing_packages[@]} -gt 0 ]]; then
pkg_install "${core_packages[@]}"
fi
if [[ ${#missing_support[@]} -gt 0 ]]; then
pkg_install "${support_packages[@]}"
fi
if [[ ${#missing_audio_stack[@]} -gt 0 || $need_bt_plugin_pkg -eq 1 ]]; then
pkg_install "${audio_stack_packages[@]}"
fi
if [[ $need_venv_pkg -eq 1 ]] && ! python3 -m venv --help >/dev/null 2>&1; then
case "$PACKAGE_MANAGER" in
dnf)
pkg_install python3-virtualenv
;;
zypper)
pkg_install python3-virtualenv
;;
*)
die "python3 venv support is missing after package install"
;;
esac
fi
for cmd in python3 mpv ffmpeg playerctl flatpak curl git socat bluetoothctl wpctl pw-cli pactl; do
command -v "$cmd" >/dev/null 2>&1 || die "Expected command missing after package install: $cmd"
done
if ! bt_plugin_present; then
warn "PipeWire BlueZ SPA plugin is still missing; Bluetooth input mode will not be available until the host provides libspa-bluez5.so"
fi
pass "native packages installed"
}
sync_project_tree() {
mkdir -p "$INSTALL_ROOT"
if [[ $LOCAL_PROJECT_MODE -eq 1 ]]; then
log "Using local project install mode at $INSTALL_ROOT"
pass "project install mode: local-project"
return
fi
log "Syncing project into $INSTALL_ROOT"
tar \
--exclude='.git' \
--exclude='.venv' \
--exclude='__pycache__' \
--exclude='backups' \
--exclude='*.pyc' \
--exclude='outputs/*.patch' \
-C "$SOURCE_DIR" -cf - . | tar -C "$INSTALL_ROOT" -xf -
pass "project synced to target directory"
}
pick_port() {
local preferred="$1"
python3 - <<'PY' "$preferred"
import socket, sys
start = int(sys.argv[1])
for port in range(start, start + 20):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(("127.0.0.1", port))
except OSError:
continue
print(port)
break
else:
print(start)
PY
}
read_env_value() {
local key="$1"
local env_file="${2:-$INSTALL_ROOT/.env}"
[[ -f "$env_file" ]] || return 0
awk -F= -v wanted="$key" '
$0 !~ /^[[:space:]]*#/ && $1 == wanted {
sub(/^[^=]*=/, "", $0)
print $0
exit
}
' "$env_file"
}
env_setting_enabled() {
local normalized="${1,,}"
case "$normalized" in
1|true|yes|on|enabled)
return 0
;;
*)
return 1
;;
esac
}
env_interval_hours_or_default() {
local raw_value="$1"
local default_value="$2"
if [[ "$raw_value" =~ ^[1-9][0-9]*$ ]]; then
printf '%s\n' "$raw_value"
else
printf '%s\n' "$default_value"
fi
}
create_env_if_missing() {
local env_file="$INSTALL_ROOT/.env"
local env_example="$INSTALL_ROOT/.env.example"
local music_root="$HOME/Music"
local downloads_dir="incoming"
local log_level="INFO"
local host="0.0.0.0"
local port="8000"
local max_downloads="1"
if [[ -f "$env_file" ]]; then
log "Keeping existing .env"
pass ".env preserved"
return
fi
[[ -f "$env_example" ]] || die "Missing .env.example in install root"
local chosen_port
chosen_port="$(pick_port "$port")"
if [[ "$chosen_port" != "$port" ]]; then
warn "Port $port is busy, defaulting .env to $chosen_port"
port="$chosen_port"
fi
cat > "$env_file" <<EOF
MUSIC_ROOT=$music_root
DOWNLOADS_SUBDIR=$downloads_dir
LOG_LEVEL=$log_level
HOST=$host
PORT=$port
MAX_DOWNLOADS=$max_downloads
SPOTIFY_AUTOSTART=off
SPOTIFY_CACHE_CLEANUP=off
SPOTIFY_CACHE_CLEANUP_INTERVAL_HOURS=24
SYSTEM_AUTO_UPDATE=off
SYSTEM_AUTO_UPDATE_INTERVAL_HOURS=24
EOF
mkdir -p "$music_root/$downloads_dir"
pass ".env created"
}
flatpak_app_installed() {
local app_id="$1"
command -v flatpak >/dev/null 2>&1 || return 1
if flatpak list --app --user --columns=application 2>/dev/null | grep -Fxq "$app_id"; then
return 0
fi
if flatpak list --app --system --columns=application 2>/dev/null | grep -Fxq "$app_id"; then
return 0
fi
return 1
}
detect_easyeffects_mode() {
local runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
local native_socket="$runtime_dir/EasyEffectsServer"
local flatpak_socket="$runtime_dir/.flatpak/com.github.wwmm.easyeffects/xdg-run/EasyEffectsServer"
local flatpak_tmp_socket="$runtime_dir/.flatpak/com.github.wwmm.easyeffects/tmp/EasyEffectsServer"
local native_available=0
local flatpak_available=0
command -v easyeffects >/dev/null 2>&1 && native_available=1 || true
if flatpak_app_installed "com.github.wwmm.easyeffects"; then
flatpak_available=1
fi
if [[ -S "$flatpak_socket" && ! -S "$native_socket" ]]; then
EASYEFFECTS_MODE="flatpak"
EASYEFFECTS_SOCKET="$flatpak_socket"
elif [[ -S "$flatpak_tmp_socket" && ! -S "$native_socket" ]]; then
EASYEFFECTS_MODE="flatpak"
EASYEFFECTS_SOCKET="$flatpak_tmp_socket"
elif [[ -S "$native_socket" ]]; then
EASYEFFECTS_MODE="native"
EASYEFFECTS_SOCKET="$native_socket"
elif [[ $native_available -eq 1 ]]; then
EASYEFFECTS_MODE="native"
EASYEFFECTS_SOCKET="$native_socket"
elif [[ $flatpak_available -eq 1 ]]; then
EASYEFFECTS_MODE="flatpak"
EASYEFFECTS_SOCKET="$flatpak_socket"
else
EASYEFFECTS_MODE="missing"
EASYEFFECTS_SOCKET="$flatpak_socket"
fi
log "EasyEffects mode detected: $EASYEFFECTS_MODE"
}
ensure_flathub_remote() {
if flatpak remote-list --columns=name --user | grep -qx 'flathub'; then
return
fi
if flatpak remote-list --columns=name | grep -qx 'flathub'; then
pass "Flathub remote detected (system), adding user remote for user-scoped install"
fi
run_cmd flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
}
ensure_easyeffects() {
detect_easyeffects_mode
if [[ "$EASYEFFECTS_MODE" != "missing" ]]; then
EASYEFFECTS_INSTALLED_BY_FXROUTE=0
EASYEFFECTS_INSTALL_METHOD="$EASYEFFECTS_MODE"
pass "EasyEffects detected ($EASYEFFECTS_MODE)"
return
fi
ensure_flathub_remote
if [[ $ASSUME_YES -eq 1 ]]; then
run_cmd flatpak install --user -y flathub com.github.wwmm.easyeffects
else
run_cmd flatpak install --user flathub com.github.wwmm.easyeffects
fi
detect_easyeffects_mode
EASYEFFECTS_INSTALLED_BY_FXROUTE=1
EASYEFFECTS_INSTALL_METHOD="flatpak"
[[ "$EASYEFFECTS_MODE" == "flatpak" ]] || warn "EasyEffects install finished but mode is $EASYEFFECTS_MODE"
pass "EasyEffects installed or refreshed via Flatpak"
}
setup_python_env() {
local venv_dir="$INSTALL_ROOT/.venv"
run_cmd python3 -m venv "$venv_dir"
run_cmd "$venv_dir/bin/python3" -m pip install --upgrade pip setuptools wheel
run_cmd "$venv_dir/bin/pip" install -r "$INSTALL_ROOT/requirements.txt"
pass "Python venv created"
pass "pip install -r requirements.txt"
}
write_service_unit() {
local service_dir="$HOME/.config/systemd/user"
mkdir -p "$service_dir"
cat > "$service_dir/$SERVICE_NAME.service" <<EOF
[Unit]
Description=FXRoute
After=default.target
[Service]
Type=simple
WorkingDirectory=$INSTALL_ROOT
EnvironmentFile=$INSTALL_ROOT/.env
ExecStart=$INSTALL_ROOT/.venv/bin/python3 $INSTALL_ROOT/main.py
Restart=on-failure
RestartSec=10
[Install]
WantedBy=default.target
EOF
if systemctl --user daemon-reload; then
if systemctl --user enable --now "$SERVICE_NAME"; then
pass "systemd user service enabled"
else
fail "systemd user service enable/start"
warn "systemctl --user enable --now $SERVICE_NAME failed, likely because no active user bus is available in this shell"
fi
else
fail "systemd user daemon-reload"
fi
}
install_watchdog_if_needed() {
local user_systemd_dir="$HOME/.config/systemd/user"
local watchdog_timer_src="$INSTALL_ROOT/systemd-user/easyeffects-stale-watchdog.timer"
local watchdog_script="$INSTALL_ROOT/scripts/easyeffects-stale-watchdog.sh"
local watchdog_service_path="$user_systemd_dir/easyeffects-stale-watchdog.service"
local watchdog_timer_path="$user_systemd_dir/easyeffects-stale-watchdog.timer"
[[ -f "$watchdog_timer_src" && -f "$watchdog_script" ]] || return
chmod +x "$watchdog_script"
if [[ "$EASYEFFECTS_MODE" == "flatpak" ]]; then
mkdir -p "$user_systemd_dir"
if backup_user_file_once "$watchdog_service_path" "easyeffects-stale-watchdog.service.pre-fxroute"; then
EASYEFFECTS_WATCHDOG_SERVICE_BACKED_UP=1
fi
if backup_user_file_once "$watchdog_timer_path" "easyeffects-stale-watchdog.timer.pre-fxroute"; then
EASYEFFECTS_WATCHDOG_TIMER_BACKED_UP=1
fi
cat > "$watchdog_service_path" <<EOF
[Unit]
Description=Recover EasyEffects from a stale Flatpak runtime socket
After=default.target
[Service]
Type=oneshot
ExecStart=$watchdog_script
EOF
cp "$watchdog_timer_src" "$watchdog_timer_path"
if systemctl --user daemon-reload && systemctl --user enable --now easyeffects-stale-watchdog.timer; then
pass "Flatpak EasyEffects watchdog timer enabled"
else
warn "Flatpak EasyEffects watchdog files were installed, but the timer could not be enabled in this shell"
fi
else
warn "EasyEffects watchdog parity is only wired for Flatpak mode in pass 1"
systemctl --user disable --now easyeffects-stale-watchdog.timer >/dev/null 2>&1 || true
rm -f "$user_systemd_dir/easyeffects-stale-watchdog.service" "$user_systemd_dir/easyeffects-stale-watchdog.timer"
fi
}
disable_legacy_samplerate_override() {
systemctl --user stop switch-sample-rate.service >/dev/null 2>&1 || true
systemctl --user disable switch-sample-rate.service >/dev/null 2>&1 || true
rm -f "$HOME/.config/systemd/user/switch-sample-rate.service" "$HOME/switch-sample-rate.sh"
systemctl --user daemon-reload >/dev/null 2>&1 || true
}
configure_pipewire_samplerates_if_available() {
local script="$INSTALL_ROOT/scripts/configure-pipewire-samplerates.sh"
[[ -f "$script" ]] || return
chmod +x "$script"
disable_legacy_samplerate_override
if "$script" apply; then
pass "PipeWire samplerate allowed-rates configured"
else
fail "PipeWire samplerate allowed-rates configured"
warn "PipeWire samplerate setup could not be applied automatically in this shell"
fi
}
ensure_bootstrap_easyeffects_presets() {
local bootstrap_dir="$INSTALL_ROOT/assets/easyeffects-bootstrap"
local home_output_dir="$HOME/.var/app/com.github.wwmm.easyeffects/data/easyeffects/output"
[[ -f "$bootstrap_dir/Direct.json" && -f "$bootstrap_dir/Neutral.json" ]] || return
mkdir -p "$home_output_dir"
[[ -f "$home_output_dir/Direct.json" ]] || cp "$bootstrap_dir/Direct.json" "$home_output_dir/Direct.json"
[[ -f "$home_output_dir/Neutral.json" ]] || cp "$bootstrap_dir/Neutral.json" "$home_output_dir/Neutral.json"
pass "EasyEffects bootstrap presets ensured"
}
setup_easyeffects_autostart() {
local autostart_dir="$HOME/.config/autostart"
local desktop_file="$autostart_dir/easyeffects.desktop"
local exec_cmd="flatpak run com.github.wwmm.easyeffects --gapplication-service"
mkdir -p "$autostart_dir"
if [[ "$EASYEFFECTS_MODE" == "native" ]]; then
exec_cmd="easyeffects --gapplication-service"
fi
if backup_user_file_once "$desktop_file" "easyeffects.desktop.pre-fxroute"; then
EASYEFFECTS_AUTOSTART_BACKED_UP=1
fi
cat > "$desktop_file" <<EOF
[Desktop Entry]
Type=Application
Exec=$exec_cmd
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=EasyEffects Service
Comment=Start EasyEffects in background for FXRoute
EOF
pass "EasyEffects autostart configured ($EASYEFFECTS_MODE)"
}
detect_spotify_autostart_command() {
if flatpak_app_installed "com.spotify.Client"; then
printf 'flatpak run com.spotify.Client\n'
return 0
fi
if command -v spotify >/dev/null 2>&1; then
printf 'spotify\n'
return 0
fi
return 1
}
setup_spotify_autostart() {
local env_file="$INSTALL_ROOT/.env"
local enabled_value="$(read_env_value SPOTIFY_AUTOSTART "$env_file")"
local autostart_dir="$HOME/.config/autostart"
local desktop_file="$autostart_dir/fxroute-spotify.desktop"
local legacy_desktop_file="$autostart_dir/spotify-clean-start.desktop"
local script_path="$INSTALL_ROOT/scripts/spotify-autostart.sh"
mkdir -p "$autostart_dir"
if ! env_setting_enabled "$enabled_value"; then
rm -f "$desktop_file"
pass "Spotify autostart disabled"
return
fi
if ! detect_spotify_autostart_command >/dev/null; then
rm -f "$desktop_file"
warn "Spotify autostart is enabled in .env, but no local Spotify desktop app (Flatpak or native) was found"
return
fi
[[ -f "$script_path" ]] || {
warn "Spotify autostart could not be enabled because $script_path is missing"
return
}
chmod +x "$script_path"
if [[ -f "$legacy_desktop_file" ]] && grep -q "spotify-clean-start.sh" "$legacy_desktop_file"; then
rm -f "$legacy_desktop_file"
pass "legacy Spotify clean-start autostart replaced"
fi
cat > "$desktop_file" <<EOF
[Desktop Entry]
Type=Application
Exec=$script_path
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Spotify
Comment=Start Spotify automatically for FXRoute
EOF
pass "Spotify autostart configured"
}
write_install_state() {
local state_file="$INSTALL_STATE_FILE"
LAN_HOSTNAME_AFTER="${LAN_HOSTNAME_AFTER:-$(hostname 2>/dev/null || true)}"
mkdir -p "$(dirname "$state_file")"
cat > "$state_file" <<EOF
{
"easyeffects": {
"installed_by_fxroute": $( [[ $EASYEFFECTS_INSTALLED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"install_method": "${EASYEFFECTS_INSTALL_METHOD:-$EASYEFFECTS_MODE}",
"detected_mode": "$EASYEFFECTS_MODE",
"autostart_backed_up": $( [[ $EASYEFFECTS_AUTOSTART_BACKED_UP -eq 1 ]] && echo true || echo false ),
"watchdog_service_backed_up": $( [[ $EASYEFFECTS_WATCHDOG_SERVICE_BACKED_UP -eq 1 ]] && echo true || echo false ),
"watchdog_timer_backed_up": $( [[ $EASYEFFECTS_WATCHDOG_TIMER_BACKED_UP -eq 1 ]] && echo true || echo false )
},
"lan_comfort": {
"hostname_before": "$LAN_HOSTNAME_BEFORE",
"hostname_after": "$LAN_HOSTNAME_AFTER",
"hostname_changed_by_fxroute": $( [[ $LAN_HOSTNAME_CHANGED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"avahi_was_present_before": $( [[ $AVAHI_WAS_PRESENT_BEFORE -eq 1 ]] && echo true || echo false ),
"avahi_was_active_before": $( [[ $AVAHI_WAS_ACTIVE_BEFORE -eq 1 ]] && echo true || echo false ),
"avahi_was_enabled_before": $( [[ $AVAHI_WAS_ENABLED_BEFORE -eq 1 ]] && echo true || echo false ),
"avahi_installed_by_fxroute": $( [[ $AVAHI_INSTALLED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"avahi_enabled_by_fxroute": $( [[ $AVAHI_ENABLED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"caddy_was_present_before": $( [[ $CADDY_WAS_PRESENT_BEFORE -eq 1 ]] && echo true || echo false ),
"caddy_service_was_active_before": $( [[ $CADDY_SERVICE_WAS_ACTIVE_BEFORE -eq 1 ]] && echo true || echo false ),
"caddy_installed_by_fxroute": $( [[ $CADDY_INSTALLED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"default_caddy_disabled_by_fxroute": $( [[ $DEFAULT_CADDY_DISABLED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"caddy_proxy_enabled": $( [[ $CADDY_PROXY_ENABLED -eq 1 ]] && echo true || echo false ),
"firewalld_was_active_before": $( [[ $FIREWALLD_WAS_ACTIVE_BEFORE -eq 1 ]] && echo true || echo false ),
"http_was_allowed_before": $( [[ $HTTP_WAS_ALLOWED_BEFORE -eq 1 ]] && echo true || echo false ),
"mdns_was_allowed_before": $( [[ $MDNS_WAS_ALLOWED_BEFORE -eq 1 ]] && echo true || echo false ),
"http_opened_by_fxroute": $( [[ $HTTP_OPENED_BY_FXROUTE -eq 1 ]] && echo true || echo false ),
"mdns_opened_by_fxroute": $( [[ $MDNS_OPENED_BY_FXROUTE -eq 1 ]] && echo true || echo false )
}
}
EOF
pass "install state recorded"
}
install_helpers() {
local bin_dir="$HOME/.local/bin"
mkdir -p "$bin_dir"
cat > "$bin_dir/fxroute-status" <<EOF
#!/usr/bin/env bash
exec systemctl --user status $SERVICE_NAME
EOF
cat > "$bin_dir/fxroute-logs" <<EOF
#!/usr/bin/env bash
exec journalctl --user -u $SERVICE_NAME -f
EOF
cat > "$bin_dir/fxroute-restart" <<EOF
#!/usr/bin/env bash
exec systemctl --user restart $SERVICE_NAME
EOF
cat > "$bin_dir/fxroute-update" <<EOF
#!/usr/bin/env bash
set -euo pipefail
exec "$INSTALL_ROOT/install.sh" --local-project
EOF
cat > "$bin_dir/fxroute-update-ytdlp" <<EOF
#!/usr/bin/env bash
set -euo pipefail
exec "$INSTALL_ROOT/.venv/bin/pip" install -U yt-dlp
EOF
chmod +x "$bin_dir"/fxroute-*
pass "helper commands installed in $bin_dir"
}
configure_spotify_cache_cleanup_helper() {
local env_file="$INSTALL_ROOT/.env"
local user_systemd_dir="$HOME/.config/systemd/user"
local service_name="fxroute-spotify-cache-cleanup.service"
local timer_name="fxroute-spotify-cache-cleanup.timer"
local script_path="$INSTALL_ROOT/scripts/spotify-cache-cleanup.sh"
local enabled_value="$(read_env_value SPOTIFY_CACHE_CLEANUP "$env_file")"
local interval_value="$(read_env_value SPOTIFY_CACHE_CLEANUP_INTERVAL_HOURS "$env_file")"
local interval_hours="$(env_interval_hours_or_default "$interval_value" 24)"
mkdir -p "$user_systemd_dir"
if ! env_setting_enabled "$enabled_value"; then
systemctl --user disable --now "$timer_name" >/dev/null 2>&1 || true
rm -f "$user_systemd_dir/$service_name" "$user_systemd_dir/$timer_name"
systemctl --user daemon-reload >/dev/null 2>&1 || true
pass "Spotify cache cleanup helper disabled"
return
fi
[[ -f "$script_path" ]] || {
warn "Spotify cache cleanup helper could not be enabled because $script_path is missing"
return
}
chmod +x "$script_path"
cat > "$user_systemd_dir/$service_name" <<EOF
[Unit]
Description=FXRoute Spotify cache cleanup
After=default.target
[Service]
Type=oneshot
ExecStart=$script_path
EOF
cat > "$user_systemd_dir/$timer_name" <<EOF
[Unit]
Description=Run FXRoute Spotify cache cleanup periodically
[Timer]
OnBootSec=20min
OnUnitActiveSec=${interval_hours}h
Persistent=true
[Install]
WantedBy=timers.target
EOF
if systemctl --user daemon-reload && systemctl --user enable --now "$timer_name"; then
pass "Spotify cache cleanup helper enabled (${interval_hours}h)"
else
warn "Spotify cache cleanup helper files were installed, but the timer could not be enabled in this shell"
fi
}
configure_system_auto_update_helper() {
local env_file="$INSTALL_ROOT/.env"
local service_name="fxroute-system-update.service"
local timer_name="fxroute-system-update.timer"
local service_path="/etc/systemd/system/$service_name"
local timer_path="/etc/systemd/system/$timer_name"
local script_path="$INSTALL_ROOT/scripts/system-package-update.sh"
local enabled_value="$(read_env_value SYSTEM_AUTO_UPDATE "$env_file")"
local interval_value="$(read_env_value SYSTEM_AUTO_UPDATE_INTERVAL_HOURS "$env_file")"
local interval_hours="$(env_interval_hours_or_default "$interval_value" 24)"
local tmp_service=""
local tmp_timer=""
if ! env_setting_enabled "$enabled_value"; then
"${SUDO_CMD[@]}" systemctl disable --now "$timer_name" >/dev/null 2>&1 || true
"${SUDO_CMD[@]}" rm -f "$service_path" "$timer_path" >/dev/null 2>&1 || true
"${SUDO_CMD[@]}" systemctl daemon-reload >/dev/null 2>&1 || true
pass "Optional system auto-update helper disabled"
return
fi
[[ -f "$script_path" ]] || {
warn "System auto-update helper could not be enabled because $script_path is missing"
return
}
chmod +x "$script_path"
tmp_service="$(mktemp)"
tmp_timer="$(mktemp)"
cat > "$tmp_service" <<EOF
[Unit]
Description=FXRoute optional system package update helper
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=$script_path
EOF
cat > "$tmp_timer" <<EOF
[Unit]
Description=Run FXRoute optional system package updates periodically
[Timer]
OnBootSec=30min
OnUnitActiveSec=${interval_hours}h
Persistent=true
[Install]
WantedBy=timers.target
EOF
if ! "${SUDO_CMD[@]}" install -m 644 "$tmp_service" "$service_path"; then
warn "Failed to install optional system auto-update service"
rm -f "$tmp_service" "$tmp_timer"
return
fi