diff --git a/install.sh b/install.sh index ea848d7c..6a724182 100755 --- a/install.sh +++ b/install.sh @@ -1,16 +1,35 @@ #!/usr/bin/env bash -set -e +set -eu -o pipefail -if [[ ! -z $DEBUG ]]; then +if [[ ! -z ${DEBUG-} ]]; then set -x fi -if [[ $EUID != 0 ]]; then +: ${PREFIX:=/usr/local} +BINDIR="$PREFIX/bin" + +if [[ $# -gt 0 ]]; then + BINDIR=$1 +fi + +_can_install() { + if [[ ! -d "$BINDIR" ]]; then + mkdir -p "$BINDIR" 2> /dev/null + fi + [[ -d "$BINDIR" && -w "$BINDIR" ]] +} + +if ! _can_install && [[ $EUID != 0 ]]; then sudo "$0" "$@" exit "$?" fi +if ! _can_install; then + echo "Can't install to $BINDIR" + exit 1 +fi + case "$(uname -m)" in x86_64) machine="amd64" @@ -39,5 +58,5 @@ esac latest="$(curl -sL 'https://api.github.com/repos/zaquestion/lab/releases/latest' | grep 'tag_name' | grep --only 'v[0-9\.]\+' | cut -c 2-)" curl -sL "https://github.com/zaquestion/lab/releases/download/v${latest}/lab_${latest}_${os}_${machine}.tar.gz" | tar -C /tmp/ -xzf - -cp /tmp/lab /usr/local/bin/lab -echo "Successfully installed lab into /usr/local/bin/" +install -m755 /tmp/lab $BINDIR/lab +echo "Successfully installed lab into $BINDIR/"