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
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
#!/bin/bash

set -e

echo "πŸš€ Installing G1 Deploy system dependencies..."
sudo apt-get update && sudo apt-get install -y libgtest-dev

# Detect system type and architecture
ARCH=$(uname -m)
OS_ID=$(lsb_release -si 2>/dev/null || echo "Unknown")
OS_VERSION=$(lsb_release -sr 2>/dev/null || echo "Unknown")

# Common CUDA installation paths (used by multiple functions)
CUDA_PATHS=(
    "/usr/local/cuda"
    "/usr/local/cuda-12.6"  # CUDA 12.6 on newer Jetson
    "/usr/local/cuda-12.5"  # CUDA 12.5 variant  
    "/usr/local/cuda-12.4"  # CUDA 12.4 variant
    "/usr/local/cuda-12"    # Generic CUDA 12.x
    "/usr/local/cuda-11.4"  # Common on older Jetson
    "/usr/local/cuda-10.2"  # Common on older Jetson
    "/usr/local/cuda-11"
    "/usr/local/cuda-10"
    "/opt/cuda"
    "/usr/cuda"
)

# Common CUDA runtime library paths (used by multiple functions)
CUDA_RUNTIME_PATHS=(
    "/usr/local/cuda/lib64/libcudart.so"
    "/usr/local/cuda-12.6/lib64/libcudart.so"
    "/usr/local/cuda-12.5/lib64/libcudart.so"
    "/usr/local/cuda-12.4/lib64/libcudart.so"
    "/usr/local/cuda-12/lib64/libcudart.so"
    "/usr/local/cuda-11.4/lib64/libcudart.so"
    "/usr/local/cuda-10.2/lib64/libcudart.so"
    "/usr/lib/aarch64-linux-gnu/libcudart.so"
    "/usr/lib/x86_64-linux-gnu/libcudart.so" 
    "/usr/lib64/libcudart.so"
    "/usr/lib/libcudart.so"
)

# Common CUDA header paths (used by multiple functions)
CUDA_HEADER_PATHS=(
    "/usr/local/cuda/include/cuda_runtime.h"
    "/usr/local/cuda-12.6/include/cuda_runtime.h"
    "/usr/local/cuda-12.5/include/cuda_runtime.h"
    "/usr/local/cuda-12.4/include/cuda_runtime.h"
    "/usr/local/cuda-12/include/cuda_runtime.h"
    "/usr/local/cuda-11.4/include/cuda_runtime.h"
    "/usr/local/cuda-10.2/include/cuda_runtime.h"
    "/usr/local/cuda-11/include/cuda_runtime.h"
    "/usr/include/cuda_runtime.h"
    "/usr/include/cuda/cuda_runtime.h"
)

# Common utility functions
has_cuda_in_ldconfig() {
    ldconfig -p 2>/dev/null | grep -q "libcudart\|libcuda"
}

# Update package cache (with deduplication)
update_package_cache() {
    if [ "$PACKAGE_MANAGER" = "apt" ] && [ "$APT_UPDATED" != "true" ]; then
        sudo apt-get update
        export APT_UPDATED="true"
    fi
}

# Enhanced Jetson detection
IS_JETSON=false
JETSON_MODEL=""

if [[ "$ARCH" == "aarch64" ]]; then
    # Check for Jetson-specific indicators
    if [[ -f "/etc/nv_tegra_release" ]] || \
       [[ -d "/usr/src/jetson_multimedia_api" ]] || \
       ls /etc/apt/sources.list.d/ 2>/dev/null | grep -q jetson || \
       [[ -f "/proc/device-tree/model" && $(cat /proc/device-tree/model 2>/dev/null) =~ "Jetson" ]]; then
        IS_JETSON=true
        
        # Detect Jetson model for optimized package selection
        if [[ -f "/proc/device-tree/model" ]]; then
            JETSON_MODEL=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0' | sed 's/NVIDIA //')
            echo "πŸ€– Jetson system detected: $JETSON_MODEL"
        else
            echo "πŸ€– Jetson system detected (model unknown)"
        fi
        
        # Check JetPack version if available
        if command -v jetson_release &> /dev/null; then
            JETPACK_VERSION=$(jetson_release -v 2>/dev/null | grep "JETPACK" | cut -d' ' -f2 || echo "unknown")
            echo "πŸ“¦ JetPack version: $JETPACK_VERSION"
        fi
    fi
fi

echo "πŸ” System: $OS_ID $OS_VERSION ($ARCH)$([ "$IS_JETSON" = true ] && echo " - Jetson")"

# Detect the operating system and package manager
if command -v apt-get &> /dev/null; then
    PACKAGE_MANAGER="apt"
elif command -v yum &> /dev/null; then
    PACKAGE_MANAGER="yum"
elif command -v pacman &> /dev/null; then
    PACKAGE_MANAGER="pacman"
else
    echo "❌ Unsupported package manager. This script supports apt, yum, and pacman."
    exit 1
