##############################################################################
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2025 Engineering Design Team, Inc.
# https://edt.com
#
# Board-specific variables and functions for building and flashing system
# image binaries.
#
##############################################################################

# NVIDIA flash script to use, relative to the L4T root dir.
EDT_BOARD_FLASH_SH=./tools/kernel_flash/l4t_initrd_flash.sh

# Arguments to the flash.sh (or l4t_initrd_flash.sh) script in L4T.
# Refer to <L4T>/tools/kernel_flash/README_initrd_flash.txt
EDT_BOARD_FLASH_SH_ARGS=(-c tools/kernel_flash/flash_l4t_t234_nvme.xml -p '-c bootloader/generic/cfg/flash_t234_qspi.xml' --showlogs --network usb0 --external-device nvme0n1p1 edt-edgetak-r01+p3767-0000 nvme0n1p1)

export EDT_BOARD_FLASH_SH_ARGS

# Refer to the "Appendix" section of
# <L4T>/tools/kernel_flash/README_initrd_flash.txt
BOARDID="3767"
BOARDSKU="0000"
BOARDREV="G.3"
FAB="300"
CHIP_SKU="00:00:00:D3"
RAMCODE_ID="0"

# Function run after executing the "apply_binaries.sh" and adding in rootfs
# files, but before building system images.
function fn_board_specific_final_changes() {
    sudo sed -i 's/NVIDIA_Login_Logo.png/EDT_EdgeTak_Login_Logo.png/g' "$L4T_DIR"/rootfs/etc/skel/.xsessionrc
    sudo sed -i 's/-border white/-border black/g' "$L4T_DIR"/rootfs/etc/skel/.xsessionrc

    sed -i 's/log_level = <4>/log_level = <2>/g' "$L4T_DIR"/bootloader/tegra234-mb1-bct-misc-common.dtsi

    # Kernel driver source code for the board's USB PD controller was added into the
    # staged rootfs directory during the "apply_binaries.sh" step. Manually
    # running the DKMS "install" command in a chroot environment will
    # output a few warnings but will otherwise successfully build and install
    # the kernel module. The module will be available on first boot after flashing.
    echo "Build and install additional kernel driver modules... "

    sudo chroot "$L4T_DIR"/rootfs ./bin/bash -c \
        "dkms install -m tps6598x -v 6.9+a38297e3fb0-edt1.1.0 -k 5.15.148-tegra"

    sudo chroot "$L4T_DIR"/rootfs ./bin/bash -c \
        'apt-get purge --autoremove -y libreoffice* thunderbird* gnome-mahjongg gnome-mines gnome-sudoku transmission-gtk'

    # EDT disabled starting nvargus-daemon at boot on EdgeTak because there
    # are no MIPI cameras and nvargus-daemon will crash on login.
    # https://forums.developer.nvidia.com/t/the-nvargus-daemon-crashed-with-sigsegv-after-boot-up-for-a-while/198757/3
    echo "Disable 'nvargus-daemon' service from the 'nvidia-l4t-camera' package..."
    sudo chroot "$L4T_DIR"/rootfs /usr/bin/systemctl disable nvargus-daemon.service

    sudo rm -fv "$L4T_DIR"/rootfs/README.txt

    # Jetson AGX Orin is irrelevant
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3701*
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3737-camera*
    # Jetson Orin NX Nano is irrelevant
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3767-0003*
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3767-0004*
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3767-0005*
    # devkit or gpio header stuff
    sudo rm -rfv "$L4T_DIR"/rootfs/boot/*p3767-camera*

}
