#!/bin/bash

# SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

set -e

. /usr/share/debconf/confmodule

nvsnap_install_script="/usr/lib/nvidia/utils/nvsnap-install.sh"

db_input high nvchromium/info
db_input high nvchromium/install || true
db_go

db_get nvchromium/install
if [ "${RET}" = "true" ]; then
	"${nvsnap_install_script}" -i chromium &
	child_pid=$!

	num_steps=300
	counter=1
	db_progress START 0 ${num_steps} nvchromium/wait_for_install_title
	db_progress INFO nvchromium/wait_for_install_info
	while [ -d "/proc/${child_pid}" ]; do
		sleep 1
		if [[ "${counter}" -lt "${num_steps}" ]]; then
			db_progress STEP 1
			counter=$((counter+1))
		fi
	done
	db_progress SET ${num_steps}
	db_progress STOP

	set +e
	wait ${child_pid}
	return_code=$?
	set -e

	case ${return_code} in
	1)
		db_input high nvchromium/install_error_1 || true
	;;
	2)
		db_input high nvchromium/install_error_2 || true
	;;
	3)
		db_input high nvchromium/install_error_3 || true
	;;
	esac
	db_go
fi
