#!/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.fastforward.debian.net/debian-fastforward/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 Fast Forward 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 "# 2. Checking availability of the debian-fastforward package..."
	# ##############################################################################

	if ! apt show debian-fastforward > /dev/null 2>&1
	then
		echo
		echo
		echo -n "# Downloading debian-fastforward package..."

		TMPDIR="$(mktemp --directory)"

		cd "${TMPDIR}"
		wget -q https://deb.debian.org/debian/pool/main/d/debian-fastforward/debian-fastforward_20250601-2_all.deb
		wget -q https://deb.debian.org/debian/pool/main/d/debian-fastforward/debian-fastforward-pgp-keys_20250601-2_all.deb
		wget -q https://deb.debian.org/debian/pool/main/d/debian-fastforward/debian-fastforward-ssh-keys_20250601-2_all.deb
		cd "${OLDPWD}"
	fi

	echo " 👍"
	echo

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

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

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

	echo

	# ##############################################################################
	echo -n "# 4. Preseeding debian-fastforward package..."
	# ##############################################################################

	TMPFILE="$(mktemp)"

cat > "${TMPFILE}" << EOF
debian-fastforward debian-fastforward/archives multiselect trixie-fastforward, trixie-fastforward-security, trixie-fastforward-updates, trixie-fastforward-backports
debian-fastforward debian-fastforward/archive-areas multiselect main, contrib, non-free, non-free-firmware
debian-fastforward debian-fastforward/mirror string https://deb.fastforward.debian.net/debian-fastforward
debian-fastforward debian-fastforward/backports-priority boolean true
EOF

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

	rm -f "${TMPFILE}"

	echo " 👍"
	echo

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

		if apt show debian-fastforward > /dev/null 2>&1
		then
			echo
			echo "--- Begin: apt install debian-fastforward ---"
			${SUDO} apt install --no-install-recommends --quiet --quiet --update --yes debian-fastforward
			echo "--- End: apt install debian-fastforward ---"
		else
			echo
			echo "--- Begin: dpkg --install debian-fastforward ---"
			${SUDO} dpkg --install debian-fastforward_20250601-2_all.deb debian-fastforward-pgp-keys_20250601-2_all.deb debian-fastforward-ssh-keys_20250601-2_all.deb
			echo "--- End: dpkg --install debian-fastforward ---"

			cd "${OLDPWD}"
			rm -rf "${TMPDIR}"
		fi
	else
		# ##############################################################################
		echo -n "# 5. Reconfiguring debian-fastforward package..."
		# ##############################################################################

		${SUDO} dpkg-reconfigure -fnoninteractive -pcritical debian-fastforward

		echo " 👍"
	fi

	echo

	# ##############################################################################
	echo "# 6. 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 Fast Forward packages - please reboot now 👌"
	echo "################################################################################"
fi
