#!/usr/bin/env bash

D="/opt/MXB"

. "$D/sbin/common_utils-fp.sh"

[ "$(id -u)" != "0" ] && error "please run as root"

SURE=false

function parse_cmd_args {
    while [ $# -gt 0 ]
    do
        case $1 in
            -s | --sure)
                SURE=true
                ;;
            *)
                error "invalid command line parameters entered."
                ;;
        esac
        shift
    done
}

service_ctl=$(get_service_ctl)

sure_to_remove() # name, descriptiom
{
    local _name="$1"
    local _descr="$2"

    if [ $SURE = "true" ]; then
        return 0
    fi

    echo "Caution! $_name is a general purpose $_descr which could be used by other MXB programs installed on your system." | fold -s
    printf "Are you sure you want to remove it? (y/N) "
    read yn
    case "$yn" in
        [yY]|[yY][eE][sS])
            return 0
            ;;
    esac

    echo "Skipped removal of $_name."
    return 1
}

parse_cmd_args "$@"


if [ "0" -ne 0 ]; then
    "$D/bin/ClientTool" control.dashboard.unsubscribe
fi

echo "-- Stopping $PC_NAME"
$service_ctl stop "$PC_NAME"

echo "-- Removing BackupFP"
rm -f "$D/bin/BackupFP"
rm -f "$D/sbin/configure-fp.sh"
rm -f "$D/sbin/proxy_settings_utils-fp.sh"
rm -f "$D/sbin/uif_v1-fp.sh"
rm -f "$D/sbin/update_v1-fp.sh"
rm -f "$D/sbin/ct_utils-fp.sh"
rm -f "$D/sbin/ui_utils-fp.sh"
rm -f "$D/sbin/common_utils-fp.sh"
rm -fr "$D/var/log/BackupFP"
rm -fr "$D/var/dumps/BackupFP"
rm -fr "$D/var/storage"
rm -f "$D/var/SessionReport.xml"
rm -f "$D/var/StatusReport.xml"
rm -fr "$D/share/bm"

echo "-- Removing BackupUP"
rm -f "$D/bin/BackupUP"
rm -fr "$D/var/log/BackupUP"

echo "-- Removing AutoUpdate"
rm -f "$D/bin/AutoUpdate"
rm -fr "$D/var/log/AutoUpdate"
rm -fr "$D/var/update"

echo "-- Removing ClientTool"
rm -f "$D/bin/ClientTool"
rm -fr "$D/var/log/ClientTool"
rm -fr "$D/var/dumps/ClientTool"

echo "-- Removing xtrabackup"
rm -f "$D/bin/xtrabackup_8.0"
rm -f "$D/bin/xtrabackup_2.0_51"
rm -f "$D/bin/xtrabackup_2.4"

echo "-- Removing $PC_NAME"
if sure_to_remove "$PC_NAME" "service"; then
    $service_ctl uninstall "$PC_NAME"
    rm -f "/etc/tmpfiles.d/$PC_NAME-tmpfiles.conf"
    rm -f "$D/bin/$PC_NAME"
    rm -f "$D/etc/$PC_NAME.config"
    rm -f "$D/sbin/$PC_NAME"
    rm -fr "$D/var/log/$PC_NAME"
    rm -fr "$D/var/dumps/$PC_NAME"
    rm -f "$D/var/run/$PC_NAME.pid"
else
    echo "You will need to manually remove BackupFP from $PC_NAME.config."
fi

echo "-- Removing configuration files"
if sure_to_remove "Backup.Branding.config" "configuration file"; then
    rm -f "$D/etc/Backup.Branding.config"
fi

if sure_to_remove "config.ini" "configuration file"; then
    rm -f "$D/etc/config.ini"
    rm -f "$D/etc/config.ini~"
fi

echo "-- Removing BRMigrationTool"
rm -f "$D/bin/BRMigrationTool"
rm -fr "$D/var/log/BRMigrationTool"

echo "-- Removing AuthTool"
rm -f "$D/bin/AuthTool"
rm -f "$D/etc/AuthTool.config"

echo "-- Removing OpenSslModules"
rm -fr "$D/lib/OpenSslModules"

echo "-- Removing MySQL tools"
find "$D/bin/tools" -type f -name "mysql*" -delete

echo "-- Removing Relax-and-Recover"
rm -fr "$D/rear"
rm -fr "$D/var/log/rear"
rm -fr "$D/var/lib/rear"

echo "-- Removing miscellaneous files"
rm -f "$D/bin/dmidecode"

echo "-- Removing temporary locations"
rm -fr "$D/temp"
rm -fr "$D/var/tmp"

echo "-- Removing uninstall script"
rm -f "$0"

echo "-- Removing empty directories"
find -L "$D" -type d | sort -r | xargs -n1 rmdir 2>/dev/null

if $service_ctl exists "$PC_NAME"; then
    echo "-- Starting $PC_NAME"
    $service_ctl start "$PC_NAME"
fi

echo "-- Done"