fi

# Function to install packages based on the package manager
install_packages() {
    case $PACKAGE_MANAGER in
        apt)
            update_package_cache
            sudo apt-get install -y "$@"
            ;;
        yum)
            sudo yum install -y "$@"
            ;;
        pacman)
            sudo pacman -S --noconfirm "$@"
            ;;
    esac
}

echo "πŸ“¦ Installing base development tools..."
case $PACKAGE_MANAGER in
    apt)
        if [ "$IS_JETSON" = true ]; then
            echo "πŸ€– Installing packages optimized for Jetson system..."
            # Jetson systems may have some tools pre-installed
            JETSON_PACKAGES="build-essential clang cmake git git-lfs pkg-config patchelf zlib1g-dev curl wget"
            
            # Add optional packages if available
            if apt-cache show cmake-format &>/dev/null; then
                JETSON_PACKAGES="$JETSON_PACKAGES cmake-format"
            fi
            if apt-cache show cppcheck &>/dev/null; then
                JETSON_PACKAGES="$JETSON_PACKAGES cppcheck"
            fi
            
            install_packages $JETSON_PACKAGES
        else
        install_packages \
            build-essential \
            clang \
            cmake \
            cmake-format \
            cppcheck \
            git \
            git-lfs \
            pkg-config \
            patchelf \
            zlib1g-dev \
            curl \
            wget
        fi
        ;;
    yum)
        install_packages \
            gcc \
            gcc-c++ \
            clang \
            cmake \
            cppcheck \
            git \
            git-lfs \
            pkgconfig \
            patchelf \
            zlib-devel \
            curl \
            wget
        ;;
    pacman)
        install_packages \
            base-devel \
            clang \
            cmake \
            cppcheck \
            git \
            git-lfs \
            pkgconf \
            patchelf \
            zlib \
            curl \
            wget
        ;;
esac

echo "πŸ“š Installing C++ libraries..."
case $PACKAGE_MANAGER in
    apt)
        install_packages \
            libyaml-cpp-dev \
            libeigen3-dev \
            libmsgpack-dev \
            libzmq3-dev \
            nlohmann-json3-dev \
            libgtest-dev
        ;;
    yum)
        install_packages \
            yaml-cpp-devel \
            eigen3-devel \
            msgpack-devel \
            zeromq-devel \
            cppzmq-devel \
            nlohmann-json-devel \
            gtest-devel
        ;;
    pacman)
        install_packages \
            yaml-cpp \
            eigen \
            msgpack-cxx \
            zeromq \
            cppzmq \
            nlohmann-json \
            gtest
        ;;
esac

# Ensure ZeroMQ C++ headers (cppzmq) are available.
# On some Ubuntu releases (e.g., 24.04), the 'cppzmq' or 'libcppzmq-dev' package may be missing.
# If not found, vendor the header-only repo to third_party/cppzmq.
echo "🧩 Ensuring ZeroMQ C++ headers (cppzmq)..."

# Resolve repository root relative to this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
THIRD_PARTY_DIR="$REPO_ROOT/third_party"

have_cppzmq_header=false
if [ -f "/usr/include/zmq.hpp" ]; then
    have_cppzmq_header=true
fi

if [ "$have_cppzmq_header" = false ]; then
    if [ "$PACKAGE_MANAGER" = "apt" ]; then
        # Try distro packages if available
        if apt-cache policy cppzmq 2>/dev/null | grep -q "Candidate:" && \
           ! apt-cache policy cppzmq 2>/dev/null | grep -q "(none)"; then
            update_package_cache
            if sudo apt-get install -y cppzmq; then
                have_cppzmq_header=true
            fi
        elif apt-cache policy libcppzmq-dev 2>/dev/null | grep -q "Candidate:" && \
             ! apt-cache policy libcppzmq-dev 2>/dev/null | grep -q "(none)"; then
            update_package_cache
            if sudo apt-get install -y libcppzmq-dev; then
                have_cppzmq_header=true
            fi
        fi
    fi
fi

if [ "$have_cppzmq_header" = false ]; then
    echo "πŸ“¦ Vendoring header-only cppzmq into third_party..."
    mkdir -p "$THIRD_PARTY_DIR"
    if [ ! -d "$THIRD_PARTY_DIR/cppzmq/.git" ]; then
        if git clone --depth 1 https://github.com/zeromq/cppzmq.git "$THIRD_PARTY_DIR/cppzmq"; then
            echo "βœ… cppzmq headers cloned to $THIRD_PARTY_DIR/cppzmq"
            echo "   Add include path: -I$THIRD_PARTY_DIR/cppzmq"
        else
            echo "❌ Failed to clone cppzmq repo. Please install headers manually."
        fi
    else
        if git -C "$THIRD_PARTY_DIR/cppzmq" pull --ff-only; then
            echo "βœ… cppzmq headers updated in $THIRD_PARTY_DIR/cppzmq"
        else
            echo "⚠️  Failed to update cppzmq headers. Using existing copy."
        fi
    fi
