#!/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
user=$(ls /home/)

sed -i "s/tux/"$user"/" /etc/NetworkManager/system-connections/*.nmconnection
sed -i "s/User=tux/User=/" "$CHROOT"/etc/sddm.conf.d/kde_settings.conf


# wird nur ausgeführt, wenn keine geführte crypt install
if [ ! -f /root/crypt ]; then
    if [ -d /boot/efi ]; then
	grub-install --bootloader-id=TxoS_on_Debian
	dpkg -i /usr/share/tuxedo/packages/tuxedo-webfai*.deb
    fi
    dpkg -i /usr/share/tuxedo/packages/tuxedo-grub-theme*.deb
    rm -rf /usr/share/tuxedo/packages
fi

update-grub

echo "apt-get --assume-yes purge attr cpufrequtils cpuid dialog ext4magic fakeroot fbset gddrescue memtester squashfs-tools sysstat testdisk powerdevil+"
apt-get --assume-yes purge attr cpufrequtils cpuid dialog ext4magic fakeroot fbset gddrescue memtester squashfs-tools sysstat testdisk powerdevil+


# default tomte settings
tomte unblock all
tomte block text-boot

if [ "$noblob" = "yes" ]; then
    tomte block nvidia-driver
fi

# locale fix
echo "dpkg-reconfigure -f noninteractive keyboard-configuration"
dpkg-reconfigure -f noninteractive keyboard-configuration
dpkg-reconfigure -f noninteractive tuxedo-grub-theme

# remove live only packages from tuxedo menu
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

# remove calamares desktop icon
rm -f /usr/local/share/applications/calamares.desktop
rm -f /home/"$user"/*/calamares.desktop
rm -f /home/"$user"/.local/share/user-places.*

#fix fstab for btrfs
sed -i 's/subvol=\/@/subvol=@/g' /etc/fstab

purge_pkg() {
    apt-get purge $1 --assume-yes || 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
