#!/bin/sh

# Copyright (C) 2010-2026 Daniel Baumann <daniel@debian.org>
#
# SPDX-License-Identifier: GPL-3.0+
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Usage:
# wget -q https://deb.progress-linux.org/packages/project/install/yolo.sh -O - | /bin/sh -

# truncation guard
if true
then
	set -e

	RED="\033[1;33;31m"
	NORMAL="\033[0m"

	DEBIAN_FRONTEND="noninteractive"
	DEBIAN_PRIORITY="critical"
	export DEBIAN_FRONTEND DEBIAN_PRIORITY

	DEBCONF_NONINTERACTIVE_SEEN="true"
	DEBCONF_NOWARNINGS="true"
	export DEBCONF_NONINTERACTIVE_SEEN DEBCONF_NOWARNINGS

	UCF_FORCE_CONFNEW="yes"
	export UCF_FORCE_CONFNEW

	echo "################################################################################"
	echo "# Starting upgrade with Progress Linux packages - YOLO install 🤙"
	echo "################################################################################"
	echo

	# ##############################################################################
	echo -n "# 1. Checking user..."
	# ##############################################################################

	if [ "$(id -u)" -eq "0" ]
	then
		SUDO=""

		echo
		echo -n "# Running as root..."
	else
		echo
		sudo true

		if [ "$(sudo id -u )" -eq "0" ]
		then
			SUDO="sudo"

			echo
			echo -n "# Running with sudo..."
		else
			echo " 🫵"
			echo
			echo "${RED}Error:${NORMAL} needs to be started as root or having sudo privileges"
			exit 1
		fi
	fi

	echo " 👍"
	echo

	# ##############################################################################
	echo "# 2. Running apt update..."
	# ##############################################################################

	echo
	echo "--- Begin: apt update ---"
	${SUDO} apt update --quiet --quiet
	echo "--- End: apt update ---"
	echo

	# ##############################################################################
	echo -n "# 3. Checking availability of the progress-linux package..."
	# ##############################################################################

	if ! apt show progress-linux > /dev/null 2>&1
	then
		echo " 👎"
		echo
		echo "${RED}Error:${NORMAL} unsupported (debian) distribution"
		exit 1
	fi

	echo " 👍"
	echo

	# ##############################################################################
	echo -n "# 4. Checking availability of the debconf-utils package..."
	# ##############################################################################

	if ! dpkg --get-selections | awk '{ print $1 }' | grep -qs '^debconf-utils$'
	then
		echo
		echo "# Installing debconf-utils..."
		echo

		echo
		echo "--- Begin: apt install debconf-utils ---"
		${SUDO} apt install --no-install-recommends --quiet --quiet --yes debconf-utils
		echo "--- End: apt install debconf-utils ---"
	else
		echo " 👍"
	fi

	echo

	# ##############################################################################
	echo -n "# 5. Preseeding progress-linux package..."
	# ##############################################################################

	TMPFILE="$(mktemp)"

cat > "${TMPFILE}" << EOF
progress-linux progress-linux/archives multiselect horok, horok-security, horok-updates, horok-extras, horok-backports, horok-backports-extras
progress-linux progress-linux/archive-areas multiselect main, contrib, non-free, non-free-firmware, restricted
progress-linux progress-linux/mirror string https://deb.progress-linux.org/packages
EOF

	cat "${TMPFILE}" | ${SUDO} debconf-set-selections

	rm -f "${TMPFILE}"

	echo " 👍"
	echo

	if ! dpkg --get-selections | awk '{ print $1 }' | grep -qs '^progress-linux$'
	then
		# ##############################################################################
		echo "# 6. Installing progress-linux package..."
		# ##############################################################################

		echo
		echo "--- End: apt install progress-linux ---"
		${SUDO} apt install --no-install-recommends --quiet --quiet --yes progress-linux
		echo "--- End: apt install progress-linux ---"
	else
		# ##############################################################################
		echo -n "# 6. Reconfiguring progress-linux package..."
		# ##############################################################################

		${SUDO} dpkg-reconfigure -fnoninteractive -pcritical progress-linux

		echo " 👍"
	fi

	echo

	# ##############################################################################
	echo -n "# 7. Removing incompatible packages (if any)..."
	# ##############################################################################

	# FIXME: fix grub2 signed to grub2 unsigned upgrade path
	if dpkg --get-selections | grep -qs grub-efi-amd64-signed
	then
		echo
		echo "# Removing grub-efi-amd64-signed (\"secure boot\" is currently unsupported)"

		${SUDO} apt purge --allow-remove-essential --quiet --quiet --yes grub-efi-amd64-signed
	else
		echo " none 👍"
	fi

	echo

	# ##############################################################################
	echo "# 8. Upgrading system..."
	# ##############################################################################

	echo
	echo "--- Begin: apt full upgrade ---"
	${SUDO} DEBIAN_FRONTEND="noninteractive" DEBIAN_PRIORITY="critical" DEBCONF_NONINTERACTIVE_SEEN="true" DEBCONF_NOWARNINGS="true" apt full-upgrade --option Dpkg::Options::=--force-confnew --quiet --quiet --update --yes
	echo "--- End: apt full upgrade ---"

	echo
	echo "--- Begin: apt autopurge ---"
	${SUDO} apt autopurge --quiet --quiet --yes
	echo "--- End: apt autopurge ---"

	echo
	echo "################################################################################"
	echo "# Finished upgrade with Progress Linux packages - please reboot now 👌"
	echo "################################################################################"
fi
