Files
distrobox/install
T

130 lines
3.5 KiB
Bash
Executable File

#!/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
dest_path="/usr/local/bin"
verbose=0
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
install -p /usr/local/bin
Options:
--path/-p: path where to deploy the files (default /usr/local)
--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
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
[ ! -w "${dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${dest_path}" && exit 1
mkdir -p "${dest_path}/../share/man/man1"
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 [ -f "${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
cp "${file}" "${dest_path}"
chmod 0755 "${dest_path}/${file}"
done
for file in man/man1/*; do
cp "${file}" "${man_dest_path}"
chmod 0644 "${man_dest_path}/$(basename "${file}")"
done
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
cd /tmp
# 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
cp "${file}" "${dest_path}"
chmod 0755 "${dest_path}/$(basename "${file}")"
done
for file in "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/man/*; do
cp "${file}" "${man_dest_path}"
chmod 0644 "${man_dest_path}/$(basename "${file}")"
done
# securely delete unneeded files
rm -f "${release_name}"
rm -rf "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/*
rm -rf "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/.github
rmdir "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"
fi