#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project: https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.

# POSIX

no_color=0
verbose=0

# Print usage to stdout.
# Arguments:
#   None
# Outputs:
#   print usage with examples.
show_help()
{
	cat << EOF
uninstall --prefix /usr/local

Options:
	--no-color:		disable color formatting
	--prefix/-P:		base bath where all files will be deployed (default /usr/local if root, ~/.local if not)
	--help/-h:		show this message
	-v:			show more verbosity
EOF
}

# Parse arguments
while :; do
	case $1 in
		-h | --help)
			# Call a "show_help" function to display a synopsis, then exit.
			show_help
			exit
			;;
		--no-color)
			shift
			no_color=1
			;;
		-v | --verbose)
			shift
			verbose=1
			;;
		-p | --path)
			if [ -n "$2" ]; then
				dest_path="$2"
				shift
				shift
			fi
			;;
		-P | --prefix)
			if [ -n "$2" ]; then
				prefix="$2"
				shift
				shift
			fi
			;;
		*) # Default case: If no more options then break out of the loop.
			break ;;
	esac
done

# if we're in not a tty, don't use colors
BOLD_GREEN=""
BOLD_RED=""
CLEAR=""
if [ -t 0 ] && [ -t 1 ] && [ "${no_color}" -ne 1 ]; then
	# we're in a tty, use colors
	BOLD_GREEN='\033[1;32m'
	BOLD_RED='\033[1;31m'
	CLEAR='\033[0m'
fi

if  [ -z "${prefix}" ]; then
	prefix="/usr/local"
	# in case we're not root, just default to the home directory
	if [ "$(id -u)" -ne 0 ]; then
		prefix="${HOME}/.local"
	fi
fi
dest_path="${prefix}/bin"
man_dest_path="${prefix}/share/man/man1"
icon_dest_path="${prefix}/share/icons"
completion_dest_path="${prefix}/share/bash-completion/completions/"

set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

[ ! -w "${dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${dest_path}" && exit 1
[ ! -w "${man_dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${man_dest_path}" && exit 1

# uninstall
for file in "${dest_path}/distrobox"*; do
	[ -e "${file}" ] && rm "${file}"
done
for file in "${man_dest_path}/distrobox"*; do
	[ -e "${file}" ] && rm "${file}"
done
for file in "${completion_dest_path}/distrobox"*; do
	[ -e "${file}" ] && rm "${file}"
done
[  -e "${icon_dest_path}"/terminal-distrobox-icon.svg ] && rm "${icon_dest_path}"/terminal-distrobox-icon.svg
[  -e "${icon_dest_path}"/distrobox ] && rm -rf "${icon_dest_path}"/distrobox

printf >&2 "%b Thank you for using Distrobox. Uninstall complete.\n%b" "${BOLD_GREEN}" "${CLEAR}"
printf >&2 "%b Removed shell scripts located in %b%s\n%b" "${CLEAR}" "${BOLD_RED}" "${dest_path}" "${CLEAR}"
printf >&2 "%b Removed manpages located in %b%s\n%b" "${CLEAR}" "${BOLD_RED}" "${man_dest_path}" "${CLEAR}"
