#!/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")
if [ -r $CHROOT/etc/sddm.conf.d/kde_settings.conf ]; then
    sed -i 's/siducer//g' $CHROOT/etc/sddm.conf.d/kde_settings.conf
fi

purge_pkg() {
    apt 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
    purge_pkg xserver-xorg-video-vmware
}

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
