#!/bin/sh
# -*- coding: utf-8 -*-
#
#   Copyright 2017, Alf Gaida <agaida@siduction.org>
#
#   This module 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 module 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 module. If not, see <http://www.gnu.org/licenses/>.

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")
noblob=""
if [ $(grep noblob /proc/cmdline) ]; then
	noblob="yes"
fi

if [ -r $CHROOT/etc/sddm.conf.d/kde_settings.conf ]; then
    sed -i 's/User=tux/User=/' $CHROOT/etc/sddm.conf.d/kde_settings.conf
fi

user=$(ls /home/)

sed -i "s/tux/$user/" /etc/NetworkManager/system-connections/*.nmconnection

apt --assume-yes install /usr/share/tuxedo/packages/tuxedo-grub-theme*.deb
if [ -d /boot/efi ]; then
    apt --assume-yes install /usr/share/tuxedo/packages/tuxedo-webfai*.deb
fi

rm -rf /usr/share/tuxedo/packages
apt-get --assume-yes purge attr cpufrequtils cpuid dialog ext4magic ethtool fakeroot fbset gddrescue i2c-tools memtester squashfs-tools sysstat testdisk libelf-dev zlib1g-dev --autoremove

sed -i "s/yes yes/yes no/" /etc/tomte/tomte.cfg

if [ "$noblob" = "yes" ]; then
	sed -i '/nvidia-driver/d' /etc/tomte/tomte.cfg
	echo nvidia-driver 5 yes yes yes 1 >> /etc/tomte/tomte.cfg
fi


xdg-desktop-menu uninstall chroot-to-install.desktop
xdg-desktop-menu uninstall automated_repair.desktop
xdg-desktop-menu uninstall TUXEDO.directory calamares.desktop
xdg-desktop-menu uninstall calamares.desktop

rm -f /usr/local/share/applications/calamares.desktop

purge_pkg() {
    apt-get purge $1 --assume-yes --autoremove || true
}

remove_qemu() {
    purge_pkg qemu-guest-agent
    purge_pkg qemu-system-common
    purge_pkg qemu-system-x86
    purge_pkg qemu-utils
}

remove_vmware() {
    purge_pkg open-vm-tools
    purge_pkg open-vm-tools-desktop
    purge_pkg open-vm-tools-dkms
}

remove_spice() {
    purge_pkg spice-vdagent
    purge_pkg xserver-xorg-video-qxl
}

remove_virtualbox() {
    purge_pkg virtualbox-guest-dkms
    purge_pkg virtualbox-guest-utils
    purge_pkg virtualbox-guest-x11
}


# test bare metal
i=$(virt-what)
[ "x$i" = "x" ] && isBare=true


# test misc virts
for i in $(virt-what); do
    echo "Test auf i=$i"
    [ "x$i" = "xkvm" ] && isKVM=true
    [ "x$i" = "xqemu" ] && isQemu=true
    [ "x$i" = "xvirtualbox" ] && isVirtualBox=true
    [ "x$i" = "xvmware" ] && isVMWare=true
done

[ $isBare ] && virt=none
[ $isKVM ] &&  virt=kvm
[ $isQemu ] && virt=qemu
[ $isVMWare ] && virt=vmware
[ $isVirtualBox ] && virt=virtualbox

case ${virt} in
    "kvm")
        remove_vmware
        remove_virtualbox
        ;;
    "qemu")
        echo "in qemu"
        remove_vmware
        remove_virtualbox
        ;;
    "virtualbox")
        remove_vmware
        remove_qemu
        remove_spice
        ;;
    "vmware")
        echo "in vmware"
        remove_qemu
        remove_spice
        remove_virtualbox
        ;;
    "none")
        remove_qemu
        remove_vmware
        remove_spice
        remove_virtualbox
        ;;
    *)
        echo "Toter Hund angebunden! Please file a bug in"
        echo "https://git.siduction.org/extra/calamares-settings-siduction.git"
        echo "or in https://forum.siduction.org"
        ;;
esac

exit 0
