improve portability of install.sh (#107)

* Replace echo with printf

The echo(1) command varies among sh implementations.  Some interpret
hyphen command-line options.

It would be unusual, but in theory if $PREFIX began with a hyphen or
two then this usage of echo might cause an error or unknown behaviour
in such implementations.

The printf(1) command is consistent across shell implementations.

* Leave privilege elevation to the user

* Add die function

* Break long line
This commit is contained in:
Gregory Chamberlain 2021-04-18 10:00:06 +01:00 committed by GitHub
parent c405280c92
commit 82ce67f572
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 17 deletions

View file

@ -50,8 +50,16 @@ more flexibility.
## Installation
As root, using sudo:
```sh
curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | sh
curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | sudo sh
```
As a regular user:
```sh
curl -sSL https://raw.githubusercontent.com/soywod/himalaya/master/install.sh | PREFIX=~/.local sh
```
Read the Nix section below for a Nix-based installation method. *See the [wiki section](https://github.com/soywod/himalaya/wiki/Installation)

View file

@ -2,6 +2,11 @@
set -eu
die() {
printf '%s\n' "$1" >&2
exit "${2-1}"
}
DESTDIR="${DESTDIR:-/}"
PREFIX="${PREFIX:-"$DESTDIR/usr/local"}"
RELEASES_URL="https://github.com/soywod/himalaya/releases"
@ -12,28 +17,20 @@ case $system in
msys*|mingw*|cygwin*|win*) system=windows;;
linux|freebsd) system=linux;;
darwin) system=macos;;
*) echo "Error: Unsupported system: $system"; exit 1;;
*) die "Unsupported system: $system" ;;
esac
if ! tmpdir=$(mktemp -d); then
echo "Error: Failed to create tmpdir"
exit 1
else
trap "rm -rf $tmpdir" EXIT
fi
tmpdir=$(mktemp -d) || die "Failed to create tmpdir"
trap "rm -rf $tmpdir" EXIT
echo "Downloading latest $system release…"
curl -sLo "$tmpdir/himalaya.tar.gz" "$RELEASES_URL/latest/download/himalaya-$system.tar.gz"
curl -sLo "$tmpdir/himalaya.tar.gz" \
"$RELEASES_URL/latest/download/himalaya-$system.tar.gz"
echo "Installing binary…"
tar -xzf "$tmpdir/himalaya.tar.gz" -C "$tmpdir"
if [ -w "$PREFIX" ]; then
mkdir -p "$PREFIX/bin"
cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya"
else
sudo mkdir -p "$PREFIX/bin"
sudo cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya"
fi
mkdir -p "$PREFIX/bin"
cp -f -- "$tmpdir/himalaya.exe" "$PREFIX/bin/himalaya"
echo "$("$PREFIX/bin/himalaya" --version) installed!"
die "$("$PREFIX/bin/himalaya" --version) installed!" 0