else
    echo "βœ… ZeroMQ C++ headers found in system includes"
fi

# Ensure JSON headers (nlohmann/json) are available (similar to cppzmq ensure)
echo "🧩 Ensuring JSON headers (nlohmann/json)..."

have_json_header=false
if [ -f "/usr/include/nlohmann/json.hpp" ] || [ -f "/usr/local/include/nlohmann/json.hpp" ]; then
    have_json_header=true
fi

if [ "$have_json_header" = false ]; then
    case $PACKAGE_MANAGER in
        apt)
            update_package_cache
            sudo apt-get install -y nlohmann-json3-dev || true
            ;;
        yum)
            sudo yum install -y nlohmann-json-devel || true
            ;;
        pacman)
            sudo pacman -S --noconfirm nlohmann-json || true
            ;;
    esac
    if [ -f "/usr/include/nlohmann/json.hpp" ] || [ -f "/usr/local/include/nlohmann/json.hpp" ]; then
        have_json_header=true
    fi
fi

if [ "$have_json_header" = false ]; then
    echo "πŸ“¦ Vendoring nlohmann/json into third_party..."
    # Reuse THIRD_PARTY_DIR from earlier cppzmq ensure block if set; otherwise define
    if [ -z "$THIRD_PARTY_DIR" ]; then
        SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
        REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
        THIRD_PARTY_DIR="$REPO_ROOT/third_party"
    fi
    mkdir -p "$THIRD_PARTY_DIR"
    if [ ! -d "$THIRD_PARTY_DIR/nlohmann_json/.git" ]; then
        if git clone --depth 1 https://github.com/nlohmann/json.git "$THIRD_PARTY_DIR/nlohmann_json"; then
            echo "βœ… nlohmann/json cloned to $THIRD_PARTY_DIR/nlohmann_json"
            echo "   Add include path: -I$THIRD_PARTY_DIR/nlohmann_json/single_include"
        else
            echo "❌ Failed to clone nlohmann/json. Please install headers manually."
        fi
    else
        if git -C "$THIRD_PARTY_DIR/nlohmann_json" pull --ff-only; then
            echo "βœ… nlohmann/json updated in $THIRD_PARTY_DIR/nlohmann_json"
        else
            echo "⚠️  Failed to update nlohmann/json. Using existing copy."
        fi
    fi
else
    echo "βœ… JSON headers found in system includes"
fi

# Install Just command runner
echo "⚑ Installing Just command runner..."
if ! command -v just &> /dev/null; then
    case $PACKAGE_MANAGER in
        apt|yum)
            # Install Just from GitHub releases for Ubuntu/Debian and RHEL/CentOS
            JUST_VERSION="1.43.0"
            JUST_ARCH=$(uname -m)
            
            # Map architecture names to just release naming
            case $JUST_ARCH in
                x86_64)
                    JUST_ARCH_NAME="x86_64-unknown-linux-musl"
                    ;;
                aarch64|arm64)
                    JUST_ARCH_NAME="aarch64-unknown-linux-musl"
                    ;;
                armv7l)
                    JUST_ARCH_NAME="armv7-unknown-linux-musleabihf"
                    ;;
                arm*)
                    JUST_ARCH_NAME="arm-unknown-linux-musleabihf"
                    ;;
                *)
                    echo "❌ Unsupported architecture for just: $JUST_ARCH"
                    echo "   Supported architectures: x86_64, aarch64, armv7l, arm"
                    exit 1
                    ;;
            esac
            
            echo "πŸ“₯ Downloading Just ${JUST_VERSION} for ${JUST_ARCH}..."
            JUST_URL="https://github.com/casey/just/releases/download/${JUST_VERSION}/just-${JUST_VERSION}-${JUST_ARCH_NAME}.tar.gz"
            curl -L "$JUST_URL" | tar xz -C /tmp
            sudo mv /tmp/just /usr/local/bin/
            ;;
        pacman)
            install_packages just
            ;;
    esac
else
    echo "βœ… Just is already installed"
fi

# Install ONNX Runtime
echo "🧠 Setting up ONNX Runtime..."

# Allow configurable installation path
ONNX_INSTALL_PATH="${ONNX_INSTALL_PATH:-/opt/onnxruntime}"

