#!/bin/bash

#This script is developed and is maintained by Kevin Chevreuil - Kaisen for the Kaisen Linux project and is under AGPL license.

export PATH=/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/local/sbin

export DEBIAN_FRONTEND=noninteractive
DATE=$(date '+%Y-%m-%d_%H-%M-%S')
YELLOW='\e[0;33m'
GREEN='\e[1;32m'
RED='\e[1;31m'
BLUE='\e[1;36m'
RESET_COLOR='\033[0m'

#Check root execution
if [[ "$EUID" -ne "0" ]]; then
        echo -e $RED "Run this screen as root or with the sudo command to continue." $RESET_COLOR
        exit 1
fi

#Check GUI is using
ps -e | grep -o 'kded6' > /dev/null
    if [ $? -eq 0 ]; then
    DESKTOP="KDE"
    fi
ps -e | grep -o 'lxqt-panel' > /dev/null
    if [ $? -eq 0 ]; then
    DESKTOP="LXQT"
    fi
ps -e | grep -o 'mate-panel' > /dev/null
    if [ $? -eq 0 ]; then
    DESKTOP="MATE"
    fi
ps -e | grep -o 'xfce4-panel' > /dev/null
    if [ $? -eq 0 ]; then
    DESKTOP="XFCE"
    fi

if [[ $DESKTOP != KDE && $DESKTOP != LXQT && $DESKTOP != MATE && $DESKTOP != XFCE ]]; then
	echo -e $RED "To use this script, you must use KDE, LXQT, MATE or XFCE." $RESET_COLOR
	exit 1
fi

echo -e $YELLOW "Check APT access..." $RESET_COLOR
apt-get update 2>&1 | grep '^[WE]:' && echo -e $RED "Please check your internet connection or APT access to continue." $RESET_COLOR && exit 1
echo ""
echo -e $GREEN "APT access ok.\e[0;33m Check connected network interface..." $RESET_COLOR
echo ""
ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}' | grep '^[w]l' 1> /dev/null && echo -e $RED "Disable wifi and connect an Ethernet cable or USB modem to continue." $RESET_COLOR && exit 1
echo -e $GREEN "Checks complete." $RESET_COLOR
echo ""
echo -e $GREEN "Welcome at Kaisen Linux interface switcher." $RESET_COLOR
echo ""
echo -e $GREEN "This script will install a graphical interface of your choice listed below." $RESET_COLOR
echo ""
echo -e $YELLOW "CAUTION: This script will automatically restart the computer at the end of the process." $RESET_COLOR
echo -e $YELLOW "Your unsaved work will be lost." $RESET_COLOR
echo -e $YELLOW "It is advisable to increase the duration or deactivate standby." $RESET_COLOR
echo ""
echo -e $BLUE "Which graphical interface do you want to install?" $RESET_COLOR
echo ""

option_selection() {
	while [[ $OPTION != "1" && $OPTION != "2" && $OPTION != "3" && $OPTION != "4" ]]; do
		read -rp "$(echo -e $YELLOW "Select an option \e[1;36m[1-4]\e[0;33m:\e[0m ")" OPTION
	done
}

log() {
mkdir -p /var/log/kaisen-interface-switcher 2> /dev/null || true
exec > >(tee /var/log/kaisen-interface-switcher/kaisen-interface-switcher-KDE-$DATE.log) 2>&1
}

ui_uninstallation_echo() {
echo ""
echo -e $GREEN "${NEW_DESKTOP} selected... Removal of ${DESKTOP} packages..." $RESET_COLOR
echo ""
}

desktop_remove() {
case ${DESKTOP} in
	KDE)
		apt-get autoremove --purge -y `dpkg -l | grep kde | cut -d \  -f 3` && dhclient 2> /dev/null
	;;
	LXQT)
		apt-get autoremove --purge -y `dpkg -l | grep 'lxqt\|qterminal' | cut -d \  -f 3` && dhclient 2> /dev/null
	;;
	MATE)
		apt-get autoremove --purge -y `dpkg -l | grep 'mate-\|dconf-editor' | cut -d \  -f 3` && dhclient 2> /dev/null
	;;
	XFCE)
		apt-get autoremove --purge -y `dpkg -l | grep 'xfce\|gedit' | cut -d \  -f 3` && dhclient 2> /dev/null
	;;
