#!/bin/bash

#
# SPDX-FileCopyrightText: Copyright (c) 2016-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ ! -e /etc/nv/nvfirstboot ]; then
	exit 0
fi

if [ -f "/usr/share/applications/ubuntu-amazon-default.desktop" ]; then
	echo "Hidden=true" >> /usr/share/applications/ubuntu-amazon-default.desktop
fi

# Generate SSH host keys if they are not present under /etc/ssh/
# SSH host keys will also be generated by cloud-init if it is present. Skip generating SSH host keys
# here to avoid race condition.
if [ -e "/lib/systemd/system/ssh.service" ] && [ ! -e "/lib/systemd/system/cloud-init.service" ] ; then
	# Wait for the process to finish which has aquired this lock
	while fuser "/var/cache/debconf/config.dat" > "/dev/null" 2>&1; do sleep 1; done;
	while fuser "/var/cache/debconf/templates.dat" > "/dev/null" 2>&1; do sleep 1; done;
	while fuser "/var/cache/debconf/passwords.dat" > "/dev/null" 2>&1; do sleep 1; done;

	# Prevent starting ssh.service immediately after generating SSH host keys
	UBUNTU_VERSION=$(lsb_release -rs)
	if [ "$(echo "${UBUNTU_VERSION} >= 22.04" | bc)" -eq 1 ]; then
		echo exit 101 > /usr/sbin/policy-rc.d
		chmod +x /usr/sbin/policy-rc.d
	fi

	while [ "${status-1}" -ne 0 ]; do
		dpkg-reconfigure --frontend=noninteractive openssh-server; status="$?"
	done

	if [ "$(echo "${UBUNTU_VERSION} >= 22.04" | bc)" -eq 1 ]; then
		rm /usr/sbin/policy-rc.d
	fi
fi

# Update chipid to apt source list file
SOURCE="/etc/apt/sources.list.d/nvidia-l4t-apt-source.list"
if [ -e "${SOURCE}" ]; then
	CHIP="$(cat /proc/device-tree/compatible)"
	if [[ "${CHIP}" =~ "tegra194" ]]; then
		sed -i "s/<SOC>/t194/g" "${SOURCE}"
	elif [[ "${CHIP}" =~ "tegra234" ]]; then
		sed -i "s/<SOC>/t234/g" "${SOURCE}"
	else
		logger "nvfb: Updating apt source list failed with exit code: 1"
	fi
fi

function wait_debconf_resource() {
	# Wait for the process to finish which has acquired this lock
	while fuser "/var/cache/debconf/config.dat" > "/dev/null" 2>&1; do sleep 1; done
	while fuser "/var/cache/debconf/templates.dat" > "/dev/null" 2>&1; do sleep 1; done
}

# Reconfigure blueman if it's present in rootfs
if [ -d "/usr/lib/blueman" ]; then
	wait_debconf_resource
	if [[ $(dpkg --audit blueman) ]]; then
		dpkg --configure blueman
	fi
fi

# Restrict blueman applet loading at login
blueman_app="/etc/xdg/autostart/blueman.desktop"
if [ -f "${blueman_app}" ]; then
	sed -i '/^NotShowIn=/ s/$/;Unity;GNOME/' "${blueman_app}"
fi

# Restrict clipit autostart at login
clipit_app="/etc/xdg/autostart/clipit-startup.desktop"
if [ -f "${clipit_app}" ]; then
	sed -i 's/OnlyShowIn/NotShowIn/g' "${clipit_app}"
fi

rm -rf /etc/nv/nvfirstboot