if [ ! -d "$ONNX_INSTALL_PATH" ]; then
    ONNX_VERSION="1.16.3"
    ONNX_ARCH=$(uname -m)
    
    if [ "$ONNX_ARCH" = "x86_64" ]; then
        ONNX_URL="https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-linux-x64-${ONNX_VERSION}.tgz"
    elif [ "$ONNX_ARCH" = "aarch64" ]; then
        # Same URL for all ARM64 systems (Jetson and regular ARM64)
        ONNX_URL="https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-linux-aarch64-${ONNX_VERSION}.tgz"
        if [ "$IS_JETSON" = true ]; then
            echo "πŸ€– Jetson system detected - using ARM64 ONNX Runtime..."
        fi
    else
        echo "❌ Unsupported architecture: $ONNX_ARCH"
        exit 1
    fi
    
    echo "πŸ“₯ Downloading ONNX Runtime ${ONNX_VERSION} for ${ONNX_ARCH}$([ "$IS_JETSON" = true ] && echo " (Jetson-optimized)")..."
    cd /tmp
    
    # Download with retry for Jetson systems (sometimes have slower network)
    if [ "$IS_JETSON" = true ]; then
        curl -L --retry 3 --retry-delay 2 "$ONNX_URL" -o onnxruntime.tgz
    else
    curl -L "$ONNX_URL" -o onnxruntime.tgz
    fi
    
    tar xzf onnxruntime.tgz
    
    # Move to configured location (may need sudo for system paths)
    ONNX_DIR=$(ls -d onnxruntime-linux-* | head -n1)
    
    if [[ "$ONNX_INSTALL_PATH" =~ ^(/opt|/usr) ]]; then
        # System path - needs sudo
        sudo mv "$ONNX_DIR" "$ONNX_INSTALL_PATH"
        sudo ln -sf "$ONNX_INSTALL_PATH/lib/libonnxruntime.so" /usr/local/lib/ 2>/dev/null || true
        sudo ln -sf "$ONNX_INSTALL_PATH/include" /usr/local/include/onnxruntime 2>/dev/null || true
        echo "$ONNX_INSTALL_PATH/lib" | sudo tee /etc/ld.so.conf.d/onnxruntime.conf
        sudo ldconfig
    else
        # User path - no sudo needed
        mv "$ONNX_DIR" "$ONNX_INSTALL_PATH"
        # Create user-accessible symlinks if possible
        if [ -w /usr/local/lib ]; then
            ln -sf "$ONNX_INSTALL_PATH/lib/libonnxruntime.so" /usr/local/lib/ 2>/dev/null || true
        fi
    fi
    
    # For Jetson systems, add to environment
    if [ "$IS_JETSON" = true ]; then
        echo "# ONNX Runtime for Jetson" | sudo tee -a /etc/environment
        echo "LD_LIBRARY_PATH=\"$ONNX_INSTALL_PATH/lib:\$LD_LIBRARY_PATH\"" | sudo tee -a /etc/environment
    fi
    
    echo "βœ… ONNX Runtime installed to $ONNX_INSTALL_PATH$([ "$IS_JETSON" = true ] && echo " with Jetson optimizations")"
else
    echo "βœ… ONNX Runtime is already installed at $ONNX_INSTALL_PATH"
fi

echo "πŸ’‘ To install to a different location, set ONNX_INSTALL_PATH environment variable"

# Function to check for CUDA toolkit (nvcc compiler) - silent check
check_cuda_toolkit() {
    # Check for nvcc in PATH
    if command -v nvcc &> /dev/null; then
        return 0
    fi
    
    # Check common CUDA installation paths (using shared variable)
    
    for cuda_path in "${CUDA_PATHS[@]}"; do
        if [ -f "$cuda_path/bin/nvcc" ]; then
            return 0
        fi
    done
    
    return 1
}

# Function to check for CUDA runtime libraries AND headers (needed for compilation)
check_cuda_runtime() {
    # Check for CUDA runtime libraries
    local has_libraries=false
    if has_cuda_in_ldconfig; then
        has_libraries=true
    else
        # Check specific paths for runtime libraries (using shared variable)
        for lib_path in "${CUDA_RUNTIME_PATHS[@]}"; do
            if [ -f "$lib_path" ]; then
                has_libraries=true
                break
            fi
        done
    fi
    
    # Check for CUDA headers (essential for compilation) - using shared variable
    local has_headers=false
    for header_path in "${CUDA_HEADER_PATHS[@]}"; do
        if [ -f "$header_path" ]; then
            has_headers=true
            break
        fi
    done
    
    # Need both libraries and headers
    if [ "$has_libraries" = true ] && [ "$has_headers" = true ]; then
        return 0
    else
        return 1
    fi
}

# Check what CUDA components we have and provide clear, non-confusing output
echo "πŸ” Checking CUDA installation..."


# Function to handle non-Jetson ARM64 systems  
install_cuda_for_arm64() {
    echo "πŸ”§ Installing CUDA packages for ARM64 system..."
    
    if sudo apt-get install -y cuda-cudart-dev-12-2 cuda-runtime-12-2 2>/dev/null || \
       sudo apt-get install -y cuda-cudart-dev-11-8 cuda-runtime-11-8 2>/dev/null || \
       sudo apt-get install -y libcudart-dev nvidia-cuda-runtime-cu12 2>/dev/null; then
        echo "βœ… CUDA runtime and development packages installed for ARM64!"
        return 0
    else
        echo "❌ Failed to install CUDA packages for ARM64 system"
        return 1
    fi
}

