#!/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
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
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
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes purge --allow-remove-essential $(dpkg -l | awk '/i386/{print $2}')
echo ""
echo "### remove plasma packages ###"
echo ""
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes purge libqt5* xterm+ policykit-1-gnome-
DEBIAN_FRONTEND=noninteractive apt-get -y install apt-show-versions
apt-get update
apt-show-versions | grep -E "No avail|newer|upgradeable" | awk -F\: '{print$1}' > /mnt/remove.txt
echo ""
echo "### remove all packages which not exist in any repo ###"
echo ""
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes purge $(</mnt/remove.txt)
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 --reinstall tuxedo-base-files tuxedo-common-settings
echo virtualbox-ext-pack virtualbox-ext-pack/license select true | debconf-set-selections
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install $(</mnt/install.txt)
DEBIAN_FRONTEND=noninteractive apt-get -y purge $(dpkg -l | grep ^rc | awk '{print $2}')
DEBIAN_FRONTEND=noninteractive apt-get -y purge apt-show-versions
DEBIAN_FRONTEND=noninteractive apt-get -y autopurge

rm -f /mnt/remove.txt
rm -f /mnt/install.txt

export apt_ret=$?


if [ -e /mnt/tomte ]
    then
    echo ""
    echo " ### run tomte configure all ###"
    echo ""
    rm -f /mnt/tomte
    /mnt/tomte_run.sh

    echo ""
    echo " ### tomte done ###"
    echo ""
else
    DEBIAN_FRONTEND=noninteractive apt-get -y purge tuxedo-tomte
fi

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

#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 $?