esac
}

ui_installation_echo() {
echo ""
echo -e $GREEN "${NEW_DESKTOP} packages installation..." $RESET_COLOR
echo ""
}

desktop_installation() {
case ${NEW_DESKTOP} in
	KDE)
		apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-kde || apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-kde && systemctl enable sddm
	;;
	LXQT)
		apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-lxqt || apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-lxqt && systemctl enable lightdm
	;;
	MATE)
		apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-mate || apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-mate && systemctl enable lightdm
	;;
	XFCE)
		apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-xfce || apt-get -qy -o "Dpkg::Options::=--force-confnew" install kaisen-xfce && systemctl enable lightdm
	;;
esac
}

reboot() {
exec /sbin/reboot
}

steps() {
log
ui_uninstallation_echo
desktop_remove
ui_installation_echo
desktop_installation
reboot
}

user_exit() {
if [[ $OPTION = "4" ]]; then
	exit 0
fi
}

##### KDE #####
if [[ $DESKTOP = KDE ]]; then
	echo -e $BLUE "   1) XFCE"
	echo -e $BLUE "   2) LXQT"
	echo -e $BLUE "   3) MATE"
	echo -e $BLUE "   4) EXIT"
	echo ""

	option_selection

if [[ $OPTION = "1" ]]; then
	NEW_DESKTOP=XFCE
	steps
fi

if [[ $OPTION = "2" ]]; then
	NEW_DESKTOP=LXQT
	steps
fi

if [[ $OPTION = "3" ]]; then
	NEW_DESKTOP=MATE
	steps
fi

user_exit
fi

##### LXQT #####
if [[ $DESKTOP = LXQT ]]; then
	echo -e $BLUE "   1) KDE"
	echo -e $BLUE "   2) MATE"
	echo -e $BLUE "   3) XFCE"
	echo -e $BLUE "   4) EXIT"
	echo ""

	option_selection

if [[ $OPTION = "1" ]]; then
	NEW_DESKTOP=KDE
	steps
fi

if [[ $OPTION = "2" ]]; then
	NEW_DESKTOP=MATE
	steps
fi

if [[ $OPTION = "3" ]]; then
	NEW_DESKTOP=XFCE
	steps
fi

user_exit
fi

##### MATE #####
if [[ $DESKTOP = MATE ]]; then
	echo -e $BLUE "   1) KDE"
	echo -e $BLUE "   2) LXQT"
	echo -e $BLUE "   3) XFCE"
	echo -e $BLUE "   4) EXIT"
	echo ""

	option_selection

if [[ $OPTION = "1" ]]; then
	NEW_DESKTOP=KDE
	steps
fi

if [[ $OPTION = "2" ]]; then
	NEW_DESKTOP=LXQT
	steps
fi

if [[ $OPTION = "3" ]]; then
	NEW_DESKTOP=XFCE
	steps
fi

user_exit
fi

##### XFCE #####
if [[ $DESKTOP = XFCE ]]; then
	echo -e $BLUE "   1) KDE"
	echo -e $BLUE "   2) LXQT"
	echo -e $BLUE "   3) MATE"
	echo -e $BLUE "   4) EXIT"
	echo ""

	option_selection

if [[ $OPTION = "1" ]]; then
	NEW_DESKTOP=KDE
	steps
fi

if [[ $OPTION = "2" ]]; then
	NEW_DESKTOP=LXQT
	steps
fi

if [[ $OPTION = "3" ]]; then
	NEW_DESKTOP=MATE
	steps
fi

user_exit
fi
