#!/bin/bash

set -e
shopt -s extglob

# This script does almost, but not quite, the same thing as preinst. 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!

DIR=$(mktemp -d)

. /etc/os-release

too_old() {
	echo "distribution too old, cannot continue. Please upgrade, or compile from source."
	exit 0
}

# 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
	;;
        $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
sed -e "s/@DIST@/${VERSION_CODENAME}/g" /usr/share/eid-archive/eid.sources > $DIR/eid.sources
ucf $DIR/eid.sources /etc/apt/sources.list.d/eid.sources

if [ "$1" == "configure" -a ! -z "$2" ]; then
        if dpkg --compare-versions 2025.4 ge "$2"; then
                echo "Upgrading from old (pre-2025.4) version, deleting old config"
                ucf --purge /etc/apt/sources.list.d/eid.list
                rm -f /etc/apt/sources.list.d/eid.list
                rm -f /etc/apt/trusted.gpg.d/eid-archive*
        fi
fi

if [ -x /usr/sbin/update-software-center ]; then
	update-software-center --triggered=TRIGGERED
fi

echo -e "Repository enabled, keys installed. Please run \"apt-get update\" followed by\n\"apt-get install eid-mw eid-viewer\" to install the middleware and the viewer, respectively."

#DEBHELPER#
