-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathinstall.sh
More file actions
1158 lines (1027 loc) · 42.4 KB
/
Copy pathinstall.sh
File metadata and controls
1158 lines (1027 loc) · 42.4 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
#!/bin/bash
# Raspberry Ninja Universal Installer
# Supports: Raspberry Pi, Orange Pi, Nvidia Jetson, Ubuntu, Debian, WSL
# This script provides an interactive installation and setup experience
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Global variables
PLATFORM=""
DISTRO=""
VERSION=""
ARCH=""
IS_WSL=false
IS_RASPBERRY_PI=false
IS_ORANGE_PI=false
IS_JETSON=false
# Determine script directory - handle both piped and direct execution
if [ -n "${BASH_SOURCE[0]}" ] && [ -f "${BASH_SOURCE[0]}" ]; then
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
else
# When piped, we need to find or clone the repository
SCRIPT_DIR=""
fi
# We'll set CONFIG_FILE after we know the actual directory
CONFIG_FILE=""
SERVICE_NAME="raspberry-ninja"
SETUP_AUTOSTART=false
install_type=""
NON_INTERACTIVE=false
RUNTIME_ONLY=false
SKIP_SYSTEM_UPGRADE=false
# Check for command line arguments
for arg in "$@"; do
case $arg in
--non-interactive|-y|--yes)
NON_INTERACTIVE=true
;;
--runtime-only)
RUNTIME_ONLY=true
;;
--skip-system-upgrade)
SKIP_SYSTEM_UPGRADE=true
;;
--help|-h)
echo "Raspberry Ninja Installer"
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " --non-interactive, -y Run in non-interactive mode"
echo " --runtime-only Install runtime packages without development headers"
echo " --skip-system-upgrade Refresh package indexes without upgrading the OS"
echo " --help, -h Show this help message"
exit 0
;;
esac
done
# Check if we're running in non-interactive mode (piped input)
if [ ! -t 0 ] && [ ! -e /dev/tty ]; then
NON_INTERACTIVE=true
fi
# Print colored output
print_color() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Safe read function that works with piped input
safe_read() {
local prompt="$1"
local options="$2"
local default_value="$3"
if [ "$NON_INTERACTIVE" = true ]; then
if [ -n "$default_value" ]; then
REPLY="$default_value"
print_color "$YELLOW" "Non-interactive mode: Using default value '$default_value'"
else
print_color "$RED" "Error: Non-interactive mode requires default values"
exit 1
fi
else
if [ -n "$options" ]; then
read -p "$prompt" $options < /dev/tty
else
read -p "$prompt" < /dev/tty
fi
fi
}
print_header() {
echo
print_color "$BLUE" "=========================================="
print_color "$BLUE" " Raspberry Ninja Installer"
print_color "$BLUE" "=========================================="
echo
if [ "$NON_INTERACTIVE" = true ]; then
print_color "$YELLOW" "Running in NON-INTERACTIVE mode"
print_color "$YELLOW" "Only basic dependency installation will be performed."
print_color "$YELLOW" ""
print_color "$YELLOW" "For full setup with configuration and auto-start:"
print_color "$YELLOW" " wget https://raw.githubusercontent.com/steveseguin/raspberry_ninja/main/install.sh"
print_color "$YELLOW" " chmod +x install.sh"
print_color "$YELLOW" " ./install.sh"
echo
fi
}
# Find or setup the raspberry_ninja directory
setup_repository() {
if [ -z "$SCRIPT_DIR" ]; then
# We're running from curl, need to find or clone the repo
print_color "$YELLOW" "Checking for Raspberry Ninja repository..."
# Check if we're already in the repo
if [ -f "$(pwd)/publish.py" ]; then
SCRIPT_DIR="$(pwd)"
print_color "$GREEN" "✓ Already in Raspberry Ninja repository at: $SCRIPT_DIR"
elif [ -f "$(pwd)/raspberry_ninja/publish.py" ]; then
SCRIPT_DIR="$(pwd)/raspberry_ninja"
print_color "$GREEN" "✓ Found existing installation at: $SCRIPT_DIR"
else
# Need to clone the repository to current directory
print_color "$YELLOW" "Raspberry Ninja repository not found. Installing to current directory..."
# Clone to current directory
INSTALL_DIR="$(pwd)/raspberry_ninja"
# Clone the repository
print_color "$YELLOW" "Cloning repository to $INSTALL_DIR..."
if command -v git &> /dev/null; then
# Check if directory already exists
if [ -d "$INSTALL_DIR" ]; then
SCRIPT_DIR="$INSTALL_DIR"
print_color "$YELLOW" "Directory $INSTALL_DIR already exists. Checking for updates..."
# Check if it's a git repository
if [ -d "$INSTALL_DIR/.git" ]; then
# Pull latest changes
cd "$INSTALL_DIR"
if git pull origin main; then
print_color "$GREEN" "✓ Updated to latest version"
else
print_color "$YELLOW" "⚠ Could not update (may have local changes)"
fi
cd - > /dev/null
else
print_color "$YELLOW" "⚠ Not a git repository, using as-is"
fi
# Verify it's a valid installation
if [ -f "$SCRIPT_DIR/publish.py" ]; then
print_color "$GREEN" "✓ Using existing installation at: $SCRIPT_DIR"
else
print_color "$YELLOW" "⚠ Directory exists but publish.py not found. Attempting fresh clone..."
# Try to clone into a temp dir and move files
TEMP_DIR="$(mktemp -d)"
if git clone https://github.com/steveseguin/raspberry_ninja.git "$TEMP_DIR/raspberry_ninja"; then
cp -r "$TEMP_DIR/raspberry_ninja/"* "$INSTALL_DIR/" 2>/dev/null || true
cp -r "$TEMP_DIR/raspberry_ninja/".* "$INSTALL_DIR/" 2>/dev/null || true
rm -rf "$TEMP_DIR"
print_color "$GREEN" "✓ Repository files copied to: $SCRIPT_DIR"
else
rm -rf "$TEMP_DIR"
print_color "$RED" "✗ Failed to get repository files"
exit 1
fi
fi
else
if git clone https://github.com/steveseguin/raspberry_ninja.git "$INSTALL_DIR"; then
SCRIPT_DIR="$INSTALL_DIR"
print_color "$GREEN" "✓ Repository cloned successfully to: $SCRIPT_DIR"
# Verify the clone worked
if [ -f "$SCRIPT_DIR/publish.py" ]; then
print_color "$GREEN" "✓ Verified: publish.py found in $SCRIPT_DIR"
else
print_color "$RED" "✗ Error: Repository cloned but publish.py not found"
exit 1
fi
else
print_color "$RED" "✗ Failed to clone repository"
print_color "$RED" " Please check your internet connection and try again"
exit 1
fi
fi
else
print_color "$RED" "✗ Git is not installed. This should not happen."
exit 1
fi
fi
else
# SCRIPT_DIR was already set (running from downloaded script)
# Verify it's actually the raspberry_ninja directory
if [ ! -f "$SCRIPT_DIR/publish.py" ]; then
# Not in the right directory, find or clone
print_color "$YELLOW" "Current directory is not the Raspberry Ninja repository..."
# Check if raspberry_ninja exists in current directory
if [ -f "$(pwd)/raspberry_ninja/publish.py" ]; then
SCRIPT_DIR="$(pwd)/raspberry_ninja"
print_color "$GREEN" "✓ Found existing installation at: $SCRIPT_DIR"
else
# Need to clone the repository to current directory
print_color "$YELLOW" "Cloning Raspberry Ninja to current directory..."
# Clone to current directory
INSTALL_DIR="$(pwd)/raspberry_ninja"
# Clone the repository
print_color "$YELLOW" "Cloning repository to $INSTALL_DIR..."
if command -v git &> /dev/null; then
# Check if directory already exists
if [ -d "$INSTALL_DIR" ]; then
SCRIPT_DIR="$INSTALL_DIR"
print_color "$YELLOW" "Directory $INSTALL_DIR already exists. Checking for updates..."
# Check if it's a git repository
if [ -d "$INSTALL_DIR/.git" ]; then
# Pull latest changes
cd "$INSTALL_DIR"
if git pull origin main; then
print_color "$GREEN" "✓ Updated to latest version"
else
print_color "$YELLOW" "⚠ Could not update (may have local changes)"
fi
cd - > /dev/null
else
print_color "$YELLOW" "⚠ Not a git repository, using as-is"
fi
# Verify it's a valid installation
if [ -f "$SCRIPT_DIR/publish.py" ]; then
print_color "$GREEN" "✓ Using existing installation at: $SCRIPT_DIR"
else
print_color "$YELLOW" "⚠ Directory exists but publish.py not found. Attempting fresh clone..."
# Try to clone into a temp dir and move files
TEMP_DIR="$(mktemp -d)"
if git clone https://github.com/steveseguin/raspberry_ninja.git "$TEMP_DIR/raspberry_ninja"; then
cp -r "$TEMP_DIR/raspberry_ninja/"* "$INSTALL_DIR/" 2>/dev/null || true
cp -r "$TEMP_DIR/raspberry_ninja/".* "$INSTALL_DIR/" 2>/dev/null || true
rm -rf "$TEMP_DIR"
print_color "$GREEN" "✓ Repository files copied to: $SCRIPT_DIR"
else
rm -rf "$TEMP_DIR"
print_color "$RED" "✗ Failed to get repository files"
exit 1
fi
fi
else
if git clone https://github.com/steveseguin/raspberry_ninja.git "$INSTALL_DIR"; then
SCRIPT_DIR="$INSTALL_DIR"
print_color "$GREEN" "✓ Repository cloned successfully to: $SCRIPT_DIR"
else
print_color "$RED" "✗ Failed to clone repository"
print_color "$RED" " Please check your internet connection and try again"
exit 1
fi
fi
else
print_color "$RED" "✗ Git is not installed. This should not happen."
exit 1
fi
fi
fi
fi
# Now set the config file location
CONFIG_FILE="$SCRIPT_DIR/config.json"
# Final verification
if [ ! -f "$SCRIPT_DIR/publish.py" ]; then
print_color "$RED" "✗ Error: publish.py not found in $SCRIPT_DIR"
print_color "$RED" " Something went wrong during setup"
exit 1
fi
}
# Detect platform and system information
detect_platform() {
print_color "$YELLOW" "Detecting system platform..."
# Check if running in WSL
if grep -qi microsoft /proc/version; then
IS_WSL=true
PLATFORM="WSL"
fi
# Get architecture
ARCH=$(uname -m)
# Get distribution info
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO=$ID
VERSION=$VERSION_ID
fi
# Check for Raspberry Pi
if [ -f /proc/device-tree/model ]; then
MODEL=$(tr -d '\0' < /proc/device-tree/model)
if [[ $MODEL == *"Raspberry Pi"* ]]; then
IS_RASPBERRY_PI=true
PLATFORM="Raspberry Pi"
elif [[ $MODEL == *"Orange Pi"* ]]; then
IS_ORANGE_PI=true
PLATFORM="Orange Pi"
fi
fi
# Check for Nvidia Jetson
if [ -f /etc/nv_tegra_release ] || [ -f /sys/module/tegra_fuse/parameters/tegra_chip_id ]; then
IS_JETSON=true
PLATFORM="Nvidia Jetson"
fi
# Default to generic Linux if not detected
if [ -z "$PLATFORM" ]; then
PLATFORM="Linux"
fi
print_color "$GREEN" "✓ Detected: $PLATFORM ($DISTRO $VERSION) on $ARCH"
echo
}
# Check if running as root
check_root() {
if [ "$EUID" -eq 0 ]; then
print_color "$YELLOW" "⚠ Running as root. Some operations may create files owned by root."
print_color "$YELLOW" " It's recommended to run this script as a regular user with sudo access."
echo
safe_read "Continue anyway? (y/N): " "-n 1 -r" "N"
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# Update system packages
update_system() {
print_color "$YELLOW" "Updating system packages..."
if command -v apt-get &> /dev/null; then
sudo apt-get update
if [ "$SKIP_SYSTEM_UPGRADE" = false ]; then
sudo apt-get upgrade -y
else
print_color "$YELLOW" "Skipping the full OS upgrade as requested."
fi
elif command -v dnf &> /dev/null; then
sudo dnf update -y
elif command -v yum &> /dev/null; then
sudo yum update -y
else
print_color "$RED" "✗ Package manager not supported. Please update system manually."
return 1
fi
print_color "$GREEN" "✓ System updated"
}
ensure_display_utilities() {
local missing_drm=true
local missing_kmsprint=true
if command -v drm_info &> /dev/null; then
missing_drm=false
fi
if command -v kmsprint &> /dev/null; then
missing_kmsprint=false
fi
if command -v apt-get &> /dev/null; then
if [ "$missing_drm" = true ]; then
local drm_candidates=("drm-tools" "libdrm-tests" "drm-info")
for pkg in "${drm_candidates[@]}"; do
if dpkg -s "$pkg" &> /dev/null; then
missing_drm=false
break
fi
print_color "$YELLOW" "Attempting to install $pkg for drm_info support..."
if sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "$pkg"; then
if command -v drm_info &> /dev/null; then
missing_drm=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
if [ "$missing_kmsprint" = true ]; then
local kms_candidates=("kms++-utils" "kms-tools")
for pkg in "${kms_candidates[@]}"; do
if dpkg -s "$pkg" &> /dev/null; then
missing_kmsprint=false
break
fi
print_color "$YELLOW" "Attempting to install $pkg for kmsprint support..."
if sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "$pkg"; then
if command -v kmsprint &> /dev/null; then
missing_kmsprint=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
elif command -v dnf &> /dev/null; then
if [ "$missing_drm" = true ]; then
local drm_candidates_dnf=("drm-info" "libdrm-tests")
for pkg in "${drm_candidates_dnf[@]}"; do
print_color "$YELLOW" "Attempting to install $pkg for drm_info support..."
if sudo dnf install -y "$pkg"; then
if command -v drm_info &> /dev/null; then
missing_drm=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
if [ "$missing_kmsprint" = true ]; then
local kms_candidates_dnf=("kms++-utils" "kms-tools")
for pkg in "${kms_candidates_dnf[@]}"; do
print_color "$YELLOW" "Attempting to install $pkg for kmsprint support..."
if sudo dnf install -y "$pkg"; then
if command -v kmsprint &> /dev/null; then
missing_kmsprint=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
elif command -v yum &> /dev/null; then
if [ "$missing_drm" = true ]; then
local drm_candidates_yum=("drm-info" "libdrm-tests")
for pkg in "${drm_candidates_yum[@]}"; do
print_color "$YELLOW" "Attempting to install $pkg for drm_info support..."
if sudo yum install -y "$pkg"; then
if command -v drm_info &> /dev/null; then
missing_drm=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
if [ "$missing_kmsprint" = true ]; then
local kms_candidates_yum=("kms++-utils" "kms-tools")
for pkg in "${kms_candidates_yum[@]}"; do
print_color "$YELLOW" "Attempting to install $pkg for kmsprint support..."
if sudo yum install -y "$pkg"; then
if command -v kmsprint &> /dev/null; then
missing_kmsprint=false
break
fi
else
print_color "$YELLOW" "Package $pkg unavailable; trying next option..."
fi
done
fi
fi
if [ "$missing_drm" = true ]; then
print_color "$YELLOW" "⚠ Could not install drm_info automatically. Display detection will fall back to other probes."
fi
if [ "$missing_kmsprint" = true ]; then
print_color "$YELLOW" "⚠ kmsprint not available. Display detection will rely on alternative tools."
fi
}
# Install platform-specific dependencies
install_dependencies() {
print_color "$YELLOW" "Installing dependencies for $PLATFORM..."
# Common dependencies for Debian-based systems
if command -v apt-get &> /dev/null; then
# Handle Python package management on newer systems
if [ -f /usr/lib/python3.*/EXTERNALLY-MANAGED ]; then
print_color "$YELLOW" "Detected PEP 668 system. Using --break-system-packages flag."
PIP_FLAGS="--break-system-packages"
else
PIP_FLAGS=""
fi
# Runtime dependencies. Keep this list free of development headers so
# small devices such as the 512-MB Pi Zero 2 W can be deployed without
# pulling in a full multimedia build environment.
runtime_packages=(
python3-pip \
python3-websockets \
python3-cryptography \
python3-aiohttp \
git \
wget \
curl \
v4l-utils \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-tools \
gstreamer1.0-alsa \
gstreamer1.0-gl \
gstreamer1.0-nice \
python3-gi \
python3-gi-cairo \
gir1.2-gstreamer-1.0 \
gir1.2-gst-plugins-base-1.0 \
gir1.2-gst-plugins-bad-1.0
)
development_packages=(
libgstreamer1.0-dev
libgstreamer-plugins-base1.0-dev
libgstreamer-plugins-bad1.0-dev
libcairo2-dev
libgirepository1.0-dev
)
desktop_runtime_packages=(
gstreamer1.0-x
gstreamer1.0-gtk3
gstreamer1.0-pulseaudio
)
sudo apt-get install -y "${runtime_packages[@]}"
if [ "$RUNTIME_ONLY" = false ]; then
sudo apt-get install -y "${desktop_runtime_packages[@]}"
sudo apt-get install -y "${development_packages[@]}"
fi
ensure_display_utilities
# Platform-specific packages
if [ "$IS_RASPBERRY_PI" = true ]; then
print_color "$YELLOW" "Installing Raspberry Pi specific packages..."
pi_packages=()
if apt-cache show raspi-utils-core &> /dev/null; then
# Raspberry Pi OS Trixie and newer.
pi_packages+=(raspi-utils-core)
apt-cache show raspi-utils-dt &> /dev/null && pi_packages+=(raspi-utils-dt)
else
# Raspberry Pi OS Bookworm/Bullseye and older.
apt-cache show libraspberrypi-bin &> /dev/null && pi_packages+=(libraspberrypi-bin)
if [ "$RUNTIME_ONLY" = false ] && apt-cache show libraspberrypi-dev &> /dev/null; then
pi_packages+=(libraspberrypi-dev)
fi
fi
apt-cache show rpicam-apps-lite &> /dev/null && pi_packages+=(rpicam-apps-lite)
if [ ${#pi_packages[@]} -gt 0 ]; then
sudo apt-get install -y "${pi_packages[@]}"
else
print_color "$YELLOW" "⚠ No Raspberry Pi utility package candidates found; continuing with detected system tools."
fi
# Add user to video group
sudo usermod -a -G video $USER
fi
if [ "$IS_JETSON" = true ]; then
print_color "$YELLOW" "Installing Jetson specific packages..."
sudo apt-get install -y \
nvidia-l4t-gstreamer \
deepstream-6.0 || true # Deepstream might not be needed
fi
# Python packages
print_color "$YELLOW" "Installing Python packages..."
missing_python_packages=()
python3 -c 'import websockets' 2>/dev/null || missing_python_packages+=(websockets)
python3 -c 'import cryptography' 2>/dev/null || missing_python_packages+=(cryptography)
python3 -c 'import aiohttp' 2>/dev/null || missing_python_packages+=(aiohttp)
if ! python3 -c 'import gi' 2>/dev/null; then
print_color "$RED" "✗ Python GObject bindings are unavailable after installing python3-gi."
print_color "$YELLOW" " Check that python3 and python3-gi come from the same OS package repository."
return 1
fi
if [ ${#missing_python_packages[@]} -eq 0 ]; then
print_color "$GREEN" "✓ Python packages installed successfully from the OS repository"
else
print_color "$YELLOW" "Missing Python packages: ${missing_python_packages[*]}"
# Fallback: Fix broken pip installations
print_color "$YELLOW" "Attempting a pip fallback..."
# Remove broken pip symlinks if they exist
if [ -L /usr/local/bin/pip3 ] && [ ! -e /usr/local/bin/pip3 ]; then
print_color "$YELLOW" "Removing broken pip3 symlink..."
sudo rm -f /usr/local/bin/pip3
fi
if [ -L /usr/local/bin/pip ] && [ ! -e /usr/local/bin/pip ]; then
print_color "$YELLOW" "Removing broken pip symlink..."
sudo rm -f /usr/local/bin/pip
fi
# Ensure python3-pip is installed
if ! dpkg -l python3-pip &> /dev/null; then
print_color "$YELLOW" "Installing python3-pip package..."
sudo apt-get install -y python3-pip
fi
# Find the correct pip command
PIP_CMD=""
# First try python3 -m pip
if python3 -m pip --version &> /dev/null; then
PIP_CMD="python3 -m pip"
print_color "$GREEN" "✓ Using python3 -m pip"
# Then try direct pip3 command
elif command -v pip3 &> /dev/null && pip3 --version &> /dev/null; then
PIP_CMD="pip3"
print_color "$GREEN" "✓ Using pip3"
# Try /usr/bin/pip3
elif [ -x /usr/bin/pip3 ] && /usr/bin/pip3 --version &> /dev/null; then
PIP_CMD="/usr/bin/pip3"
print_color "$GREEN" "✓ Using /usr/bin/pip3"
else
# Last resort - bootstrap pip
print_color "$YELLOW" "Bootstrapping pip..."
curl -sS https://bootstrap.pypa.io/get-pip.py | python3 || true
if python3 -m pip --version &> /dev/null; then
PIP_CMD="python3 -m pip"
print_color "$GREEN" "✓ pip bootstrapped successfully"
else
print_color "$RED" "✗ Failed to install pip. Please install manually."
return 1
fi
fi
# Ensure pip is up to date
print_color "$YELLOW" "Updating pip..."
$PIP_CMD install --upgrade pip $PIP_FLAGS || true
# Install Python packages with the fixed pip
print_color "$YELLOW" "Installing Python packages..."
$PIP_CMD install $PIP_FLAGS "${missing_python_packages[@]}"
fi
else
print_color "$RED" "✗ Unsupported package manager. Please install dependencies manually."
return 1
fi
print_color "$GREEN" "✓ Dependencies installed"
}
# Detect available cameras
detect_cameras() {
print_color "$YELLOW" "Detecting available cameras..."
CAMERAS=()
# Check for V4L2 devices
if [ -d /dev ]; then
for device in /dev/video*; do
if [ -e "$device" ]; then
if command -v v4l2-ctl &> /dev/null; then
info=$(v4l2-ctl -d "$device" --all 2>/dev/null || sudo -n v4l2-ctl -d "$device" --all 2>/dev/null || true)
# Memory-to-memory codec and ISP nodes also appear as
# /dev/video*. Only list devices with a standalone capture
# capability as cameras.
if ! grep -Eq '^[[:space:]]*Video Capture( Multiplanar)?[[:space:]]*$' <<< "$info"; then
continue
fi
driver=$(grep "Driver name" <<< "$info" | head -n 1 | cut -d: -f2- | xargs)
if [ "$IS_RASPBERRY_PI" = true ] && [[ "$driver" == "bcm2835-isp" || "$driver" == "bcm2835-codec" ]]; then
continue
fi
name=$(grep "Card type" <<< "$info" | head -n 1 | cut -d: -f2- | xargs)
CAMERAS+=("$device${name:+ - $name}")
else
CAMERAS+=("$device")
fi
fi
done
fi
# Check for Raspberry Pi camera
if [ "$IS_RASPBERRY_PI" = true ]; then
if command -v rpicam-hello &> /dev/null; then
camera_list=$(rpicam-hello --list-cameras 2>&1 || true)
if grep -Eq '^[[:space:]]*[0-9]+[[:space:]]*:' <<< "$camera_list"; then
CAMERAS+=("rpicam - Raspberry Pi Camera Module")
fi
elif command -v libcamera-hello &> /dev/null; then
camera_list=$(libcamera-hello --list-cameras 2>&1 || true)
if grep -Eq '^[[:space:]]*[0-9]+[[:space:]]*:' <<< "$camera_list"; then
CAMERAS+=("libcamera - Raspberry Pi Camera Module")
fi
elif command -v raspistill &> /dev/null; then
if vcgencmd get_camera &> /dev/null | grep -q "detected=1"; then
CAMERAS+=("raspicam - Legacy Raspberry Pi Camera")
fi
fi
fi
if [ ${#CAMERAS[@]} -eq 0 ]; then
print_color "$YELLOW" "No cameras detected. You can still use test sources."
else
print_color "$GREEN" "✓ Found ${#CAMERAS[@]} camera(s):"
for cam in "${CAMERAS[@]}"; do
echo " - $cam"
done
fi
echo
}
# Create configuration
create_config() {
print_color "$YELLOW" "Setting up configuration..."
# Config file is now in the script directory, no need to create a separate folder
# Check for non-interactive mode
if [ "$NON_INTERACTIVE" = true ]; then
# In non-interactive mode, do basic installation only
install_type="1"
print_color "$YELLOW" "Non-interactive mode: Performing basic installation only"
print_color "$GREEN" "✓ Dependencies installed. You can now run:"
echo " python3 publish.py --help"
return
fi
# Ask what the user wants to do
echo
print_color "$BLUE" "=== Installation Type ==="
echo
print_color "$YELLOW" "What would you like to do?"
echo "1) Basic installation (just install dependencies)"
echo "2) Configure for manual use (install + create config file)"
echo "3) Full setup with auto-start (install + config + boot service)"
safe_read "Choice [1-3]: " "" "1"
install_type="$REPLY"
case $install_type in
1)
# Basic installation - skip configuration
print_color "$GREEN" "✓ Dependencies installed. You can now run:"
echo " python3 publish.py --help"
return
;;
2)
# Manual configuration
SETUP_AUTOSTART=false
;;
3)
# Full setup with auto-start
SETUP_AUTOSTART=true
;;
*)
# Default to basic
print_color "$GREEN" "✓ Dependencies installed. You can now run:"
echo " python3 publish.py --help"
return
;;
esac
# Interactive setup for options 2 and 3
echo
print_color "$BLUE" "=== Raspberry Ninja Configuration ==="
echo
# Stream ID
safe_read "Enter your stream ID (leave empty for random): " "" ""
STREAM_ID="$REPLY"
# Room name
safe_read "Enter room name (optional): " "" ""
ROOM_NAME="$REPLY"
# Server
print_color "$YELLOW" "Select handshake server:"
echo "1) wss://wss.vdo.ninja:443 (Primary server - recommended)"
echo "2) wss://apibackup.vdo.ninja:443 (Backup server)"
echo "3) Custom server"
safe_read "Choice [1-3]: " "" "1"
server_choice="$REPLY"
case $server_choice in
2) SERVER="wss://apibackup.vdo.ninja:443" ;;
3)
echo "Enter custom handshake server URL"
echo "Format: wss://your-server.com:443"
safe_read "Server URL: " "" "wss://wss.vdo.ninja:443"
SERVER="$REPLY"
;;
*) SERVER="wss://wss.vdo.ninja:443" ;;
esac
# Ask about advanced settings
safe_read "Configure advanced video settings? (y/N): " "-n 1 -r" "N"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Video settings
safe_read "Video bitrate in kbps [2000]: " "" "2000"
BITRATE="${REPLY:-2000}"
safe_read "Video width [1280]: " "" "1280"
WIDTH="${REPLY:-1280}"
safe_read "Video height [720]: " "" "720"
HEIGHT="${REPLY:-720}"
safe_read "Framerate [30]: " "" "30"
FRAMERATE="${REPLY:-30}"
else
# Use defaults
BITRATE=2000
WIDTH=1280
HEIGHT=720
FRAMERATE=30
fi
# Camera selection
print_color "$YELLOW" "Select video source:"
echo "1) Test source (recommended for first run)"
echo "2) USB camera (/dev/video0)"
echo "3) Raspberry Pi camera (libcamera)"
echo "4) Custom pipeline"
safe_read "Choice [1-4]: " "" "1"
camera_choice="$REPLY"
case $camera_choice in
2) VIDEO_SOURCE="v4l2" ;;
3) VIDEO_SOURCE="libcamera" ;;
4)
safe_read "Enter custom video pipeline: " "" ""
CUSTOM_VIDEO_PIPELINE="$REPLY"
VIDEO_SOURCE="custom"
;;
*) VIDEO_SOURCE="test" ;;
esac
# Audio settings
safe_read "Enable audio? (Y/n): " "-n 1 -r" "Y"
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
AUDIO_ENABLED="false"
else
AUDIO_ENABLED="true"
fi
# Create config file
cat > "$CONFIG_FILE" << EOF
{
"stream_id": "$STREAM_ID",
"room": "$ROOM_NAME",
"server": "$SERVER",
"bitrate": $BITRATE,
"width": $WIDTH,
"height": $HEIGHT,
"framerate": $FRAMERATE,
"video_source": "$VIDEO_SOURCE",
"custom_video_pipeline": "$CUSTOM_VIDEO_PIPELINE",
"audio_enabled": $AUDIO_ENABLED,
"platform": "$PLATFORM",
"auto_start": false
}
EOF
print_color "$GREEN" "✓ Configuration saved to $CONFIG_FILE"
echo
}
# Setup auto-start service
setup_autostart() {
# Skip if user chose option 2 (manual configuration only)
if [ "$SETUP_AUTOSTART" = false ]; then
return
fi
print_color "$YELLOW" "Setting up auto-start service..."
# For option 3, ask for confirmation
safe_read "Enable auto-start on boot? (Y/n): " "-n 1 -r" "Y"
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
# Verify config file exists
if [ ! -f "$CONFIG_FILE" ]; then
print_color "$RED" "✗ Config file not found at: $CONFIG_FILE"
print_color "$RED" " Cannot create service without configuration"
return
fi
# Create systemd service
sudo tee /etc/systemd/system/$SERVICE_NAME.service > /dev/null << EOF
[Unit]
Description=Raspberry Ninja Streaming Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$SCRIPT_DIR
ExecStart=/usr/bin/python3 $SCRIPT_DIR/publish.py --config $CONFIG_FILE
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
# Enable and start service
sudo systemctl daemon-reload
sudo systemctl enable $SERVICE_NAME.service
print_color "$GREEN" "✓ Auto-start service created and enabled"
# Print service information
echo
print_color "$BLUE" "=== Service Information ==="
print_color "$YELLOW" "Service name: $SERVICE_NAME"
print_color "$YELLOW" "Service file: /etc/systemd/system/$SERVICE_NAME.service"
print_color "$YELLOW" "Config file: $CONFIG_FILE"
print_color "$YELLOW" "Working directory: $SCRIPT_DIR"
echo
safe_read "Start the service now? (Y/n): " "-n 1 -r" "Y"
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
if sudo systemctl start $SERVICE_NAME.service; then
print_color "$GREEN" "✓ Service started successfully"
# Wait a moment for service to initialize
sleep 2
# Check if service is running
if systemctl is-active --quiet $SERVICE_NAME.service; then
print_color "$GREEN" "✓ Service is running"
else
print_color "$RED" "⚠ Service may have failed to start"
print_color "$YELLOW" "Check logs with: sudo journalctl -u $SERVICE_NAME -n 50"
fi
else
print_color "$RED" "✗ Failed to start service"
fi
echo
fi
# Print management commands
print_color "$BLUE" "=== Service Management Commands ==="
print_color "$YELLOW" "View status: sudo systemctl status $SERVICE_NAME"
print_color "$YELLOW" "Stop service: sudo systemctl stop $SERVICE_NAME"
print_color "$YELLOW" "Start service: sudo systemctl start $SERVICE_NAME"
print_color "$YELLOW" "Restart: sudo systemctl restart $SERVICE_NAME"
print_color "$YELLOW" "View logs: sudo journalctl -u $SERVICE_NAME -f"
print_color "$YELLOW" "Disable boot: sudo systemctl disable $SERVICE_NAME"
print_color "$YELLOW" "Enable boot: sudo systemctl enable $SERVICE_NAME"
# Update config to reflect auto-start
if command -v jq &> /dev/null; then
jq '.auto_start = true' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
fi
fi
echo
}
# Test installation
test_installation() {
# Skip test for basic installation
if [ "$install_type" = "1" ]; then
return
fi
print_color "$YELLOW" "Testing installation..."
safe_read "Run a test stream? (Y/n): " "-n 1 -r" "Y"