#!/bin/bash

trap "exit 1" TERM
export TOP_PID=$$

if [[ $UID != 0 ]]; then
    echo "Please start the script as root or sudo."
    exit 1
fi

function test {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo "Fatal error with $@" >&2
		kill -s TERM $TOP_PID
	else
	    printf "\n[  OK  ]\n"
    fi
    return $status
}


DISTRO=$(lsb_release -is)
CODENAME=$(lsb_release -cs)

#Uninstall docker
read -r -p "Do you want to uninstall Docker? [y/N] " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        	case $DISTRO in
		    "Ubuntu" | "CentOS" | "RedHat"  | "Debian"  | "Fedora"  | "openSUSE" )
				echo "Uninstalling docker..."
				test service docker start
				test /etc/init.d/docker start	
				test apt-get purge -y docker*
				test apt-get autoremove -y --purge docker*
				test apt-get autoclean
				rm -rf /var/lib/docker
				rm /etc/apparmor.d/docker
				test groupdel docker				
			;;
		   "" )
			echo "Please install lsb_release and try again. Package redhat-lsb-core, lsb-release or similar."
				exit;
			;;
		    * )
		        echo "Linux version $DISTRO-$CODENAME is not compatible with this uninstall method"
				exit
			;;
		esac
        ;;
    *)
        
        ;;
esac

#Uninstall xpra
read -r -p "Do you want to uninstall XPRA? [y/N] " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        	case $DISTRO in
		    "Ubuntu" | "CentOS" | "RedHat"  | "Debian"  | "Fedora"  | "openSUSE" )
		        test apt-get install -y curl
				rm /etc/apt/sources.list.d/winswitch.list
				test apt-get purge -y  xpra
				test apt-get autoremove -y --purge xpra
				test apt-get autoclean
			;;
		   "" )
			echo "Please install lsb_release and try again. Package redhat-lsb-core, lsb-release or similar."
				exit;
			;;
		    * )
		        echo "Linux version $DISTRO-$CODENAME is not compatible with this uninstall method"
				exit
			;;
		esac
        ;;
    *)
        
        ;;
esac	

#Uninstall DEWE
echo "Uninstalling DEWE..."
rm /usr/local/bin/dewe
rm -rf /opt/DEWE/
rm /usr/share/applications/dewe.desktop
