#!/bin/bash

# Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * 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.
#  * Neither the name of NVIDIA CORPORATION 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 ``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 OWNER 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.

#
# This target-side script calls dpkg to install Debian packages located in
# NV_DEBS_BASE_DIR. The --upgrade-only option will only upgrade Debian
# packages currently installed on the target system and not install any
# new packages.
#

set -e

USER=$(whoami)
if [ "${USER}" != "root" ]; then
	echo "This script requires root privileges"
	exit 1
fi

NV_DEBS_BASE_DIR="/opt/nvidia/l4t-packages"

function show_usage() {
	echo "Usage: sudo install-nv-debs [OPTIONS]"
cat << EOF
	This script installs nvidia debian packages and their dependencies

	Options:
		-h | --help	Show this help message
		--upgrade-only	Only perform upgrade of currently installed packages
EOF
}

case "$1" in

	--help|-h)
			show_usage
			exit 1
			;;

	--upgrade-only)
			DEBS_TO_INSTALL=
			for deb in "${NV_DEBS_BASE_DIR}/userspace"/*.deb "${NV_DEBS_BASE_DIR}/bootloader"/*.deb "${NV_DEBS_BASE_DIR}/kernel"/*.deb; do
				package="$(echo ${deb} | grep -o '^[^_]*')"
				dpkg -s "${package}" > /dev/null 2>&1
				DEBS_TO_INSTALL+=("${package}"})
			done
esac

if [ -z "${DEBS_TO_INSTALL}" ]; then
	DEBS_TO_INSTALL=("${NV_DEBS_BASE_DIR}/userspace"/*.deb)

	# Install nvidia-l4t-bootloader and nvidia-l4t-kernel packages separately
	# since they require special treatment
	if [ -e "${NV_DEBS_BASE_DIR}/bootloader"/nvidia-l4t-bootloader_*.deb ]; then
		dpkg --unpack "${NV_DEBS_BASE_DIR}/bootloader"/nvidia-l4t-bootloader_*.deb
		rm /var/lib/dpkg/info/nvidia-l4t-bootloader.postinst -f
		dpkg --configure nvidia-l4t-bootloader
	fi
	if [ -e "${NV_DEBS_BASE_DIR}/kernel"/nvidia-l4t-kernel_*.deb ]; then
		dpkg --unpack "${NV_DEBS_BASE_DIR}/kernel"/nvidia-l4t-kernel_*.deb
		rm /var/lib/dpkg/info/nvidia-l4t-kernel.postinst -f
		dpkg --configure nvidia-l4t-kernel
	fi

fi

dpkg -i --skip-same-version --refuse-downgrade "${DEBS_TO_INSTALL[@]}"

echo "Installation of debian packages: SUCCESS"
