#!/bin/bash

# Copy files.
cp -rf bsp/* /
ldconfig

# links
ln -s /opt/nvgpu/usr/share/egl/egl_external_platform.d/nvidia_wayland.json /usr/share/egl/egl_external_platform.d/nvidia_wayland.json
ln -s /opt/nvgpu/usr/share/glvnd/egl_vendor.d/10_nvidia.json /usr/share/glvnd/egl_vendor.d/10_nvidia.json
ln -s /opt/nvgpu/etc/vulkan/icd.d/nvidia_icd.json /etc/vulkan/icd.d/nvidia_icd.json
ln -s /opt/nvgpu/usr/lib/xorg/modules/drivers/nvidia_drv.so /usr/lib64/xorg/modules/drivers/nvidia_drv.so
ln -s /opt/nvgpu/usr/lib/xorg/modules/extensions/libglxserver_nvidia.so /usr/lib64/xorg/modules/extensions/libglxserver_nvidia.so

machine="$(tr -d '\0' < /proc/device-tree/compatible)"
if [[ "${machine}" =~ "jetson-nano" ]]; then
        machine="jetson-nano"
elif [[ "${machine}" =~ "jetson-xavier" ]]; then
        machine="jetson-xavier"
elif [[ "${machine}" =~ "jetson-cv" ]]; then
        machine="jetson_tx1"
elif [[ "${machine}" =~ "storm" ]]; then
        machine="storm"
elif [[ "${machine}" =~ "quill" ]]; then
        machine="quill"
elif [[ "${machine}" =~ "lightning" ]]; then
        machine="lightning"
elif [[ "${machine}" =~ "p2972-0006" ]]; then
        machine="p2972-0006"
elif [[ "${machine}" =~ "p3668" ]]; then
        machine="p3668"
else
        machine="`cat /proc/device-tree/model`"
fi

CHIP="$(tr -d '\0' < /proc/device-tree/compatible)"
if [[ ${CHIP} =~ "tegra186" ]]; then
         SOCFAMILY="tegra186"
elif [[ ${CHIP} =~ "tegra210" ]]; then
         SOCFAMILY="tegra210"
elif [[ ${CHIP} =~ "tegra194" ]]; then
         SOCFAMILY="tegra194"
fi

# create /etc/nvpmodel.conf symlink
if [ ! -e "/etc/nvpmodel.conf" ]; then
        conf_file=""
        if [ "${SOCFAMILY}" = "tegra186" ]; then
                if [ "${machine}" = "storm" ]; then
                        use_case_model="`cat /proc/device-tree/use_case_model`"
                        if [ "${use_case_model}" = "ucm1" ]; then
                                conf_file="/etc/nvpmodel/nvpmodel_t186_storm_ucm1.conf"
                        elif [ "${use_case_model}" = "ucm2" ]; then
                                conf_file="/etc/nvpmodel/nvpmodel_t186_storm_ucm2.conf"
                        fi
                else
                        conf_file="/etc/nvpmodel/nvpmodel_t186.conf"
                fi
        elif [ "${SOCFAMILY}" = "tegra194" ]; then
                if [ "${machine}" = "p2972-0006" ]; then
                        conf_file="/etc/nvpmodel/nvpmodel_t194_8gb.conf"
                elif [ "${machine}" = "p3668" ]; then
                        conf_file="/etc/nvpmodel/nvpmodel_t194_p3668.conf"
                else
                        conf_file="/etc/nvpmodel/nvpmodel_t194.conf"
                fi
        elif [ "${SOCFAMILY}" = "tegra210" ]; then
                if [ "${machine}" = "jetson-nano" ]; then
                        conf_file="/etc/nvpmodel/nvpmodel_t210_jetson-nano.conf"
                else
                        conf_file="/etc/nvpmodel/nvpmodel_t210.conf"
                fi
        fi

        if [ "${conf_file}" != "" ]; then
                if [ -e "${conf_file}" ]; then
                        ln -sf "${conf_file}" /etc/nvpmodel.conf
                else
                        echo "${SCRIPT_NAME} - WARNING: file ${conf_file} not found!"
                fi
        fi
fi

# The nvidia udev policy, which is used here
# only allows access to the gpu to users in
# the video group. 
usermod -aG video gdm
usermod -aG video $(getent passwd 1000 | cut -d: -f1)
usermod -aG video lightdm

# enable services
systemctl enable nvphs.service
systemctl enable nvpmodel.service
