#!/bin/env  bash
echo ""
echo "### backup sources ###"
echo "----------------------"
mkdir -p /etc/apt/backup
mv -f /etc/apt/sources.list /etc/apt/backup/
mv -f /etc/apt/sources.list.d /etc/apt/backup/
rm -f /etc/apt/sources.list
rm -f /etc/apt/sources.list.d/*.list

echo ""
echo "### write default sources ###"
echo "-----------------------------"
echo deb https://mirrors.tuxedocomputers.com/ubuntu/mirror/archive.ubuntu.com/ubuntu jammy main restricted universe multiverse >> /etc/apt/sources.list
echo deb https://mirrors.tuxedocomputers.com/ubuntu/mirror/security.ubuntu.com/ubuntu jammy-security main restricted universe multiverse >> /etc/apt/sources.list
echo deb https://mirrors.tuxedocomputers.com/ubuntu/mirror/archive.ubuntu.com/ubuntu jammy-updates main restricted universe multiverse >> /etc/apt/sources.list
mkdir -p /etc/apt/sources.list.d/
echo deb https://deb.tuxedocomputers.com/ubuntu jammy main >> /etc/apt/sources.list.d/tuxedo-computers.list
if [ "$(grep PRETTY /etc/os-release | awk -F\" '{print $2}')" = "TUXEDO OS 2" ] ; then
    rm -f /mnt/plasma6
    echo deb https://plasma.tuxedocomputers.com jammy main >> /etc/apt/sources.list.d/tuxedo-plasma.list
    echo deb https://txos.tuxedocomputers.com/ubuntu jammy main >> /etc/apt/sources.list.d/tuxedo-os.list
else
    touch /mnt/plasma6
    echo deb https://plasma6.tuxedocomputers.com jammy main >> /etc/apt/sources.list.d/tuxedo-plasma.list
    echo deb https://txos.tuxedocomputers.com/ubuntu jammy main >> /etc/apt/sources.list.d/tuxedo-os.list
    echo deb https://txos.tuxedocomputers.com/ubuntu jammy-plasma main >> /etc/apt/sources.list.d/tuxedo-os.list
fi
echo deb https://txos-extra.tuxedocomputers.com/ubuntu jammy main >> /etc/apt/sources.list.d/tuxedoos-extra.list

echo ""
echo "### apt run with defined package-list ###"
echo "-----------------------------------------"

dpkg --configure -a
apt-get update

echo ""
echo " ### Remove all i386 packages ###"
echo ""

DEBIAN_FRONTEND=noninteractive apt-get --assume-yes purge --allow-remove-essential $(dpkg -l | awk '/i386/{print $2}')
dpkg --remove-architecture i386

echo ""
echo "### remove all packages besides the base system ###"
echo ""
#export DEBIAN_FRONTEND=noninteractive
#comm -23 <(LANG=C apt list --installed | cut -d'/' -f1 | grep -Ev "^lib|Listing|gcc|g++|grub|shim|mok|cpp|initramfs|ucf|libc6|sudo|resolve|systemd" | sort) <(sort /mnt/basesystem.list) | xargs apt-get purge -y

DEBIAN_FRONTEND=noninteractive apt-get --assume-yes purge $(dpkg -l | grep -E "kde|kf|qt|qml|plasma|wayland|x11|xorg|cups|nextcloud|neon" | awk '{print $2}') foomatic-filters-
DEBIAN_FRONTEND=noninteractive apt-get -y -f install
rm /etc/resolvconf/resolv.conf.d/original

dpkg --add-architecture i386

apt-get update
echo ""
echo "### make sure tuxedo-base-files and tuxedo-common-settings are installed ###"
echo ""

DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install tuxedo-base-files tuxedo-common-settings tuxedo-plymouth
#echo virtualbox-ext-pack virtualbox-ext-pack/license select true | debconf-set-selections
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes upgrade

if [ -f /mnt/plasma6 ]; then
    if [ -f /mnt/nvidia ]; then
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p6) tuxedo-nvidia-driver-550
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p6) tuxedo-nvidia-driver-550
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
        DEBIAN_FRONTEND=noninteractive apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')
	DEBIAN_FRONTEND=noninteractive apt-get -y autopurge
    else
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p6) plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p6) plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
        DEBIAN_FRONTEND=noninteractive apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')
	DEBIAN_FRONTEND=noninteractive apt-get -y autopurge
    fi
else
    if [ -f /mnt/nvidia ]; then
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p5) tuxedo-nvidia-driver-550 plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p5) tuxedo-nvidia-driver-550 plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
        DEBIAN_FRONTEND=noninteractive apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')
	DEBIAN_FRONTEND=noninteractive apt-get -y autopurge
    else
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p5) plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
	DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/paketliste-p5) plymouth-
	DEBIAN_FRONTEND=noninteractive apt-get -y -f install
        DEBIAN_FRONTEND=noninteractive apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')
	DEBIAN_FRONTEND=noninteractive apt-get -y autopurge
    fi
fi

export apt_ret=$?

echo ""
echo " ### apt run done ###"
echo ""

if [ -e /mnt/tomte ]; then
    echo ""
else
    DEBIAN_FRONTEND=noninteractive apt-get -y purge tuxedo-tomte
fi

#echo "#remove temporary sources"
#rm -rf /etc/apt/sources.list.d/
#rm -f /etc/apt/sources.list
#mv /etc/apt/sources.list.d.bak /etc/apt/sources.list.d/
#mv /etc/apt/sources.list.bak /etc/apt/sources.list

exit $?