# Function to handle x86_64 systems
install_cuda_for_x86_64() {
    echo "πŸ”§ Installing CUDA packages for x86_64 system..."
    
    # Detect CUDA version from nvidia-smi if available
    local DETECTED_CUDA_VERSION=""
    if command -v nvidia-smi &> /dev/null; then
        DETECTED_CUDA_VERSION=$(nvidia-smi | grep "CUDA Version:" | sed 's/.*CUDA Version: \([0-9]\+\.[0-9]\+\).*/\1/' | head -n1)
        if [ -n "$DETECTED_CUDA_VERSION" ]; then
            echo "πŸ“ Detected CUDA $DETECTED_CUDA_VERSION from nvidia-smi"
        fi
    fi
    
    # Check if NVIDIA CUDA repository is configured
    local has_cuda_repo=false
    if [ -f /etc/apt/sources.list.d/cuda*.list ] || \
       grep -r "developer.download.nvidia.com/compute/cuda" /etc/apt/sources.list* &>/dev/null; then
        has_cuda_repo=true
    fi
    
    # Add NVIDIA CUDA repository if not present
    if [ "$has_cuda_repo" = false ]; then
        echo "πŸ“¦ NVIDIA CUDA repository not found, adding it now..."
        
        # Detect Ubuntu version for correct repository
        local ubuntu_version=""
        if [ "$OS_ID" = "Ubuntu" ]; then
            case "$OS_VERSION" in
                22.04)
                    ubuntu_version="ubuntu2204"
                    ;;
                20.04)
                    ubuntu_version="ubuntu2004"
                    ;;
                24.04)
                    ubuntu_version="ubuntu2404"
                    ;;
                *)
                    # Default to 22.04 for unknown versions
                    ubuntu_version="ubuntu2204"
                    echo "⚠️  Ubuntu $OS_VERSION detected, using ubuntu2204 repository (may need adjustment)"
                    ;;
            esac
        else
            ubuntu_version="ubuntu2204"
            echo "⚠️  Non-Ubuntu system detected, using ubuntu2204 repository (may not work)"
        fi
        
        # Download and install CUDA keyring
        local cuda_keyring_url="https://developer.download.nvidia.com/compute/cuda/repos/${ubuntu_version}/x86_64/cuda-keyring_1.1-1_all.deb"
        
        if [ -f /tmp/cuda-keyring.deb ]; then
            rm /tmp/cuda-keyring.deb
        fi
        
        echo "πŸ“₯ Downloading CUDA repository keyring for $ubuntu_version..."
        if wget -q "$cuda_keyring_url" -O /tmp/cuda-keyring.deb 2>/dev/null; then
            if sudo dpkg -i /tmp/cuda-keyring.deb 2>/dev/null; then
                echo "βœ… NVIDIA CUDA repository keyring installed"
                # Force apt-get update after adding new repository
                echo "πŸ”„ Updating package cache with new CUDA repository..."
                sudo apt-get update
                has_cuda_repo=true
            else
                echo "⚠️  Failed to install CUDA keyring"
            fi
            rm -f /tmp/cuda-keyring.deb
        else
            echo "⚠️  Failed to download CUDA keyring from $cuda_keyring_url"
        fi
        
        # If keyring install failed, provide manual instructions
        if [ "$has_cuda_repo" = false ]; then
            echo ""
            echo "⚠️  Automatic CUDA repository setup failed. Please add it manually:"
            echo "   wget https://developer.download.nvidia.com/compute/cuda/repos/${ubuntu_version}/x86_64/cuda-keyring_1.1-1_all.deb"
            echo "   sudo dpkg -i cuda-keyring_1.1-1_all.deb"
            echo "   sudo apt-get update"
            echo ""
        fi
    else
        echo "βœ… NVIDIA CUDA repository already configured"
    fi
    
    # Try to install CUDA packages - prioritize detected version
    local cuda_installed=false
    
    if [ -n "$DETECTED_CUDA_VERSION" ]; then
        # Convert version like "12.4" to package suffix like "12-4"
        local version_suffix=$(echo "$DETECTED_CUDA_VERSION" | sed 's/\./-/')
        echo "🎯 Attempting to install CUDA $DETECTED_CUDA_VERSION packages..."
        
        # IMPORTANT: We need BOTH cuda-compiler (for complete headers) AND cuda-cudart-dev (for runtime)
        # Try to install both together first
        echo "πŸ“¦ Installing cuda-compiler-${version_suffix} and cuda-cudart-dev-${version_suffix}..."
        if sudo apt-get install -y cuda-compiler-${version_suffix} cuda-cudart-dev-${version_suffix}; then
            echo "βœ… CUDA $DETECTED_CUDA_VERSION compiler and runtime development packages installed!"
            cuda_installed=true
        else
            # If combined install fails, try separately
            echo "⚠️  Combined install failed, trying packages separately..."
            local compiler_installed=false
            local cudart_installed=false
            
            if sudo apt-get install -y cuda-compiler-${version_suffix}; then
                echo "βœ… cuda-compiler-${version_suffix} installed"
                compiler_installed=true
            else
                echo "❌ Failed to install cuda-compiler-${version_suffix}"
            fi
            
            if sudo apt-get install -y cuda-cudart-dev-${version_suffix}; then
                echo "βœ… cuda-cudart-dev-${version_suffix} installed"
                cudart_installed=true
            else
                echo "❌ Failed to install cuda-cudart-dev-${version_suffix}"
            fi
            
            if [ "$compiler_installed" = true ] || [ "$cudart_installed" = true ]; then
                if [ "$compiler_installed" = false ]; then
                    echo "⚠️  Warning: cuda-compiler not installed - headers may be incomplete!"
                fi
                if [ "$cudart_installed" = false ]; then
                    echo "⚠️  Warning: cuda-cudart-dev not installed - runtime dev files missing!"
                fi
                cuda_installed=true
            fi
        fi
        
        # If version-specific install failed, try toolkit
        if [ "$cuda_installed" = false ]; then
            echo "πŸ“¦ Trying cuda-toolkit-${version_suffix} as fallback..."
            if sudo apt-get install -y cuda-toolkit-${version_suffix}; then
                echo "βœ… CUDA $DETECTED_CUDA_VERSION toolkit installed!"
                cuda_installed=true
            fi
        fi
    fi
    
    # Fall back to trying various package names if version-specific install failed
    if [ "$cuda_installed" = false ]; then
        echo "πŸ”„ Trying generic CUDA package installation..."
        
        # List available CUDA packages for debugging
        echo "πŸ” Checking available CUDA packages..."
        local available_cuda_pkgs=$(apt-cache search "^cuda-cudart-dev" 2>/dev/null | head -5)
        if [ -n "$available_cuda_pkgs" ]; then
            echo "   Found packages:"
            echo "$available_cuda_pkgs" | sed 's/^/     /'
        fi
        
        # Try installing with specific version numbers first, then fall back to generic
        # Always try to get both compiler (for headers) and cudart-dev (for runtime)
        if sudo apt-get install -y cuda-compiler-12-4 cuda-cudart-dev-12-4; then
            echo "βœ… CUDA 12.4 compiler and runtime development installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-compiler-12-6 cuda-cudart-dev-12-6; then
            echo "βœ… CUDA 12.6 compiler and runtime development installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-compiler-12-2 cuda-cudart-dev-12-2; then
            echo "βœ… CUDA 12.2 compiler and runtime development installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-compiler-12-0 cuda-cudart-dev-12-0; then
            echo "βœ… CUDA 12.0 compiler and runtime development installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-toolkit-12-4; then
            echo "βœ… CUDA 12.4 toolkit installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-toolkit-12-6; then
            echo "βœ… CUDA 12.6 toolkit installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda-toolkit; then
            echo "βœ… CUDA toolkit (latest) installed!"
            cuda_installed=true
        elif sudo apt-get install -y cuda; then
            echo "βœ… CUDA (full package) installed!"
            cuda_installed=true
        fi
    fi
    
    if [ "$cuda_installed" = true ]; then
        return 0
    else
        echo "❌ Failed to install CUDA packages for x86_64 system"
        echo "   πŸ’‘ You may need to manually install CUDA toolkit from:"
        echo "      https://developer.nvidia.com/cuda-downloads"
        return 1
    fi
}

