-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·853 lines (738 loc) · 29.9 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·853 lines (738 loc) · 29.9 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
#!/bin/bash
# RPI-Clock Setup Script
# This script automates the installation and configuration of the RPI-Clock project
set -e # Exit on any error
# Define color codes for better terminal output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo "RPI-Clock Setup Script"
echo "======================"
echo ""
# Check if running as root
if [[ $EUID -eq 0 ]]; then
echo "This script should not be run as root. Please run as a regular user."
echo "The script will prompt for sudo when needed."
exit 1
fi
# Non-interactive mode: auto-detect TTY or set via flags
INTERACTIVE=true
AUTO_REBOOT=false
[ -t 0 ] || INTERACTIVE=false
while [[ $# -gt 0 ]]; do
case $1 in
-y|--yes|--non-interactive) INTERACTIVE=false ;;
--reboot) AUTO_REBOOT=true ;;
-h|--help)
echo "Usage: setup.sh [-y|--yes] [--reboot]"
echo " -y, --yes Skip confirmation prompts (non-interactive mode)"
echo " --reboot Auto-reboot at end if hardware config changes require it"
exit 0 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
shift
done
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to prompt for user input
prompt_yes_no() {
if [[ "$INTERACTIVE" == "false" ]]; then
echo "$1 (y/n): y [non-interactive: defaulting to yes]"
return 0
fi
while true; do
read -r -p "$1 (y/n): " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
echo "This script will:"
echo "1. Update package lists"
echo "2. Install Python dependencies"
echo "3. Install GPS daemon (gpsd)"
echo "4. Install time synchronization software (chrony)"
echo "5. Configure GPS daemon"
echo "6. Configure chrony for GPS time sync"
echo "7. Create systemd service for auto-start"
echo ""
if ! prompt_yes_no "Do you want to continue?"; then
echo "Setup cancelled."
exit 0
fi
# Function to check if package is installed
package_installed() {
dpkg -l "$1" 2>/dev/null | grep -q "^ii"
}
# Function to check if Python module is available
python_module_available() {
python3 -c "import $1" 2>/dev/null
}
# Function to check if I2C is enabled
i2c_enabled() {
raspi-config nonint get_i2c 2>/dev/null | grep -q "0"
}
# Function to check if user is in i2c group
user_in_i2c_group() {
groups "$USER" | grep -q i2c
}
# Function to check if user is in gpio group
user_in_gpio_group() {
groups "$USER" | grep -q gpio
}
# Quick check if everything is already installed and configured
echo ""
echo -e "${CYAN}Checking if system is already configured...${NC}"
# Check if all required packages are installed
REQUIRED_PACKAGES=("python3-pip" "python3-venv" "python3-requests" "python3-ntplib" "gpsd" "gpsd-clients" "chrony" "pps-tools" "i2c-tools" "shellcheck")
MISSING_PACKAGES=()
for package in "${REQUIRED_PACKAGES[@]}"; do
if ! package_installed "$package"; then
MISSING_PACKAGES+=("$package")
fi
done
# Check if services are already configured
SERVICES_CONFIGURED=true
if [[ ! -f /etc/systemd/system/rpi-clock.service ]] || [[ ! -f /etc/systemd/system/gpsd.service ]]; then
SERVICES_CONFIGURED=false
fi
# Check if config files exist
CONFIG_FILES_EXIST=true
if [[ ! -f /opt/rpi-clock/clock.py ]] || [[ ! -f /opt/rpi-clock/config.ini ]]; then
CONFIG_FILES_EXIST=false
fi
# Check if I2C and GPIO groups are set up
GROUPS_CONFIGURED=true
if ! user_in_i2c_group || ! user_in_gpio_group; then
GROUPS_CONFIGURED=false
fi
# Check if clock.py needs updating (even if everything else is configured)
CLOCK_NEEDS_UPDATE=false
if [[ -f clock.py ]] && [[ -f /opt/rpi-clock/clock.py ]]; then
if ! diff -q clock.py /opt/rpi-clock/clock.py >/dev/null 2>&1; then
CLOCK_NEEDS_UPDATE=true
fi
fi
# If everything is already configured, skip most steps
if [[ ${#MISSING_PACKAGES[@]} -eq 0 ]] && [[ "$SERVICES_CONFIGURED" == "true" ]] && [[ "$CONFIG_FILES_EXIST" == "true" ]] && [[ "$GROUPS_CONFIGURED" == "true" ]] && i2c_enabled && [[ "$CLOCK_NEEDS_UPDATE" == "false" ]]; then
echo -e "${GREEN}✓ System appears to be fully configured!${NC}"
echo -e "${YELLOW}Skipping package installation and basic configuration...${NC}"
SKIP_PACKAGES=true
else
echo -e "${YELLOW}System needs configuration updates.${NC}"
if [[ ${#MISSING_PACKAGES[@]} -gt 0 ]]; then
echo -e "${CYAN}Missing packages:${NC} ${MISSING_PACKAGES[*]}"
fi
if [[ "$CLOCK_NEEDS_UPDATE" == "true" ]]; then
echo -e "${CYAN}Clock.py needs updating${NC}"
fi
SKIP_PACKAGES=false
fi
if [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 1: Updating package lists...${NC}"
sudo apt update
else
echo -e "${GREEN}✓ Package lists already up to date${NC}"
fi
if [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 2: Installing Python dependencies...${NC}"
if ! package_installed python3-pip; then
sudo apt install -y python3-pip
else
echo -e "${GREEN}✓ python3-pip already installed${NC}"
fi
# Check and install Python packages
echo -e "${CYAN}Installing Python packages via apt (system-wide)...${NC}"
if ! package_installed python3-requests; then
sudo apt install -y python3-requests
else
echo -e "${GREEN}✓ python3-requests already installed${NC}"
fi
if ! package_installed python3-ntplib; then
sudo apt install -y python3-ntplib
else
echo -e "${GREEN}✓ python3-ntplib already installed${NC}"
fi
# Install Adafruit packages via pip (not available in apt)
echo -e "${CYAN}Installing Adafruit CircuitPython packages...${NC}"
if ! python_module_available board; then
echo "Installing adafruit-blinka..."
pip3 install --user adafruit-blinka 2>/dev/null || echo "adafruit-blinka installation skipped (externally managed environment)"
else
echo -e "${GREEN}✓ adafruit-blinka already installed${NC}"
fi
if ! python_module_available adafruit_ht16k33; then
echo "Installing adafruit-circuitpython-ht16k33..."
pip3 install --user adafruit-circuitpython-ht16k33 2>/dev/null || echo "adafruit-circuitpython-ht16k33 installation skipped (externally managed environment)"
else
echo -e "${GREEN}✓ adafruit-circuitpython-ht16k33 already installed${NC}"
fi
# Install shellcheck for bash script validation
if ! command_exists shellcheck; then
echo "Installing shellcheck for bash script validation..."
sudo apt install -y shellcheck
else
echo -e "${GREEN}✓ shellcheck already installed${NC}"
fi
# Also install for root (needed for systemd service)
echo -e "${CYAN}Installing Adafruit packages for root user...${NC}"
if ! sudo python3 -c "import board" 2>/dev/null; then
echo "Installing adafruit-blinka for root..."
sudo pip3 install adafruit-blinka 2>/dev/null || echo "adafruit-blinka root installation skipped (externally managed environment)"
else
echo -e "${GREEN}✓ adafruit-blinka already installed for root${NC}"
fi
if ! sudo python3 -c "import adafruit_ht16k33" 2>/dev/null; then
echo "Installing adafruit-circuitpython-ht16k33 for root..."
sudo pip3 install adafruit-circuitpython-ht16k33 2>/dev/null || echo "adafruit-circuitpython-ht16k33 root installation skipped (externally managed environment)"
else
echo -e "${GREEN}✓ adafruit-circuitpython-ht16k33 already installed for root${NC}"
fi
else
echo -e "${GREEN}✓ All Python dependencies already installed${NC}"
fi
if [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 3: Installing GPS daemon and clients...${NC}"
if ! package_installed gpsd; then
sudo apt install -y gpsd gpsd-clients
else
echo -e "${GREEN}✓ gpsd already installed${NC}"
fi
echo ""
echo -e "${CYAN}Step 4: Installing time synchronization software...${NC}"
if ! package_installed chrony; then
sudo apt install -y chrony
else
echo -e "${GREEN}✓ chrony already installed${NC}"
fi
# Install PPS tools for GPS precision timing
if ! package_installed pps-tools; then
sudo apt install -y pps-tools
else
echo -e "${GREEN}✓ pps-tools already installed${NC}"
fi
else
echo -e "${GREEN}✓ GPS daemon and time sync software already installed${NC}"
fi
# Check if hardware configuration is needed
HARDWARE_CONFIG_NEEDED=false
I2C_WAS_ENABLED=true
if ! i2c_enabled; then
HARDWARE_CONFIG_NEEDED=true
I2C_WAS_ENABLED=false
fi
if ! grep -q "enable_uart=1" /boot/firmware/config.txt || ! grep -q "dtoverlay=disable-bt" /boot/firmware/config.txt || ! grep -q "dtoverlay=pps-gpio" /boot/firmware/config.txt; then
HARDWARE_CONFIG_NEEDED=true
fi
if ! user_in_i2c_group || ! user_in_gpio_group; then
HARDWARE_CONFIG_NEEDED=true
fi
if [[ "$HARDWARE_CONFIG_NEEDED" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 5: Configuring I2C interface for display...${NC}"
# Check if I2C is already enabled
if i2c_enabled; then
echo -e "${GREEN}✓ I2C interface already enabled${NC}"
else
echo "Enabling I2C interface..."
sudo raspi-config nonint do_i2c 0
I2C_WAS_ENABLED=false
fi
# Configure serial port (disable login shell, enable hardware)
sudo raspi-config nonint do_serial 1
# Configure UART for GPS HAT
echo "Configuring UART interface for GPS HAT..."
if grep -q "enable_uart=1" /boot/firmware/config.txt; then
echo -e "${GREEN}✓ UART already enabled${NC}"
else
echo "Enabling UART interface..."
sudo sed -i 's/enable_uart=0/enable_uart=1/' /boot/firmware/config.txt
fi
# Disable Bluetooth to free up UART for GPS HAT
if grep -q "dtoverlay=disable-bt" /boot/firmware/config.txt; then
echo -e "${GREEN}✓ Bluetooth already disabled for GPS HAT${NC}"
else
echo "Disabling Bluetooth to free UART for GPS HAT..."
sudo sed -i '/enable_uart=1/a dtoverlay=disable-bt' /boot/firmware/config.txt
fi
# Enable PPS overlay for GPS precision timing (Adafruit GPS HAT uses GPIO 4)
if grep -q "dtoverlay=pps-gpio" /boot/firmware/config.txt; then
echo -e "${GREEN}✓ PPS overlay already enabled${NC}"
else
echo "Enabling PPS overlay for GPS precision timing..."
sudo sed -i '/dtoverlay=disable-bt/a dtoverlay=pps-gpio,gpiopin=4' /boot/firmware/config.txt
fi
# Note: pps_ldisc module not needed - we use kernel PPS directly via /dev/pps0
# The pps-gpio overlay provides the kernel PPS device that chrony uses
echo -e "${GREEN}✓ PPS kernel module handled by pps-gpio overlay${NC}"
# Install I2C tools
if ! package_installed i2c-tools; then
sudo apt install -y i2c-tools
else
echo -e "${GREEN}✓ i2c-tools already installed${NC}"
fi
# Add user to i2c group
if user_in_i2c_group; then
echo -e "${GREEN}✓ User already in i2c group${NC}"
else
echo "Adding user to i2c group..."
sudo usermod -a -G i2c "$USER"
fi
# Add user to gpio group
if user_in_gpio_group; then
echo -e "${GREEN}✓ User already in gpio group${NC}"
else
echo "Adding user to gpio group..."
sudo usermod -a -G gpio "$USER"
fi
# Add user to dialout group (needed for /dev/ttyACM0 Meshtastic USB access)
if groups "$USER" | grep -q dialout; then
echo -e "${GREEN}✓ User already in dialout group${NC}"
else
echo "Adding user to dialout group..."
sudo usermod -a -G dialout "$USER"
fi
echo -e "${GREEN}✓ I2C, GPIO, and dialout interfaces configured successfully!${NC}"
else
echo -e "${GREEN}✓ I2C, GPIO, and dialout interfaces already configured${NC}"
fi
# Check if GPS daemon configuration is needed
GPS_CONFIG_NEEDED=false
if [[ ! -f /etc/systemd/system/gpsd.service ]] || [[ ! -f /etc/default/gpsd ]]; then
GPS_CONFIG_NEEDED=true
fi
if [[ "$GPS_CONFIG_NEEDED" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 6: Configuring GPS daemon...${NC}"
# Stop and disable default gpsd service (idempotent)
echo "Configuring GPS daemon..."
if systemctl is-active --quiet gpsd.socket; then
echo "Stopping default gpsd.socket service..."
sudo systemctl stop gpsd.socket
fi
if systemctl is-enabled --quiet gpsd.socket; then
echo "Disabling default gpsd.socket service..."
sudo systemctl disable gpsd.socket
fi
# Create gpsd service file
sudo tee /etc/systemd/system/gpsd.service > /dev/null <<EOF
[Unit]
Description=GPS daemon
After=network.target
[Service]
Type=simple
ExecStart=/usr/sbin/gpsd -N -n /dev/ttyAMA0 /dev/pps0
Restart=always
RestartSec=5
User=gpsd
Group=dialout
[Install]
WantedBy=multi-user.target
EOF
# Configure GPSD defaults
echo "Configuring GPSD defaults..."
sudo tee /etc/default/gpsd > /dev/null <<EOF
# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyAMA0 /dev/pps0"
# Other options you want to pass to gpsd
GPSD_OPTIONS="-n"
# Automatically hot add/remove USB GPS devices via gpsdctl
USBAUTO="false"
EOF
# Enable and start gpsd (idempotent)
sudo systemctl daemon-reload
if ! systemctl is-enabled --quiet gpsd.service; then
echo "Enabling gpsd.service..."
sudo systemctl enable gpsd.service
else
echo -e "${GREEN}✓ gpsd.service already enabled${NC}"
fi
else
echo -e "${GREEN}✓ GPS daemon already configured${NC}"
fi
# Check if chrony configuration is needed
CHRONY_CONFIG_NEEDED=false
if [[ ! -f /etc/chrony/chrony.conf.backup ]]; then
CHRONY_CONFIG_NEEDED=true
fi
# Check if chrony.conf needs updating (compare with our version)
if [[ -f chrony.conf ]] && [[ -f /etc/chrony/chrony.conf ]]; then
if ! diff -q chrony.conf /etc/chrony/chrony.conf >/dev/null 2>&1; then
CHRONY_CONFIG_NEEDED=true
fi
fi
if [[ "$CHRONY_CONFIG_NEEDED" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 7: Configuring chrony for GPS time synchronization...${NC}"
# Backup original chrony.conf (idempotent)
if [[ ! -f /etc/chrony/chrony.conf.backup ]]; then
echo "Backing up original chrony.conf..."
sudo cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.backup
else
echo -e "${GREEN}✓ chrony.conf backup already exists${NC}"
fi
# Install optimized chrony configuration
echo "Installing optimized chrony configuration..."
sudo cp chrony.conf /etc/chrony/chrony.conf
# Create chrony group for PPS device access
echo "Setting up PPS device permissions for chrony..."
sudo groupadd --system chrony 2>/dev/null || true
# Create udev rule for PPS device permissions
echo 'SUBSYSTEM=="pps", KERNEL=="pps[0-9]*", GROUP="chrony", MODE="0660"' | sudo tee /etc/udev/rules.d/85-pps-permissions.rules > /dev/null
sudo udevadm control --reload-rules
sudo udevadm trigger -s pps
# Restart chrony
echo "Restarting chrony service..."
sudo systemctl restart chrony
else
echo -e "${GREEN}✓ Chrony configuration already up to date${NC}"
fi
# Check if clock.py needs updating (only file we should overwrite)
CLOCK_NEEDS_UPDATE=false
if [[ ! -f /opt/rpi-clock/clock.py ]]; then
CLOCK_NEEDS_UPDATE=true
elif [[ -f clock.py ]] && [[ -f /opt/rpi-clock/clock.py ]]; then
# Check if content differs (in case files were updated without timestamp change)
if ! diff -q clock.py /opt/rpi-clock/clock.py >/dev/null 2>&1; then
CLOCK_NEEDS_UPDATE=true
fi
fi
# Check if config.ini exists (preserve user's configuration)
CONFIG_EXISTS=false
if [[ -f /opt/rpi-clock/config.ini ]]; then
CONFIG_EXISTS=true
fi
# Check if other diagnostic scripts need updating (only if missing)
SCRIPTS_NEED_UPDATE=false
for file in i2c-test.sh gps-test.sh ntp-test.sh; do
if [[ ! -f "/opt/rpi-clock/$file" ]]; then
SCRIPTS_NEED_UPDATE=true
break
fi
done
if [[ "$CLOCK_NEEDS_UPDATE" == "true" ]] || [[ "$CONFIG_EXISTS" == "false" ]] || [[ "$SCRIPTS_NEED_UPDATE" == "true" ]]; then
echo ""
echo -e "${CYAN}Step 8: Installing RPI-Clock files...${NC}"
# Create installation directory (idempotent)
sudo mkdir -p /opt/rpi-clock
# Copy clock.py if it needs updating
if [[ "$CLOCK_NEEDS_UPDATE" == "true" ]]; then
echo "Updating clock.py..."
sudo cp clock.py /opt/rpi-clock/
else
echo -e "${GREEN}✓ clock.py already up to date${NC}"
fi
# Copy config.ini only if it doesn't exist (preserve user's configuration)
if [[ "$CONFIG_EXISTS" == "false" ]]; then
echo "Installing config.ini..."
sudo cp config.ini /opt/rpi-clock/
# Make config.ini editable by the user who ran the setup
sudo chown root:"$USER" /opt/rpi-clock/config.ini
sudo chmod 664 /opt/rpi-clock/config.ini
else
echo -e "${GREEN}✓ config.ini already exists (preserving user configuration)${NC}"
fi
# Copy optional files (only if they exist in source)
sudo cp rpi-clock-logo.png /opt/rpi-clock/ 2>/dev/null || true
sudo cp rpi-clock.gif /opt/rpi-clock/ 2>/dev/null || true
# Copy diagnostic scripts only if missing
if [[ "$SCRIPTS_NEED_UPDATE" == "true" ]]; then
echo "Installing diagnostic scripts..."
sudo cp i2c-test.sh /opt/rpi-clock/ 2>/dev/null || true
sudo cp gps-test.sh /opt/rpi-clock/ 2>/dev/null || true
sudo cp ntp-test.sh /opt/rpi-clock/ 2>/dev/null || true
else
echo -e "${GREEN}✓ Diagnostic scripts already installed${NC}"
fi
# Set proper permissions
echo "Setting file permissions..."
sudo chown -R root:root /opt/rpi-clock
sudo chmod 755 /opt/rpi-clock
sudo chmod 644 /opt/rpi-clock/*.py /opt/rpi-clock/*.ini /opt/rpi-clock/*.png /opt/rpi-clock/*.gif 2>/dev/null || true
sudo chmod 755 /opt/rpi-clock/*.sh 2>/dev/null || true
# Ensure config.ini has correct permissions if it exists
if [[ -f /opt/rpi-clock/config.ini ]]; then
sudo chown root:"$USER" /opt/rpi-clock/config.ini
sudo chmod 664 /opt/rpi-clock/config.ini
fi
# If clock.py was updated, restart the service to pick up changes
if [[ "$CLOCK_NEEDS_UPDATE" == "true" ]]; then
echo -e "${YELLOW}Restarting rpi-clock service to pick up clock.py changes...${NC}"
sudo systemctl restart rpi-clock.service
echo -e "${GREEN}✓ rpi-clock service restarted${NC}"
fi
else
echo -e "${GREEN}✓ RPI-Clock files already up to date${NC}"
fi
# Check if service creation is needed
SERVICE_NEEDED=false
if [[ ! -f /etc/systemd/system/rpi-clock.service ]]; then
SERVICE_NEEDED=true
fi
if [[ "$SERVICE_NEEDED" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 9: Creating systemd service for RPI-Clock...${NC}"
# Create systemd service file (run as user with GPIO group access)
sudo tee /etc/systemd/system/rpi-clock.service > /dev/null <<EOF
[Unit]
Description=RPI Clock - GPS-synchronized timekeeper with weather display
Documentation=https://github.com/jkeychan/rpi-clock
After=network.target gpsd.service
Wants=gpsd.service
[Service]
Type=simple
User="$USER"
Group="$USER"
WorkingDirectory=/opt/rpi-clock
ExecStart=/usr/bin/python3 -u /opt/rpi-clock/clock.py
ExecReload=/bin/kill -HUP \$MAINPID
Restart=always
RestartSec=10
# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=rpi-clock
[Install]
WantedBy=multi-user.target
EOF
# Enable and start service (idempotent)
sudo systemctl daemon-reload
if ! systemctl is-enabled --quiet rpi-clock.service; then
echo "Enabling rpi-clock.service..."
sudo systemctl enable rpi-clock.service
else
echo -e "${GREEN}✓ rpi-clock.service already enabled${NC}"
fi
else
echo -e "${GREEN}✓ RPI-Clock service already configured${NC}"
fi
# Step 10: Meshtastic GPS forwarder (venv + service)
MESHTASTIC_NEEDED=false
if [[ ! -f /opt/rpi-clock/venv/bin/python3 ]] || [[ ! -f /etc/systemd/system/meshtastic-gps-forwarder.service ]]; then
MESHTASTIC_NEEDED=true
fi
if [[ "$MESHTASTIC_NEEDED" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 10: Setting up Meshtastic GPS forwarder...${NC}"
# Create Python venv for meshtastic (isolated to avoid system package conflicts)
if [[ ! -f /opt/rpi-clock/venv/bin/python3 ]]; then
echo "Creating Python virtual environment at /opt/rpi-clock/venv..."
sudo python3 -m venv /opt/rpi-clock/venv
echo -e "${GREEN}✓ Virtual environment created${NC}"
else
echo -e "${GREEN}✓ Virtual environment already exists${NC}"
fi
# Install meshtastic Python library in venv
if ! sudo /opt/rpi-clock/venv/bin/python3 -c "import meshtastic" 2>/dev/null; then
echo "Installing meshtastic Python library..."
sudo /opt/rpi-clock/venv/bin/pip install --quiet meshtastic
echo -e "${GREEN}✓ meshtastic installed${NC}"
else
echo -e "${GREEN}✓ meshtastic already installed${NC}"
fi
# Copy GPS inject script
if [[ -f gps-meshtastic-inject.py ]]; then
sudo cp gps-meshtastic-inject.py /opt/rpi-clock/
sudo chmod 644 /opt/rpi-clock/gps-meshtastic-inject.py
echo -e "${GREEN}✓ Installed gps-meshtastic-inject.py${NC}"
fi
# Install meshtastic GPS forwarder service, substituting current username
if [[ -f meshtastic-gps-forwarder.service ]]; then
sed "s/User=jeff/User=$USER/" meshtastic-gps-forwarder.service \
| sudo tee /etc/systemd/system/meshtastic-gps-forwarder.service > /dev/null
sudo systemctl daemon-reload
if ! systemctl is-enabled --quiet meshtastic-gps-forwarder.service; then
sudo systemctl enable meshtastic-gps-forwarder.service
echo -e "${GREEN}✓ meshtastic-gps-forwarder.service enabled${NC}"
else
echo -e "${GREEN}✓ meshtastic-gps-forwarder.service already enabled${NC}"
fi
fi
else
echo -e "${GREEN}✓ Meshtastic GPS forwarder already configured${NC}"
fi
# Step 11: Apply kernel parameter optimizations for SD card and low-RAM operation
if [[ ! -f /etc/sysctl.d/99-rpi-clock.conf ]]; then
echo ""
echo -e "${CYAN}Step 11: Applying system kernel optimizations...${NC}"
if [[ -f sysctl/99-rpi-clock.conf ]]; then
sudo cp sysctl/99-rpi-clock.conf /etc/sysctl.d/
sudo sysctl --system > /dev/null 2>&1
echo -e "${GREEN}✓ Kernel parameters applied (SD card + low-RAM tuning)${NC}"
fi
else
echo -e "${GREEN}✓ System kernel optimizations already applied${NC}"
fi
# Check if services need to be started
SERVICES_NEED_START=false
if ! systemctl is-active --quiet gpsd.service || ! systemctl is-active --quiet rpi-clock.service; then
SERVICES_NEED_START=true
fi
if [[ "$SERVICES_NEED_START" == "true" ]] || [[ "$SKIP_PACKAGES" == "false" ]]; then
echo ""
echo -e "${CYAN}Step 12: Starting services...${NC}"
# Start GPS daemon (idempotent)
if ! systemctl is-active --quiet gpsd.service; then
echo "Starting gpsd.service..."
sudo systemctl start gpsd.service
else
echo -e "${GREEN}✓ gpsd.service already running${NC}"
fi
# Start RPI-Clock service (idempotent)
if ! systemctl is-active --quiet rpi-clock.service; then
echo "Starting rpi-clock.service..."
sudo systemctl start rpi-clock.service
else
echo -e "${GREEN}✓ rpi-clock.service already running${NC}"
fi
else
echo -e "${GREEN}✓ All services already running${NC}"
fi
echo ""
echo "Step 13: Validating installation..."
# Validate GPS daemon is running
if systemctl is-active --quiet gpsd.service; then
echo -e "${GREEN}✓ GPS daemon (gpsd) is running${NC}"
else
echo -e "${RED}✗ GPS daemon (gpsd) is not running${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo systemctl status gpsd.service${NC}"
fi
# Validate chrony is running
if systemctl is-active --quiet chrony.service; then
echo -e "${GREEN}✓ Chrony time synchronization is running${NC}"
else
echo -e "${RED}✗ Chrony time synchronization is not running${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo systemctl status chrony.service${NC}"
fi
# Validate RPI-Clock service is running
if systemctl is-active --quiet rpi-clock.service; then
echo -e "${GREEN}✓ RPI-Clock service is running${NC}"
else
echo -e "${RED}✗ RPI-Clock service is not running${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo systemctl status rpi-clock.service${NC}"
fi
# Validate GPS device exists
if [[ -e /dev/ttyAMA0 ]]; then
echo -e "${GREEN}✓ GPS device /dev/ttyAMA0 exists${NC}"
else
echo -e "${RED}✗ GPS device /dev/ttyAMA0 not found${NC}"
echo -e " ${YELLOW}This may require a reboot to activate UART configuration${NC}"
fi
# Validate PPS device exists
if [[ -e /dev/pps0 ]]; then
echo -e "${GREEN}✓ PPS device /dev/pps0 exists${NC}"
else
echo -e "${RED}✗ PPS device /dev/pps0 not found${NC}"
echo -e " ${YELLOW}This may require a reboot to activate PPS configuration${NC}"
fi
# Validate I2C is enabled
if i2c_enabled; then
echo -e "${GREEN}✓ I2C interface is enabled${NC}"
else
echo -e "${RED}✗ I2C interface is not enabled${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo raspi-config nonint do_i2c 0${NC}"
fi
# Validate user groups
if user_in_i2c_group; then
echo -e "${GREEN}✓ User is in i2c group${NC}"
else
echo -e "${RED}✗ User is not in i2c group${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo usermod -a -G i2c $USER${NC}"
fi
if user_in_gpio_group; then
echo -e "${GREEN}✓ User is in gpio group${NC}"
else
echo -e "${RED}✗ User is not in gpio group${NC}"
echo -e " ${YELLOW}Run:${NC} ${GREEN}sudo usermod -a -G gpio $USER${NC}"
fi
echo ""
echo -e "${GREEN}${BOLD}Setup completed successfully!${NC}"
echo ""
echo -e "${CYAN}Services started:${NC}"
echo -e "${WHITE}-${NC} ${GREEN}GPS daemon (gpsd)${NC}"
echo -e "${WHITE}-${NC} ${GREEN}RPI-Clock service${NC}"
# Check if reboot is needed
REBOOT_NEEDED=false
REBOOT_REASONS=()
if [[ "${I2C_WAS_ENABLED:-true}" == "false" ]]; then
REBOOT_NEEDED=true
REBOOT_REASONS+=("I2C interface enabled")
fi
if ! user_in_gpio_group; then
REBOOT_NEEDED=true
REBOOT_REASONS+=("User added to GPIO group")
fi
# Check if UART was enabled, Bluetooth disabled, or PPS overlay added
if ! grep -q "enable_uart=1" /boot/firmware/config.txt || ! grep -q "dtoverlay=disable-bt" /boot/firmware/config.txt || ! grep -q "dtoverlay=pps-gpio" /boot/firmware/config.txt; then
REBOOT_NEEDED=true
REBOOT_REASONS+=("UART enabled, Bluetooth disabled, and PPS overlay added for GPS HAT")
fi
if [[ "$REBOOT_NEEDED" == "true" ]]; then
echo ""
echo -e "${RED}${BOLD}IMPORTANT: Reboot Required${NC}"
echo -e "${RED}==========================${NC}"
echo -e "${YELLOW}The following changes require a reboot to activate:${NC}"
for reason in "${REBOOT_REASONS[@]}"; do
echo -e "${WHITE}-${NC} ${CYAN}$reason${NC}"
done
echo ""
echo -e "${GREEN}After reboot:${NC}"
echo -e "${WHITE}-${NC} The GPS HAT will be detected on ${GREEN}/dev/ttyAMA0${NC}"
echo -e "${WHITE}-${NC} The 7-segment display should show the current time"
echo -e "${WHITE}-${NC} GPS time synchronization will be available"
echo ""
if [[ "$AUTO_REBOOT" == "true" ]]; then
echo -e "${YELLOW}Rebooting in 5 seconds... (--reboot flag set)${NC}"
sleep 5
sudo reboot
else
echo ""
echo -e "${YELLOW}A reboot is required to activate hardware changes (UART, PPS overlay, Bluetooth disable).${NC}"
echo -e "${WHITE}Run:${NC} ${GREEN}sudo reboot${NC}"
echo ""
echo -e "${GREEN}After reboot:${NC}"
echo -e "${WHITE}-${NC} GPS HAT will be available on ${GREEN}/dev/ttyAMA0${NC}"
echo -e "${WHITE}-${NC} The 7-segment display should show the current time"
echo -e "${WHITE}-${NC} If the display is blank, check the troubleshooting guide in ${BLUE}README.md${NC}"
echo ""
echo -e "${CYAN}Tip: re-run setup.sh with --reboot to reboot automatically next time.${NC}"
fi
else
echo ""
echo -e "${GREEN}✓ All interfaces already configured - no reboot required!${NC}"
fi
echo ""
echo -e "${CYAN}${BOLD}Next steps after reboot:${NC}"
echo -e "${WHITE}1.${NC} ${YELLOW}Edit config.ini with your OpenWeatherMap API key and ZIP code:${NC}"
echo -e " ${GREEN}sudo nano /opt/rpi-clock/config.ini${NC}"
echo -e " ${WHITE}-${NC} Replace ${RED}'XXXXXXXX'${NC} with your actual ${BLUE}OpenWeatherMap API key${NC}"
echo -e " ${WHITE}-${NC} Replace ${RED}'90210'${NC} with your actual ${BLUE}ZIP code${NC}"
echo -e "${WHITE}2.${NC} ${YELLOW}Ensure your GPS antenna has a clear view of the sky${NC}"
echo -e "${WHITE}3.${NC} ${YELLOW}Test GPS connection:${NC} ${GREEN}cgps -s${NC}"
echo -e "${WHITE}4.${NC} ${YELLOW}Check GPS HAT detection:${NC} ${GREEN}ls -la /dev/ttyAMA*${NC}"
echo -e "${WHITE}5.${NC} ${YELLOW}Check chrony sources:${NC} ${GREEN}chronyc sources${NC}"
echo -e "${WHITE}6.${NC} ${YELLOW}Check clock status:${NC} ${GREEN}sudo systemctl status rpi-clock${NC}"
echo -e "${WHITE}7.${NC} ${YELLOW}View clock logs:${NC} ${GREEN}sudo journalctl -u rpi-clock -f${NC}"
echo ""
echo -e "${RED}${BOLD}IMPORTANT:${NC} ${YELLOW}The clock will not display weather data until you:${NC}"
echo -e "${WHITE}-${NC} Get a free API key from ${BLUE}https://openweathermap.org/api_keys/${NC}"
echo -e "${WHITE}-${NC} Update ${GREEN}/opt/rpi-clock/config.ini${NC} with your API key and ZIP code"
echo -e "${WHITE}-${NC} Restart the service: ${GREEN}sudo systemctl restart rpi-clock${NC}"
echo ""
echo -e "${GREEN}✓${NC} The clock will automatically start on boot."
echo -e "${GREEN}✓${NC} GPS HAT will be available on ${GREEN}/dev/ttyAMA0${NC} after reboot."
echo -e "${CYAN}ℹ${NC} For troubleshooting, see the ${BLUE}README.md${NC} file."