#!/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
# install.sh — fetch the v2 distrobox binary from GitHub releases for the
# detected architecture, then pull man pages, shell completions, and icons
# from the source archive at the same tag. To undo, run ./uninstall.
#
# Intended as a one-liner curl install for users who don't want to build
# from source. If you have Go and a checkout, prefer `make install`.

REPO="89luca89/distrobox"
# Bump on every release. Override with --version to install a different tag.
VERSION="2.0.0-rc.1"
prefix=""
no_color=0
verbose=0

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

Options:
	--prefix/-P:	base path where files will be deployed
			(default: /usr/local if root, ~/.local otherwise)
	--version/-V:	release tag to install (default: ${VERSION})
	--no-color:	disable color formatting
	--help/-h:	show this message
	-v/--verbose:	show more verbosity

Requires: tar plus either curl or wget. Detected architecture must be
amd64 or arm64 (matching the published release artefacts).

To uninstall, use the companion ./uninstall script.
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
			;;
		-V | --version)
			if [ -n "${2:-}" ]; then
				VERSION="$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=""
BOLD_RED=""
CLEAR=""
if [ -t 0 ] && [ -t 1 ] && [ "${no_color}" -ne 1 ]; then
	BOLD_GREEN='\033[1;32m'
	BOLD_RED='\033[1;31m'
	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"

if command -v curl > /dev/null 2>&1; then
	download="curl -sLfo"
elif command -v wget > /dev/null 2>&1; then
	download="wget -qO"
else
	printf >&2 "Error: need either curl or wget on PATH\n"
	exit 1
fi
if ! command -v tar > /dev/null 2>&1; then
	printf >&2 "Error: need tar on PATH\n"
	exit 1
fi

case "$(uname -m)" in
	x86_64 | amd64) arch="amd64" ;;
	aarch64 | arm64) arch="arm64" ;;
	*)
		printf >&2 "Error: unsupported architecture %s (release artefacts are linux-amd64 and linux-arm64)\n" "$(uname -m)"
		exit 1
		;;
esac

printf >&2 "%b Installing distrobox %s for linux-%s into %s%b\n" \
	"${BOLD_GREEN}" "${VERSION}" "${arch}" "${prefix}" "${CLEAR}"

tmp="$(mktemp -d)"
# shellcheck disable=SC2064
trap "rm -rf '${tmp}'" EXIT
cd "${tmp}"

printf >&2 "%b Downloading binary...%b\n" "${BOLD_RED}" "${CLEAR}"
# shellcheck disable=SC2086
${download} distrobox "https://github.com/${REPO}/releases/download/${VERSION}/distrobox-linux-${arch}"

printf >&2 "%b Downloading man pages, completions, and icons...%b\n" "${BOLD_RED}" "${CLEAR}"
# shellcheck disable=SC2086
${download} src.tar.gz "https://github.com/${REPO}/archive/refs/tags/${VERSION}.tar.gz"
mkdir -p src
tar -xzf src.tar.gz -C src --strip-components=1

if ! install -d "${bindir}" "${mandir}" "${bashdir}" "${zshdir}"; then
	printf >&2 "Do you have permission to write to %s?\n" "${prefix}"
	exit 1
fi

install -m 0755 distrobox "${bindir}/distrobox"
install -m 0644 src/man/man1/*.1 "${mandir}/"
install -m 0644 src/completions/bash/distrobox "${bashdir}/distrobox"
install -m 0644 src/completions/zsh/_distrobox  "${zshdir}/_distrobox"

install -d "${icondir}/scalable/apps"
install -m 0644 src/icons/terminal-distrobox-icon.svg "${icondir}/scalable/apps/"
for sz in ${icon_sizes}; do
	install -d "${icondir}/${sz}x${sz}/apps"
	install -m 0644 "src/icons/hicolor/${sz}x${sz}/apps/terminal-distrobox-icon.png" \
		"${icondir}/${sz}x${sz}/apps/"
done

printf >&2 "%b Installation successful!%b\n" "${BOLD_GREEN}" "${CLEAR}"
printf >&2 "%b distrobox is at %s%b\n" "${CLEAR}" "${bindir}/distrobox" "${CLEAR}"
if ! echo "${PATH}" | grep -q "${bindir}"; then
	printf >&2 "%b Note:%b add %s to your \$PATH\n" "${BOLD_RED}" "${CLEAR}" "${bindir}"
fi
