#!/bin/bash

# This file is part of the Bodhi Linux Web Browser Manager.
#
# License: GPL-3.0 or later
#
# (c) Bodhi Linux Team (2022-2023)
# (c) Feren Team (2015-2020) <ferenosdev@outlook.com>
# (c) AZorin 2010-(2015) <azorin@zoringroup.com>
# (c) Zorink (2010) <zorink@zoringroup.com>

# shellcheck disable=SC2181

# Check if program is installed
dpkg -l waterfox-g-kpe | grep -q ^ii

if [ ! $? -eq 0 ]; then
    if [ ! -e "/usr/bin/waterfox-g" ]; then
        zenity --info --title="Information" --text="Waterfox is not installed" --no-wrap &
        exit 0
    fi
fi

# Check for root
if [ ! "$(whoami)" = "root" ]; then
    zenity --question --title="Continue?" --text "Are you sure you want to remove Waterfox?" --no-wrap

    if [ ! $? -eq 0 ]; then
        exit 0
    fi

    pkexec "$0" $@

    if [ $? -eq 127 ]; then
        exit 0
    fi

    # Double Check Package
    dpkg -l waterfox-g-kpe |grep -q ^ii

    if [ ! $? -eq 0 ]; then
        if [ ! -e "/usr/bin/waterfox-g" ]; then
            zenity --info --title="Uninstalled Waterfox" --text="Waterfox was successfully removed" --no-wrap &
            exit 0
        fi
        # Error
        zenity  --error --text="Web Browser Manager couldn't remove Waterfox successfully

A log can be found at File System/tmp/waterfox-removal-log" --title="Error" --no-wrap
        exit 0
    fi
fi

# Check if program is installed
dpkg -l waterfox-g-kpe | grep -q ^ii

if [ $? -eq 0 ]; then
    if [ -e "/usr/bin/waterfox-g" ]; then
        #Log output
        exec 1>/tmp/waterfox-removal-log 2>&1
        # Uninstall program
        apt-get -y purge waterfox-g-kpe
        # Remove PPA
        rm -f /usr/share/keyrings/waterfox-archive-keyring.gpg
        rm -f /etc/apt/sources.list.d/waterfox.list
    fi
    exit 0
fi

# Error
zenity  --error --text="An error occured while initiating the Waterfox removal process" --title="Error" --no-wrap &
exit 0
