forked from openhop-dev/openhop_repeater
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.sh
More file actions
executable file
·741 lines (639 loc) · 26.5 KB
/
Copy pathmanage.sh
File metadata and controls
executable file
·741 lines (639 loc) · 26.5 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
#!/bin/bash
# pyMC Repeater Management Script - Deploy, Upgrade, Uninstall
set -e
INSTALL_DIR="/opt/pymc_repeater"
CONFIG_DIR="/etc/pymc_repeater"
LOG_DIR="/var/log/pymc_repeater"
SERVICE_USER="repeater"
SERVICE_NAME="pymc-repeater"
# Check if we're running in an interactive terminal
if [ ! -t 0 ] || [ -z "$TERM" ]; then
echo "Error: This script requires an interactive terminal."
echo "Please run from SSH or a local terminal, not via file manager."
exit 1
fi
# Check if whiptail is available, fallback to dialog
if command -v whiptail &> /dev/null; then
DIALOG="whiptail"
elif command -v dialog &> /dev/null; then
DIALOG="dialog"
else
echo "TUI interface requires whiptail or dialog."
if [ "$EUID" -eq 0 ]; then
echo "Installing whiptail..."
apt-get update -qq && apt-get install -y whiptail
DIALOG="whiptail"
else
echo ""
echo "Please install whiptail: sudo apt-get install -y whiptail"
echo "Then run this script again."
exit 1
fi
fi
# Function to show info box
show_info() {
$DIALOG --backtitle "pyMC Repeater Management" --title "$1" --msgbox "$2" 12 70
}
# Function to show error box
show_error() {
$DIALOG --backtitle "pyMC Repeater Management" --title "Error" --msgbox "$1" 8 60
}
# Function to ask yes/no question
ask_yes_no() {
$DIALOG --backtitle "pyMC Repeater Management" --title "$1" --yesno "$2" 10 70
}
# Function to show progress
show_progress() {
echo "$2" | $DIALOG --backtitle "pyMC Repeater Management" --title "$1" --gauge "$3" 8 70 0
}
# Function to check if service exists
service_exists() {
systemctl list-unit-files | grep -q "^$SERVICE_NAME.service"
}
# Function to check if service is installed
is_installed() {
[ -d "$INSTALL_DIR" ] && service_exists
}
# Function to check if service is running
is_running() {
systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1
}
# Function to get current version
get_version() {
if [ -f "$INSTALL_DIR/pyproject.toml" ]; then
grep "^version" "$INSTALL_DIR/pyproject.toml" | cut -d'"' -f2 2>/dev/null || echo "unknown"
else
echo "not installed"
fi
}
# Function to get service status for display
get_status_display() {
if ! is_installed; then
echo "Not Installed"
elif is_running; then
echo "Running ($(get_version))"
else
echo "Installed but Stopped ($(get_version))"
fi
}
# Main menu
show_main_menu() {
local status=$(get_status_display)
CHOICE=$($DIALOG --backtitle "pyMC Repeater Management" --title "pyMC Repeater Management" --menu "\nCurrent Status: $status\n\nChoose an action:" 18 70 9 \
"install" "Install pyMC Repeater" \
"upgrade" "Upgrade existing installation" \
"uninstall" "Remove pyMC Repeater completely" \
"config" "Configure radio settings" \
"start" "Start the service" \
"stop" "Stop the service" \
"restart" "Restart the service" \
"logs" "View live logs" \
"status" "Show detailed status" \
"exit" "Exit" 3>&1 1>&2 2>&3)
case $CHOICE in
"install")
if is_installed; then
show_error "pyMC Repeater is already installed!\n\nUse 'upgrade' to update or 'uninstall' first."
else
install_repeater
fi
;;
"upgrade")
if is_installed; then
upgrade_repeater
else
show_error "pyMC Repeater is not installed!\n\nUse 'install' first."
fi
;;
"uninstall")
if is_installed; then
uninstall_repeater
else
show_error "pyMC Repeater is not installed."
fi
;;
"config")
configure_radio
;;
"start")
manage_service "start"
;;
"stop")
manage_service "stop"
;;
"restart")
manage_service "restart"
;;
"logs")
clear
echo "=== Live Logs (Press Ctrl+C to return) ==="
echo ""
journalctl -u "$SERVICE_NAME" -f
;;
"status")
show_detailed_status
;;
"exit"|"")
exit 0
;;
esac
}
# Install function
install_repeater() {
# Check root
if [ "$EUID" -ne 0 ]; then
show_error "Installation requires root privileges.\n\nPlease run: sudo $0"
return
fi
# Welcome screen
$DIALOG --backtitle "pyMC Repeater Management" --title "Welcome" --msgbox "\nWelcome to pyMC Repeater Setup\n\nThis installer will configure your Raspberry Pi as a LoRa mesh network repeater.\n\nPress OK to continue..." 12 70
# SPI Check
CONFIG_FILE=""
if [ -f "/boot/firmware/config.txt" ]; then
CONFIG_FILE="/boot/firmware/config.txt"
elif [ -f "/boot/config.txt" ]; then
CONFIG_FILE="/boot/config.txt"
fi
if [ -n "$CONFIG_FILE" ] && ! grep -q "dtparam=spi=on" "$CONFIG_FILE" 2>/dev/null && ! grep -q "spi_bcm2835" /proc/modules 2>/dev/null; then
if ask_yes_no "SPI Not Enabled" "\nSPI interface is required but not enabled!\n\nWould you like to enable it now?\n(This will require a reboot)"; then
echo "dtparam=spi=on" >> "$CONFIG_FILE"
show_info "SPI Enabled" "\nSPI has been enabled in $CONFIG_FILE\n\nSystem will reboot now. Please run this script again after reboot."
reboot
else
show_error "SPI is required for LoRa radio operation.\n\nPlease enable SPI manually and run this script again."
return
fi
elif [ -z "$CONFIG_FILE" ]; then
show_error "Could not find config.txt file.\n\nPlease enable SPI manually:\nsudo raspi-config -> Interfacing Options -> SPI -> Enable"
return
fi
# Installation progress
(
echo "0"; echo "# Creating service user..."
if ! id "$SERVICE_USER" &>/dev/null; then
useradd --system --home /var/lib/pymc_repeater --shell /sbin/nologin "$SERVICE_USER"
fi
echo "10"; echo "# Adding user to hardware groups..."
usermod -a -G gpio,i2c,spi "$SERVICE_USER" 2>/dev/null || true
usermod -a -G dialout "$SERVICE_USER" 2>/dev/null || true
echo "20"; echo "# Creating directories..."
mkdir -p "$INSTALL_DIR" "$CONFIG_DIR" "$LOG_DIR" /var/lib/pymc_repeater
echo "25"; echo "# Installing system dependencies..."
apt-get update -qq
apt-get install -y libffi-dev jq python3-pip python3-rrdtool wget swig build-essential python3-dev
# Install mikefarah yq v4 if not already installed
if ! command -v yq &> /dev/null || [[ "$(yq --version 2>&1)" != *"mikefarah/yq"* ]]; then
YQ_VERSION="v4.40.5"
YQ_BINARY="yq_linux_arm64"
if [[ "$(uname -m)" == "x86_64" ]]; then
YQ_BINARY="yq_linux_amd64"
elif [[ "$(uname -m)" == "armv7"* ]]; then
YQ_BINARY="yq_linux_arm"
fi
wget -qO /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/${YQ_BINARY}" && chmod +x /usr/local/bin/yq
fi
echo "30"; echo "# Installing files..."
cp -r repeater "$INSTALL_DIR/"
cp pyproject.toml "$INSTALL_DIR/"
cp README.md "$INSTALL_DIR/"
cp setup-radio-config.sh "$INSTALL_DIR/" 2>/dev/null || true
cp radio-settings.json "$INSTALL_DIR/" 2>/dev/null || true
echo "45"; echo "# Installing configuration..."
cp config.yaml.example "$CONFIG_DIR/config.yaml.example"
if [ ! -f "$CONFIG_DIR/config.yaml" ]; then
cp config.yaml.example "$CONFIG_DIR/config.yaml"
fi
echo "55"; echo "# Installing systemd service..."
cp pymc-repeater.service /etc/systemd/system/
systemctl daemon-reload
echo "65"; echo "# Setting permissions..."
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR" "$CONFIG_DIR" "$LOG_DIR" /var/lib/pymc_repeater
chmod 750 "$CONFIG_DIR" "$LOG_DIR" /var/lib/pymc_repeater
# Ensure the service user can create subdirectories in their home directory
chmod 755 /var/lib/pymc_repeater
# Pre-create the .config directory that the service will need
mkdir -p /var/lib/pymc_repeater/.config/pymc_repeater
chown -R "$SERVICE_USER:$SERVICE_USER" /var/lib/pymc_repeater/.config
echo "75"; echo "# Starting service..."
systemctl enable "$SERVICE_NAME"
echo "90"; echo "# Installation files complete..."
) | $DIALOG --backtitle "pyMC Repeater Management" --title "Installing" --gauge "Setting up pyMC Repeater..." 8 70 0
# Install Python package outside of progress gauge for better error handling
clear
echo "=== Installing Python Dependencies ==="
echo ""
echo "Installing pymc_repeater and dependencies (including pymc_core from GitHub)..."
echo "This may take a few minutes..."
echo ""
SCRIPT_DIR="$(dirname "$0")"
cd "$SCRIPT_DIR"
if python3 -m pip install --break-system-packages --force-reinstall --no-cache-dir --ignore-installed .; then
echo ""
echo "✓ Python package installation completed successfully!"
# Reload systemd and start the service
systemctl daemon-reload
systemctl start "$SERVICE_NAME"
else
echo ""
echo "✗ Python package installation failed!"
echo "Please check the error messages above and try again."
read -p "Press Enter to continue..." || true
fi
# Radio configuration
echo ""
echo "=== Radio Configuration ==="
# Run radio configuration
SCRIPT_DIR="$(dirname "$0")"
RADIO_SCRIPT="$SCRIPT_DIR/setup-radio-config.sh"
if [ -f "$RADIO_SCRIPT" ]; then
clear
echo "=== pyMC Repeater Radio Configuration ==="
echo ""
if bash "$RADIO_SCRIPT" "$CONFIG_DIR"; then
echo ""
echo "=== Radio Configuration Complete ==="
echo "Restarting service with new configuration..."
systemctl restart "$SERVICE_NAME" 2>/dev/null || true
sleep 2
else
echo "⚠ Radio configuration failed, but installation is complete."
echo "You can run radio configuration later from the main menu."
fi
else
echo "⚠ Radio configuration script not found at $RADIO_SCRIPT"
echo "Installation complete, but you'll need to configure radio settings manually."
fi
# Show final results
sleep 2
local ip_address=$(hostname -I | awk '{print $1}')
if is_running; then
local msg="\nInstallation and configuration completed successfully!\n\n✓ Service is running\n✓ Radio configured\n\nWeb Dashboard: http://$ip_address:8000\n\nView logs: Select 'logs' from main menu"
show_info "Installation Complete" "$msg"
else
show_error "Installation completed but service failed to start!\n\nCheck logs from the main menu for details."
fi
}
# Upgrade function
upgrade_repeater() {
if [ "$EUID" -ne 0 ]; then
show_error "Upgrade requires root privileges.\n\nPlease run: sudo $0"
return
fi
local current_version=$(get_version)
if ask_yes_no "Confirm Upgrade" "Current version: $current_version\n\nThis will upgrade pyMC Repeater while preserving your configuration.\n\nContinue?"; then
# Show info that upgrade is starting
show_info "Upgrading" "Starting upgrade process...\n\nThis may take a few minutes.\nProgress will be shown in the terminal."
echo "=== Upgrade Progress ==="
echo "[1/9] Stopping service..."
systemctl stop "$SERVICE_NAME" 2>/dev/null || true
echo "[2/9] Backing up configuration..."
if [ -d "$CONFIG_DIR" ]; then
cp -r "$CONFIG_DIR" "$CONFIG_DIR.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
echo " ✓ Configuration backed up"
fi
echo "[3/9] Updating system dependencies..."
apt-get update -qq
apt-get install -y libffi-dev jq python3-pip python3-rrdtool wget swig build-essential python3-dev
# Install mikefarah yq v4 if not already installed
if ! command -v yq &> /dev/null || [[ "$(yq --version 2>&1)" != *"mikefarah/yq"* ]]; then
YQ_VERSION="v4.40.5"
YQ_BINARY="yq_linux_arm64"
if [[ "$(uname -m)" == "x86_64" ]]; then
YQ_BINARY="yq_linux_amd64"
elif [[ "$(uname -m)" == "armv7"* ]]; then
YQ_BINARY="yq_linux_arm"
fi
wget -qO /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/${YQ_BINARY}" && chmod +x /usr/local/bin/yq
fi
echo " ✓ Dependencies updated"
echo "[4/9] Installing new files..."
cp -r repeater "$INSTALL_DIR/" 2>/dev/null || true
cp pyproject.toml "$INSTALL_DIR/" 2>/dev/null || true
cp README.md "$INSTALL_DIR/" 2>/dev/null || true
cp pymc-repeater.service /etc/systemd/system/ 2>/dev/null || true
echo " ✓ Files updated"
echo "[5/9] Validating and updating configuration..."
if validate_and_update_config; then
echo " ✓ Configuration validated and updated"
else
echo " ⚠ Configuration validation failed, keeping existing config"
fi
echo "[6/9] Fixing permissions..."
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR" "$CONFIG_DIR" "$LOG_DIR" /var/lib/pymc_repeater 2>/dev/null || true
chmod 750 "$CONFIG_DIR" "$LOG_DIR" 2>/dev/null || true
chmod 755 /var/lib/pymc_repeater 2>/dev/null || true
# Pre-create the .config directory that the service will need
mkdir -p /var/lib/pymc_repeater/.config/pymc_repeater 2>/dev/null || true
chown -R "$SERVICE_USER:$SERVICE_USER" /var/lib/pymc_repeater/.config 2>/dev/null || true
echo " ✓ Permissions updated"
echo "[7/9] Reloading systemd..."
systemctl daemon-reload
echo " ✓ Systemd reloaded"
echo "=== Installing Python Dependencies ==="
echo ""
echo "Updating pymc_repeater and dependencies (including pymc_core from GitHub)..."
echo "This may take a few minutes..."
echo ""
# Install from source directory to properly resolve Git dependencies
SCRIPT_DIR="$(dirname "$0")"
cd "$SCRIPT_DIR"
if python3 -m pip install --break-system-packages --force-reinstall --no-cache-dir --ignore-installed .; then
echo ""
echo "✓ Python package update completed successfully!"
else
echo ""
echo "⚠ Python package update failed, but continuing..."
fi
echo "[8/9] Starting service..."
systemctl daemon-reload
systemctl start "$SERVICE_NAME"
echo " ✓ Service started"
echo "[9/9] Verifying installation..."
sleep 3 # Give service time to start
local new_version=$(get_version)
if is_running; then
echo " ✓ Service is running"
show_info "Upgrade Complete" "Upgrade completed successfully!\n\nVersion: $current_version → $new_version\n\n✓ Service is running\n✓ Configuration preserved"
else
echo " ✗ Service failed to start"
show_error "Upgrade completed but service failed to start!\n\nVersion updated: $current_version → $new_version\n\nCheck logs from the main menu for details."
fi
echo "=== Upgrade Complete ==="
fi
}
# Radio Configuration function
configure_radio() {
# Check if config exists
if [ ! -f "$CONFIG_DIR/config.yaml" ]; then
show_error "Configuration file not found!\n\nPlease install pyMC Repeater first or ensure $CONFIG_DIR/config.yaml exists."
return
fi
# Check if setup script exists
SCRIPT_DIR="$(dirname "$0")"
RADIO_SCRIPT="$SCRIPT_DIR/setup-radio-config.sh"
if [ ! -f "$RADIO_SCRIPT" ]; then
show_error "Radio configuration script not found!\n\nExpected: $RADIO_SCRIPT"
return
fi
# Ask for confirmation
if ask_yes_no "Configure Radio Settings" "This will update your radio configuration including:\n\n- Repeater name\n- Hardware settings\n- Frequency and LoRa parameters\n\nThe service will be restarted after configuration.\n\nContinue?"; then
# Show info that configuration is starting
show_info "Radio Configuration" "Starting radio configuration...\n\nThe configuration script will now run in the terminal.\n\nFollow the prompts to configure your radio settings."
# Clear screen and run the configuration script
clear
echo "=== pyMC Repeater Radio Configuration ==="
echo ""
# Run the setup script with the config directory
if bash "$RADIO_SCRIPT" "$CONFIG_DIR"; then
echo ""
echo "=== Configuration Complete ==="
# Restart service if it's installed and running
if is_installed; then
echo "Restarting service..."
if [ "$EUID" -eq 0 ]; then
systemctl restart "$SERVICE_NAME" 2>/dev/null || true
sleep 2
if is_running; then
echo "✓ Service restarted successfully"
show_info "Configuration Complete" "Radio configuration updated successfully!\n\n✓ Service restarted\n✓ New settings applied\n\nPress OK to return to main menu."
else
echo "✗ Service failed to restart"
show_error "Configuration updated but service failed to restart!\n\nCheck logs from the main menu for details."
fi
else
show_info "Configuration Complete" "Radio configuration updated successfully!\n\n⚠ Run as root to restart the service automatically\n\nPress OK to return to main menu."
fi
else
show_info "Configuration Complete" "Radio configuration updated successfully!\n\nPress OK to return to main menu."
fi
else
show_error "Configuration failed!\n\nThe radio configuration script encountered an error.\n\nPress OK to return to main menu."
fi
# Pause to let user see any messages
echo ""
read -p "Press Enter to return to main menu..." || true
fi
}
# Uninstall function
uninstall_repeater() {
if [ "$EUID" -ne 0 ]; then
show_error "Uninstall requires root privileges.\n\nPlease run: sudo $0"
return
fi
if ask_yes_no "Confirm Uninstall" "This will completely remove pyMC Repeater including:\n\n- Service and files\n- Configuration (backup will be created)\n- Logs and data\n\nThis action cannot be undone!\n\nContinue?"; then
(
echo "0"; echo "# Stopping and disabling service..."
systemctl stop "$SERVICE_NAME" 2>/dev/null || true
systemctl disable "$SERVICE_NAME" 2>/dev/null || true
echo "20"; echo "# Backing up configuration..."
if [ -d "$CONFIG_DIR" ]; then
cp -r "$CONFIG_DIR" "/tmp/pymc_repeater_config_backup_$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
fi
echo "40"; echo "# Removing service files..."
rm -f /etc/systemd/system/pymc-repeater.service
systemctl daemon-reload
echo "60"; echo "# Removing installation..."
rm -rf "$INSTALL_DIR"
rm -rf "$CONFIG_DIR"
rm -rf "$LOG_DIR"
rm -rf /var/lib/pymc_repeater
echo "80"; echo "# Removing service user..."
if id "$SERVICE_USER" &>/dev/null; then
userdel "$SERVICE_USER" 2>/dev/null || true
fi
echo "100"; echo "# Uninstall complete!"
) | $DIALOG --backtitle "pyMC Repeater Management" --title "Uninstalling" --gauge "Removing pyMC Repeater..." 8 70 0
show_info "Uninstall Complete" "\npyMC Repeater has been completely removed.\n\nConfiguration backup saved to /tmp/\n\nThank you for using pyMC Repeater!"
fi
}
# Service management
manage_service() {
local action=$1
if [ "$EUID" -ne 0 ]; then
show_error "Service management requires root privileges.\n\nPlease run: sudo $0"
return
fi
if ! service_exists; then
show_error "Service is not installed."
return
fi
case $action in
"start")
systemctl start "$SERVICE_NAME"
if is_running; then
show_info "Service Started" "\n✓ pyMC Repeater service has been started successfully."
else
show_error "Failed to start service!\n\nCheck logs for details."
fi
;;
"stop")
systemctl stop "$SERVICE_NAME"
show_info "Service Stopped" "\n✓ pyMC Repeater service has been stopped."
;;
"restart")
systemctl restart "$SERVICE_NAME"
if is_running; then
show_info "Service Restarted" "\n✓ pyMC Repeater service has been restarted successfully."
else
show_error "Failed to restart service!\n\nCheck logs for details."
fi
;;
esac
}
# Show detailed status
show_detailed_status() {
local status_info=""
local version=$(get_version)
local ip_address=$(hostname -I | awk '{print $1}')
status_info="Installation Status: "
if is_installed; then
status_info="${status_info}Installed\n"
status_info="${status_info}Version: $version\n"
status_info="${status_info}Install Directory: $INSTALL_DIR\n"
status_info="${status_info}Config Directory: $CONFIG_DIR\n\n"
status_info="${status_info}Service Status: "
if is_running; then
status_info="${status_info}Running ✓\n"
status_info="${status_info}Web Dashboard: http://$ip_address:8000\n\n"
else
status_info="${status_info}Stopped ✗\n\n"
fi
# Add system info
status_info="${status_info}System Info:\n"
status_info="${status_info}- SPI: "
if grep -q "spi_bcm2835" /proc/modules 2>/dev/null; then
status_info="${status_info}Enabled ✓\n"
else
status_info="${status_info}Disabled ✗\n"
fi
status_info="${status_info}- IP Address: $ip_address\n"
status_info="${status_info}- Hostname: $(hostname)\n"
else
status_info="${status_info}Not Installed"
fi
show_info "System Status" "$status_info"
}
# Function to validate and update configuration
validate_and_update_config() {
local config_file="$CONFIG_DIR/config.yaml"
local example_file="config.yaml.example"
local updated_example="$CONFIG_DIR/config.yaml.example"
# Copy the new example file
if [ -f "$example_file" ]; then
cp "$example_file" "$updated_example"
else
echo " ⚠ config.yaml.example not found in source directory"
return 1
fi
# Check if user config exists
if [ ! -f "$config_file" ]; then
echo " ⚠ No existing config.yaml found, copying example"
cp "$updated_example" "$config_file"
return 0
fi
# Check if yq is available
YQ_CMD="/usr/local/bin/yq"
if ! command -v "$YQ_CMD" &> /dev/null; then
echo " ⚠ mikefarah yq not found at $YQ_CMD, skipping config merge"
return 0
fi
# Verify it's the correct yq version
if [[ "$($YQ_CMD --version 2>&1)" != *"mikefarah/yq"* ]]; then
echo " ⚠ Wrong yq version detected at $YQ_CMD, skipping config merge"
return 0
fi
echo " Merging configuration..."
# Create backup of user config
local backup_file="${config_file}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$config_file" "$backup_file"
echo " ✓ Backup created: $backup_file"
# Merge strategy: user config takes precedence, add missing keys from example
# This uses yq's multiply merge operator (*) which:
# - Keeps all values from the right operand (user config)
# - Adds missing keys from the left operand (example config)
local temp_merged="${config_file}.merged"
if "$YQ_CMD" eval-all '. as $item ireduce ({}; . * $item)' "$updated_example" "$config_file" > "$temp_merged" 2>/dev/null; then
# Verify the merged file is valid YAML
if "$YQ_CMD" eval '.' "$temp_merged" > /dev/null 2>&1; then
mv "$temp_merged" "$config_file"
echo " ✓ Configuration merged successfully"
echo " ✓ User settings preserved, new options added"
return 0
else
echo " ✗ Merged config is invalid, restoring backup"
rm -f "$temp_merged"
cp "$backup_file" "$config_file"
return 1
fi
else
echo " ✗ Config merge failed, keeping original"
rm -f "$temp_merged"
return 1
fi
}
# Main script logic
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "pyMC Repeater Management Script"
echo ""
echo "Usage: $0 [action]"
echo ""
echo "Actions:"
echo " install - Install pyMC Repeater"
echo " upgrade - Upgrade existing installation"
echo " uninstall - Remove pyMC Repeater"
echo " config - Configure radio settings"
echo " start - Start the service"
echo " stop - Stop the service"
echo " restart - Restart the service"
echo " status - Show status"
echo " debug - Show debug information"
echo ""
echo "Run without arguments for interactive menu."
exit 0
fi
# Debug mode
if [ "$1" = "debug" ]; then
echo "=== Debug Information ==="
echo "DIALOG: $DIALOG"
echo "TERM: $TERM"
echo "TTY: $(tty 2>/dev/null || echo 'not a tty')"
echo "EUID: $EUID"
echo "PWD: $PWD"
echo "Script: $0"
echo ""
echo "Testing dialog..."
$DIALOG --backtitle "pyMC Repeater Management" --title "Test" --msgbox "Dialog test successful!" 8 40
echo "Dialog test completed."
exit 0
fi
# Handle command line arguments
case "$1" in
"install")
install_repeater
exit 0
;;
"upgrade")
upgrade_repeater
exit 0
;;
"uninstall")
uninstall_repeater
exit 0
;;
"config")
configure_radio
exit 0
;;
"start"|"stop"|"restart")
manage_service "$1"
exit 0
;;
"status")
show_detailed_status
exit 0
;;
esac
# Interactive menu loop
while true; do
show_main_menu
done