#!/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

next=0
verbose=0

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

Options:
	--prefix/-P:		base bath where all files will be deployed (default /usr/local if root, ~/.local if not)
	--path/-p:		(DEPRECATED) path where to deploy the files (default /usr/local/bin if root, ~/.local/bin 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
			;;
		-v | --verbose)
			shift
			verbose=1
			;;
		-p | --path)
			if [ -n "$2" ]; then
				dest_path="$2"
				shift
				shift
			fi
			;;
		-N | --next)
			if [ -n "$2" ]; then
				shift
				next=1
			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 [ -n "${dest_path}" ] && [ -n "${prefix}" ]; then
	printf >&2 "Both -p and -P are set. Cannot continue. Exiting.\n"
	exit 1
fi

if [ -z "${dest_path}" ]; then
	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"
else
	printf >&2 "Warning: -p/--path is deprecated. Please refer to the documentation and switch to -P/--prefix.\n"
	# dest_path is already set
	man_dest_path="${dest_path}/../share/man/man1"
fi

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

# get current dir
curr_dir=$(dirname "$0")
cd "${curr_dir}" || exit 1

# if files are available, install files in dest directory
# else download targz and uncompress it
if [ -e "${curr_dir}/distrobox-enter" ]; then
	for file in distrobox*; do
		if ! install -D -m 0755 -t "${dest_path}" "${file}"; then
			printf >&2 "Do you have permission to write to %s?\n" "${dest_path}"
			exit 1
		fi
	done
	if [ -e "man" ]; then
		for file in man/man1/*; do
			install -D -m 0644 -t "${man_dest_path}" "${file}"
		done
	fi
else
	# check that we have base dependencies
	if ! command -v curl || ! command -v tar; then
		printf >&2 "Online install depends on curl and tar\n"
		exit 1
	fi

	if [ "${next}" -eq 0 ]; then
		release_ver=$(curl -L https://github.com/89luca89/distrobox/releases/latest |
			grep 'refs/tags' | tail -1 | cut -d'"' -f2)
		release_name=$(echo "${release_ver}" | rev | cut -d'/' -f1 | rev)
	else
		release_ver="89luca89/distrobox/archive/refs/heads/main.tar.gz"
		release_name="main"
	fi
	# go in tmp
	tmp_dir="$(mktemp -d)"
	cd "${tmp_dir}"
	# download our target
	curl -L "https://github.com/${release_ver}" -o "${release_name}"
	# uncompress
	tar xvf "${release_name}"
	# deploy our files
	for file in "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/distrobox*; do
		if ! install -D -m 0755 -t "${dest_path}" "${file}"; then
			printf >&2 "Do you have permission to write to %s?\n" "${dest_path}"
			exit 1
		fi
	done
	if [ -e "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')/man/" ]; then
		for file in "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/man/*; do
			install -D -m 0644 -t "${man_dest_path}" "${file}"
		done
	fi

	# securely delete unneeded files
	cd
	if [ -n "${tmp_dir}" ] && [ -e "${tmp_dir}" ]; then
		rm -rf "${tmp_dir}"
	fi
fi

printf >&2 "Successfully installed to %s.\n" "${dest_path}"
printf >&2 "Be sure that %s is in your \$PATH environment variable for it to work.\n" "${dest_path}"
