#/bin/bash -e
# TechNexion Camera Driver Modules Installation for
# Nvidia Jetson ORIN NANO Development Kits.
# Before installing, You should backup your image to avoid
# there are any files you lost.

PLATFORM="eac5000"
MODULE_FILE="tevs.ko"
SERVICE_FILE="tn_cam.service"
TGT_SERVICE_FILE="/etc/systemd/system/"$SERVICE_FILE

REF_LEBEL_NAME="primary"
REF_DTB_FILE="kernel_tegra234-$PLATFORM-000d+p3701-0005-nv.dtb"
EXTLINUX_CONF="/boot/extlinux/extlinux.conf"

get_kernel_version() {
    TEGRA_KERNEL_VER=$(uname -r)
}

install_tncam_modules() {
    echo "Install TN-CAM modules: $1"
    TGT_MODULE_FILE=/lib/modules/$TEGRA_KERNEL_VER/kernel/drivers/media/i2c/$1
    sudo cp $1 $TGT_MODULE_FILE
}

install_tncam_dtbo() {
    echo "Install TN-CAM DTBO file: $1"
    DTB_FILE="tegra234-$PLATFORM-camera-$1-overlay.dtbo"
    TGT_DTB_FILE="/boot/"$DTB_FILE
    sudo cp $DTB_FILE $TGT_DTB_FILE
    echo "Installed TN-CAM DTB file Done."
}

add_tncam_conf() {
    echo "Add TN-CAM Configuration for modules: $1"
    # sudo sed -i "s/DEFAULT .*/DEFAULT tn-$1/" $EXTLINUX_CONF
    CHECKED_CONF=$(cat $EXTLINUX_CONF| grep "LABEL tn-$1")
    if [ -n "$CHECKED_CONF" ]; then
        return 27
    fi
    TEVS_CONF=$(awk "/LABEL $REF_LEBEL_NAME/,/APPEND /" $EXTLINUX_CONF | \
    sed -e "s|LINUX /boot/Image|&\n      FDT /boot/dtb/$REF_DTB_FILE\n      OVERLAYS /boot/$DTB_FILE|" | \
    sed "s/LABEL primary/LABEL tn-$1/")
    sudo bash -c "echo -e '\n\n$TEVS_CONF' >> $EXTLINUX_CONF"
}

set_default_conf() {
    echo "Set Default Configuration for modules: $1"
    sudo sed -i "s/DEFAULT .*/DEFAULT tn-$1/" $EXTLINUX_CONF
}

install_tncam_service() {
    echo "Install TN-CAM service..."
    SERVICE_CONTENT=$(cat $SERVICE_FILE | \
    sed "s/KERNEL_VER/$TEGRA_KERNEL_VER/g")
    sudo bash -c 'echo -e "\n\n'"$SERVICE_CONTENT"'" > '"$TGT_SERVICE_FILE"
    echo "Launch TN-CAM Service..."
    sudo systemctl daemon-reload
    sudo systemctl enable $SERVICE_FILE
    sudo systemctl start $SERVICE_FILE 2>/dev/null
}

# Start to install TechNexion Camera Driver for Jetson ORIN NANO
sudo echo "" > /dev/null # Access sudo Permissions
cat << EOF
****** TechNexion Camera Driver Installation ******
This installation is easy to install TechnNexion Camera Drivers for Nvidia
Jetson AGX Orin Platform. Before start to install camera driver,
You should BACKUP your image to avoid any file you lost while installing process.
EOF
read -p "Do you want to continue?[Y/n]" choice


if [ "$choice" = "y" ] || [ "$choice" = "Y" ]; then
    echo "Continuing with the installation..."
    get_kernel_version
    install_tncam_modules "max9296a.ko"
    install_tncam_modules "max96724.ko"
    install_tncam_modules "max96717.ko"
    install_tncam_modules "tevs.ko"
    install_tncam_dtbo "vls-gm2"
    install_tncam_dtbo "vls-gm2-fsync"
	
	module="vls-gm2"
	echo "Installing configuration: $module"
	DTB_FILE="tegra234-$PLATFORM-camera-$module-overlay.dtbo"
	add_tncam_conf $module
	
	module="vls-gm2-fsync"
	echo "Installing configuration: $module"
	DTB_FILE="tegra234-$PLATFORM-camera-$module-overlay.dtbo"
	add_tncam_conf $module
	
	set_default_conf "vls-gm2"
	
    if [ $? -eq 27 ]; then
        echo "$module have existed in $EXTCONF"
        echo "Set default configuration for $module."
    fi
    install_tncam_service

    echo "Finish Camera Driver Installation. Return Code:["$?"]"
    if [ $? -eq 0 ]; then
        echo "You should Reboot Device to enable $module Cameras."
        read -p "Do you want to reboot now?[Y/n]" choice
        if [ "$choice" = "y" ] || [ "$choice" = "Y" ]; then
            echo "Rebooting...."
            sudo reboot
        else
            echo "Reboot manually later."
        fi
    fi
else
    echo "Installation aborted."
fi