#!/usr/bin/env sh
#
# The script deletes current directory and launchers
# version 07.06.2021 by Хрюнделёк
# https://yadi.sk/i/6XsWyS6O-4YKRA

# Initial set
dir="$(dirname "$(readlink -f "$0")")"
app="$(basename "$dir")"

# User environment launchers
if [ -z "$XDG_CONFIG_HOME" ]; then
	XDG_CONFIG_HOME=$HOME/.config
fi
# shellcheck disable=SC1091
. "$XDG_CONFIG_HOME"/user-dirs.dirs 2>/dev/null

if [ -z "$XDG_DATA_HOME" ]; then
	XDG_DATA_HOME=$HOME/.local/share
fi

if [ -z "$XDG_DESKTOP_DIR" ]; then
	XDG_DESKTOP_DIR=$HOME/Desktop
fi
desklauncher=$XDG_DESKTOP_DIR/$app.desktop
menudir=$XDG_DATA_HOME/applications
menulauncher=$menudir/$app.desktop
suf="[^[:space:]]+"
string="Exec="\"$dir"$suf"

# Localization
if (locale | grep -e 'ru_RU' >/dev/null); then
	title="Удаление"
	msg="Удалить"
	msg2="Удаляется"
	msg3="Готово.
Файлы настроек (сохранений) обычно находятся в ~/.config или/и ~/. \
local.
В таком случае, они не были удалены."
	canc="Отмена."
else
	title="Uninstall"
	msg="Do you want to uninstall"
	msg2="Uninstalling"
	msg3="Done.
Configuration files (saved user files) are usually in the ~/.config \
or/and ~/.local.
In this case they were not deleted."
	canc="Canceled."
fi

# Prompt colors
red="\033[1;31m"
green="\033[1;32m"
reset="\033[0m"

# Check zenity & kdialog, otherwise run in the terminal
if command -v zenity >/dev/null; then
	if zenity --question --title="$title $app" --text="$msg $app?" \
--no-wrap >/dev/null 2>&1; then

		# delete the same path launchers only
		if [ -f "$menulauncher" ]; then
			if \grep -E "$string" "$menulauncher" >/dev/null 2>&1; then
				rm -f "$menulauncher"
			fi
		fi
		if [ -f "$desklauncher" ]; then
			if \grep -E "$string" "$desklauncher" >/dev/null 2>&1; then
				rm -f "$desklauncher"
			fi
		fi
		cd "$dir"/.. || exit
		rm -rf "$dir" | zenity --progress --title="$title $app" \
--text="$msg2 $app..." --width=300 --pulsate --no-cancel --auto-close \
>/dev/null 2>&1
	else
		exit
	fi
	zenity --info --title="$title $app" --text="$msg3" --no-wrap \
>/dev/null 2>&1
	exit
elif command -v kdialog >/dev/null; then
	if kdialog --yesno "$msg $app?" --title="$title $app" >/dev/null \
2>&1; then

		# delete the same path launchers only
		if [ -f "$menulauncher" ]; then
			if \grep -E "$string" "$menulauncher" >/dev/null 2>&1; then
				rm -f "$menulauncher"
			fi
		fi
		if [ -f "$desklauncher" ]; then
			if \grep -E "$string" "$desklauncher" >/dev/null 2>&1; then
				rm -f "$desklauncher"
			fi
		fi
		cd "$dir"/.. || exit
		rm -rf "$dir"
	else
		exit
	fi
	kdialog --msgbox "$msg3" --title="$title $app" >/dev/null 2>&1
	exit
else
	clear
	printf "\n%s$msg $app? (y)es/(n)o:\n\n"
	read -r answer
	case "$answer" in
		[y])
			printf "\n%s${red}$msg2 $app...${reset}\n"

			# delete the same path launchers only
			if [ -f "$menulauncher" ]; then
				if \grep -E "$string" "$menulauncher" >/dev/null 2>&1; then
					rm -f "$menulauncher"
				fi
			fi
			if [ -f "$desklauncher" ]; then
				if \grep -E "$string" "$desklauncher" >/dev/null 2>&1; then
					rm -f "$desklauncher"
				fi
			fi
			cd "$dir"/.. || exit
			rm -rf "$dir"
			printf "\n%s${green}$msg3${reset}\n\n"
			;;
		*)
			printf "\n%s${red}$canc${reset}\n\n"
			exit
	esac
fi
exit 0
