#!/bin/bash

set -e
shopt -s extglob

# This script does almost, but not quite, the same thing as postinst. The
# differences are:
#
# - preinst errors out when an unsupported situation is encountered,
#   postinst does not
# - postinst writes the file in /etc/apt/sources.list, preinst does not.
#
# This is because if postinst errors out, the failed package brings the
# whole system in a failed state that is extremely difficult to recover
# from. However, preinst can't use dependencies that are not in the
# Essential set, such as ucf, which we use to write the config file. So
# we can only do that in postinst...
#
# Please do not forget to update *both* scripts!

. /etc/os-release

too_old() {
	echo "distribution too old, cannot continue. Please upgrade, or compile from source."
        if [ "$FORCE_OLD_INSTALL" = "1" ]; then
                exit 0
        fi
	exit 1
}

# NOTE NOTE NOTE: See the comment above and do not forget to also update preinst

MINT_OLDSTABLE=vanessa
MINT_OLDSTABLE_VERSION=21
MINT_OLDSTABLE_ALIASES="@(vera|victoria|virginia|vanessa)"
MINT_STABLE=wilma
MINT_STABLE_ALIASES="@(wilma|xia|zara)"
DEBIAN_OLDSTABLE=bookworm
DEBIAN_OLDSTABLE_VERSION=12
DEBIAN_STABLE=trixie
UBUNTU_OLDLTS=jammy
UBUNTU_OLDLTS_VERSION=22.04
UBUNTU_LTS=noble
UBUNTU_STABLE=questing

## DO NOT change anything beyond this point

PLAIN_SUPPORTED="$DEBIAN_OLDSTABLE|$DEBIAN_STABLE|$UBUNTU_OLDLTS|$UBUNTU_LTS"

if [ ! -z "$UBUNTU_STABLE" ]; then
        PLAIN_SUPPORTED="$PLAIN_SUPPORTED|$UBUNTU_STABLE"
fi

PLAIN_SUPPORTED="@($PLAIN_SUPPORTED)"

case "$VERSION_CODENAME" in
	$MINT_OLDSTABLE_ALIASES)
		VERSION_CODENAME=$MINT_OLDSTABLE
	;;
	$MINT_STABLE_ALIASES)
		VERSION_CODENAME=$MINT_STABLE
	;;
        $PLAIN_SUPPORTED)
		:
	;;
	*)
		echo -e "WARNING: Unsupported distribution found. Please see the website at\nhttps://eid.belgium.be/en/faq/which-linux-distributions-are-supported#7406\nfor a list of supported distributions."
		case "$ID" in
			linuxmint)
				if dpkg --compare-versions $MINT_OLDSTABLE_VERSION le $VERSION_ID
				then
					VERSION_CODENAME=$MINT_OLDSTABLE
				else
					too_old
				fi
				echo "Found Linux Mint $VERSION, using $VERSION_CODENAME"
			;;
			ubuntu)
				if dpkg --compare-versions $UBUNTU_OLDLTS_VERSION le $VERSION_ID
				then
					VERSION_CODENAME=$UBUNTU_OLDLTS
				else
					too_old
				fi
				echo "Found Ubuntu $VERSION, using $VERSION_CODENAME"
			;;
			debian)
				# On testing and unstable, $VERSION_ID is empty
				if [ -z "$VERSION_ID" ]; then
					VERSION=testing/unstable
					VERSION_CODENAME=$DEBIAN_STABLE
				else
                                        if dpkg --compare-versions $DEBIAN_OLDSTABLE_VERSION le $VERSION_ID
                                        then
					VERSION_CODENAME=$DEBIAN_OLDSTABLE
                                        else
                                                too_old
                                        fi
				fi
				echo "Found Debian $VERSION, using $VERSION_CODENAME"
			;;
			*)
				VERSION_CODENAME=$DEBIAN_OLDSTABLE
				echo "Unknown distribution found. Using $VERSION_CODENAME as fallback."
			;;
		esac
	;;
esac

#DEBHELPER#
