#!/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
# uninstall.sh — remove distrobox (v1 legacy or v2 Go) from the given
# prefix. The prefix must match the one used at install time (default:
# /usr/local for root, ~/.local otherwise). Both the v1 split-script
# layout and the v2 single-binary layout are wiped in a single pass,
# so you don't need to remember which version you installed.

prefix=""
no_color=0
verbose=0

show_help()
{
	cat << EOF
uninstall --prefix /usr/local

Removes distrobox (v1 or v2) from the given prefix.

Options:
	--prefix/-P:	base path where distrobox was installed
			(default: /usr/local if root, ~/.local otherwise)
	--no-color:	disable color formatting
	--help/-h:	show this message
	-v/--verbose:	show more verbosity
EOF
}

while [ $# -gt 0 ]; do
	case $1 in
		-h | --help)
			show_help
			exit 0
			;;
		-v | --verbose)
			verbose=1
			shift
			;;
		--no-color)
			no_color=1
			shift
			;;
		-P | --prefix)
			if [ -n "${2:-}" ]; then
				prefix="$2"
				shift 2
			else
				show_help
				exit 1
			fi
			;;
		*)
			printf >&2 "Error: unknown argument %s\n" "$1"
			show_help
			exit 1
			;;
	esac
done

set -o errexit
set -o nounset

BOLD_GREEN=""
CLEAR=""
if [ -t 0 ] && [ -t 1 ] && [ "${no_color}" -ne 1 ]; then
	BOLD_GREEN='\033[1;32m'
	CLEAR='\033[0m'
fi
if [ "${verbose}" -ne 0 ]; then
	set -o xtrace
fi

if [ -z "${prefix}" ]; then
	prefix="/usr/local"
	if [ "$(id -u)" -ne 0 ]; then
		prefix="${HOME}/.local"
	fi
fi

bindir="${prefix}/bin"
mandir="${prefix}/share/man/man1"
bashdir="${prefix}/share/bash-completion/completions"
zshdir="${prefix}/share/zsh/site-functions"
icondir="${prefix}/share/icons/hicolor"

icon_sizes="16 22 24 32 36 48 64 72 96 128 256"

rm -f "${bindir}/distrobox"
# `distrobox-*` matches v1 split scripts and v2 argv[0] symlinks alike
# (the helpers distrobox-init/export/host-exec are also picked up here).
rm -f "${bindir}"/distrobox-*
rm -f "${mandir}/distrobox.1" "${mandir}"/distrobox-*.1
# v1 ships per-subcommand bash/zsh completions; the v2 release only
# ships a single `distrobox` / `_distrobox`. The globs cover both.
rm -f "${bashdir}/distrobox" "${bashdir}"/distrobox-*
rm -f "${zshdir}/_distrobox" "${zshdir}"/_distrobox-*
rm -f "${icondir}/scalable/apps/terminal-distrobox-icon.svg"
for sz in ${icon_sizes}; do
	rm -f "${icondir}/${sz}x${sz}/apps/terminal-distrobox-icon.png"
done

printf >&2 "%b Uninstalled distrobox from %s%b\n" "${BOLD_GREEN}" "${prefix}" "${CLEAR}"
