#!/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 .
# POSIX
dest_path="/usr/local/bin"
# in case we're not root, just default to the home directory
if [ "$(id -u)" -ne 0 ]; then
dest_path="${HOME}/.local/bin"
fi
verbose=0
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <&2 "Cannot write into %s, permission denied.\n" "${dest_path}" && exit 1
# Ensure dest_path exists
if [ ! -d "${dest_path}" ]; then
printf >&2 "Path %s not found.\n" "${dest_path}"
printf >&2 "Do you want to create it now? [Y/n]: "
read -r response
response="${response:-"Y"}"
# Accept only y,Y,Yes,yes,n,N,No,no.
case "${response}" in
y | Y | Yes | yes | YES)
if ! mkdir -p "${dest_path}"; then
printf >&2 "Cannot write into %s, permission denied.\n" "${dest_path}"
exit 1
fi
;;
n | N | No | no | NO)
printf >&2 "next time, create %s first\n" "${dest_path}"
exit 0
;;
*) # Default case: If no more options then break out of the loop.
printf >&2 "Invalid input.\n"
printf >&2 "The available choices are: y,Y,Yes,yes,YES or n,N,No,no,NO.\nExiting.\n"
exit 1
;;
esac
fi
man_dest_path="${dest_path}/../share/man/man1"
# 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
release_ver="github-zip-$(date +%F)"
if [ -d "${curr_dir}/.git" ] && command -v git >/dev/null; then
release_ver="git-$(git rev-parse HEAD)"
fi
for file in distrobox*; do
install -D -m 0755 -t "${dest_path}" "${file}"
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
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)
# 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
install -D -m 0755 -t "${dest_path}" "${file}"
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}"