# CUDA detection and installation - optimized for Jetson systems
install_cuda_for_jetson() {
    echo "πŸš€ Installing CUDA development packages for Jetson..."
    
    # First, try to install CUDA development headers for existing CUDA installation
    local success=false
    
    # Check if we can install from JetPack repositories (try newer versions first)
    if sudo apt-get install -y nvidia-jetpack-dev 2>/dev/null; then
        echo "βœ… JetPack development packages installed!"
        success=true
    elif sudo apt-get install -y cuda-toolkit-12-6-dev 2>/dev/null || \
         sudo apt-get install -y cuda-toolkit-12-5-dev 2>/dev/null || \
         sudo apt-get install -y cuda-toolkit-12-4-dev 2>/dev/null || \
         sudo apt-get install -y cuda-toolkit-11-4-dev 2>/dev/null || \
         sudo apt-get install -y cuda-toolkit-10-2-dev 2>/dev/null; then
        echo "βœ… CUDA toolkit development packages installed!"
        success=true
    elif sudo apt-get install -y cuda-compiler-12-6 cuda-cudart-dev-12-6 2>/dev/null || \
         sudo apt-get install -y cuda-compiler-12-5 cuda-cudart-dev-12-5 2>/dev/null || \
         sudo apt-get install -y cuda-compiler-12-4 cuda-cudart-dev-12-4 2>/dev/null || \
         sudo apt-get install -y cuda-compiler-11-4 cuda-cudart-dev-11-4 2>/dev/null || \
         sudo apt-get install -y cuda-compiler-10-2 cuda-cudart-dev-10-2 2>/dev/null; then
        echo "βœ… CUDA compiler and runtime dev packages installed!"
        success=true
    else
        # Try generic packages that might work
        echo "⚠️  Jetson-specific packages not found. Trying generic CUDA packages..."
        if sudo apt-get install -y cuda-cudart-dev libcuda1-dev 2>/dev/null || \
           sudo apt-get install -y cuda-toolkit 2>/dev/null; then
            echo "βœ… Generic CUDA development packages installed!"
            success=true
        fi
    fi
    
    # If all else fails, try to create symlinks to existing CUDA installation
    if [ "$success" = false ]; then
        echo "⚠️  Package installation failed. Trying to link existing CUDA installation..."
        
        # Find existing CUDA installation (using shared variable)
        for cuda_dir in "${CUDA_PATHS[@]}"; do
            if [ -d "$cuda_dir/include" ]; then
                echo "πŸ“ Found CUDA at $cuda_dir, creating development symlinks..."
                sudo mkdir -p /usr/include/cuda
                sudo ln -sf $cuda_dir/include/* /usr/include/cuda/ 2>/dev/null || true
                sudo ln -sf $cuda_dir/include/cuda_runtime.h /usr/include/ 2>/dev/null || true
                echo "βœ… CUDA headers linked successfully!"
                success=true
                break
            fi
        done
    fi
    
    return $([ "$success" = true ] && echo 0 || echo 1)
}

# Check CUDA installation status
if check_cuda_toolkit; then
    if command -v nvcc &> /dev/null; then
        CUDA_VERSION=$(nvcc --version | grep "release" | sed 's/.*release \([0-9]\+\.[0-9]\+\).*/\1/')
        echo "βœ… CUDA Toolkit found: nvcc version $CUDA_VERSION"
    else
        # Found in custom path - add to PATH suggestion (using shared variable)
        for cuda_path in "${CUDA_PATHS[@]}"; do
            if [ -f "$cuda_path/bin/nvcc" ]; then
                CUDA_VERSION=$($cuda_path/bin/nvcc --version | grep "release" | sed 's/.*release \([0-9]\+\.[0-9]\+\).*/\1/' 2>/dev/null || echo "unknown")
                echo "βœ… CUDA Toolkit found at: $cuda_path (version $CUDA_VERSION)"
                echo "   πŸ’‘ Consider adding $cuda_path/bin to your PATH"
                break
            fi
        done
    fi
    
    # Even with toolkit found, verify runtime libraries and headers are available
    echo "πŸ” Verifying CUDA runtime libraries and headers are complete..."
    if check_cuda_runtime; then
        echo "βœ… CUDA toolkit is complete with runtime libraries and headers!"
    else
        echo "⚠️  CUDA toolkit found but runtime libraries/headers missing!"
        
        if [ "$IS_JETSON" = true ]; then
            echo "πŸ€– Installing missing CUDA runtime development packages for Jetson..."
            if install_cuda_for_jetson; then
                echo "βœ… Jetson CUDA runtime packages installed successfully!"
            else
                echo "❌ Jetson CUDA runtime installation failed"
                echo "   πŸ’‘ System may already have working CUDA runtime"
            fi
        fi
    fi
elif check_cuda_runtime; then
    echo "βœ… CUDA runtime libraries and headers found (sufficient for this project!)"
    echo "   ℹ️  Full toolkit not detected, but runtime + headers are all you need"
else
    # CUDA components missing - install them
    echo "⚠️  CUDA components missing or incomplete. Installing CUDA runtime libraries..."
    
    if [ "$IS_JETSON" = true ]; then
        echo "πŸ€– Installing CUDA runtime development packages for Jetson..."
        update_package_cache
        
        # Install CUDA runtime development packages for Jetson
        if sudo apt install -y cuda-cudart-dev-12-6 cuda-headers-12-6 2>/dev/null; then
            echo "βœ… CUDA 12.6 development packages installed!"
        elif sudo apt install -y cuda-cudart-dev cuda-toolkit-12-6 2>/dev/null; then
            echo "βœ… CUDA development packages installed!"
        elif sudo apt install -y libcudart-dev cuda-toolkit 2>/dev/null; then
            echo "βœ… Generic CUDA development packages installed!"
        else
            echo "❌ Failed to install CUDA development packages"
            echo "   Try manually: sudo apt install -y cuda-cudart-dev-12-6"
        fi
    else
        # Non-Jetson installation logic (existing)
        if [[ "$ARCH" == "aarch64" ]]; then
            install_cuda_for_arm64
        elif [[ "$ARCH" == "x86_64" ]]; then
            install_cuda_for_x86_64
        fi
    fi
    
    # Verify CUDA is now available
    echo "πŸ” Verifying CUDA installation after package installation..."
    if check_cuda_runtime; then
        echo "βœ… CUDA runtime and headers now available!"
    else
        echo "❌ CUDA verification still failing - manual intervention may be needed"
    fi
fi

# Initialize Git LFS
echo "πŸ“ Setting up Git LFS..."
if git lfs install --force 2>/dev/null; then
    echo "βœ… Git LFS hooks installed successfully"
else
    echo "⚠️  Git LFS hook installation had issues, trying manual resolution..."
    # Try to update existing hooks
    if git lfs update --force 2>/dev/null; then
        echo "βœ… Git LFS hooks updated successfully"
    else
        echo "⚠️  Git LFS hooks may need manual attention"
        echo "   You can run 'git lfs update --force' later if needed"
    fi
fi

echo ""
echo "πŸŽ‰ System dependencies installation complete!"
echo ""

if [ "$IS_JETSON" = true ]; then
    echo "πŸ€– Jetson-specific setup completed!"
    echo ""
    echo "πŸ“‹ Next steps for Jetson system:"
    echo "   1. Ensure TensorRT is available (usually pre-installed with JetPack)"
    echo "   2. Run 'source scripts/setup_env.sh' to configure paths for Jetson"
    echo "   3. Run 'just build' to build the project"
    echo ""
    echo "πŸ’‘ Jetson tips:"
    echo "   - CUDA is typically pre-installed at /usr/local/cuda-12.6, /usr/local/cuda-11.4 or /usr/local/cuda-10.2"
    echo "   - TensorRT is usually available at /usr/lib/aarch64-linux-gnu/"
    echo "   - If build fails, you may need to install JetPack SDK development components"
    echo "   - Monitor system temperature during compilation (use 'tegrastats')"
    echo "   - For CUDA 12.6 systems, ensure you have the latest JetPack SDK Manager"
    echo ""
    
    # Install JetPack for Jetson systems (includes all DLA libraries)
    echo "πŸ”§ Installing JetPack for complete Jetson development environment..."
    
    update_package_cache
    
    if sudo apt install -y nvidia-jetpack; then
        echo "βœ… JetPack installed successfully - includes all DLA libraries!"
        echo "   This resolves all TensorRT DLA dependencies"
    else
        echo "⚠️  JetPack installation failed (may have version conflicts)"
        echo "   πŸ’‘ Try: sudo apt update && sudo apt install -y nvidia-jetpack"
        echo "   πŸ”„ If issues persist, build should still work with existing libraries"
    fi
else
    # Non-Jetson systems
    echo "πŸ“‹ Next steps for $ARCH system:"
echo "   1. Make sure TensorRT is installed and TensorRT_ROOT is set"
echo "   2. Run 'source scripts/setup_env.sh' to set up the environment"
echo "   3. Run 'just build' to build the project"
echo ""
    
    if [[ "$ARCH" == "aarch64" ]]; then
        echo "πŸ’‘ ARM64 tips:"
        echo "   - CUDA may be installed at /usr/local/cuda or system locations"
        echo "   - TensorRT typically installed via package manager"
        echo "   - DLA libraries not needed on non-Jetson ARM64 systems"
    elif [[ "$ARCH" == "x86_64" ]]; then
        echo "πŸ’‘ x86_64 tips:"
        echo "   - CUDA typically at /usr/local/cuda or via CUDA toolkit"
        echo "   - TensorRT available from NVIDIA or package repositories"
        echo "   - Use nvidia-smi to check GPU compatibility"
        echo "   - DLA libraries typically not needed on desktop GPUs"
    fi
fi

echo ""
echo "⚠️  If you encounter any issues:"
if [ "$IS_JETSON" = true ]; then
    echo "   πŸ€– Jetson-specific troubleshooting:"
    echo "   - Check JetPack version: jetson_release"
    echo "   - Verify CUDA installation: ls -la /usr/local/cuda*"
    echo "   - Monitor resources: tegrastats"
    echo "   - For DLA linking errors: Install full JetPack SDK"
    echo "   - For help: https://developer.nvidia.com/embedded/jetpack"
elif [[ "$ARCH" == "aarch64" ]]; then
    echo "   πŸ”§ ARM64 troubleshooting:"
    echo "   - Check CUDA: ls -la /usr/local/cuda*"
    echo "   - Verify libraries: ldconfig -p | grep cuda"
    echo "   - DLA errors: DLA not supported on non-Jetson ARM64"
elif [[ "$ARCH" == "x86_64" ]]; then
    echo "   πŸ–₯️  x86_64 troubleshooting:"
    echo "   - Check GPU: nvidia-smi"
    echo "   - Verify CUDA: nvcc --version"
    echo "   - Check environment: echo \$CUDA_HOME \$TensorRT_ROOT"
    echo "   - DLA errors: DLA typically not supported on desktop GPUs"
else
    echo "   - Check that all environment variables are set correctly"
    echo "   - Verify CUDA installation for your architecture"
fi