2020-05-10 15:58:05 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# This script does not have a stable API.
|
|
|
|
|
|
|
|
_gitstatus_install_main() {
|
|
|
|
if [ -n "${ZSH_VERSION:-}" ]; then
|
|
|
|
emulate -L sh -o no_unset
|
|
|
|
else
|
|
|
|
set -u
|
|
|
|
fi
|
|
|
|
|
|
|
|
local argv1=$1
|
|
|
|
shift
|
|
|
|
|
2020-05-12 21:55:25 +02:00
|
|
|
local no_check= no_install= uname_s= uname_m= gitstatus_dir= dl_status=
|
2020-05-10 15:58:05 +02:00
|
|
|
local opt= OPTARG= OPTIND=1
|
|
|
|
|
2020-05-12 21:55:25 +02:00
|
|
|
while getopts ':s:m:d:p:fnh' opt "$@"; do
|
2020-05-10 15:58:05 +02:00
|
|
|
case "$opt" in
|
|
|
|
h)
|
|
|
|
command cat <<\END
|
2020-05-12 21:55:25 +02:00
|
|
|
Usage: install [-s KERNEL] [-m ARCH] [-d DIR] [-p CMD] [-f|-n] [-- CMD [ARG]...]
|
2020-05-10 15:58:05 +02:00
|
|
|
|
|
|
|
If positional arguments are specified, call this on success:
|
|
|
|
|
|
|
|
CMD [ARG]... DAEMON VERSION INSTALLED
|
|
|
|
|
|
|
|
DAEMON is path to gitstatusd. VERSION is a glob pattern for the
|
|
|
|
version this daemon should support; it's supposed to be passed as
|
|
|
|
-G to gitstatusd. INSTALLED is 1 if gitstatusd has just been
|
|
|
|
downloaded and 0 otherwise.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-s KERNEL use this instead of lowercase `uname -s`
|
|
|
|
-m ARCH use this instead of lowercase `uname -m`
|
|
|
|
-d DIR use this instead of `dirname "$0"`
|
2020-05-12 21:55:25 +02:00
|
|
|
-p CMD eval this every second while downloading gitstatusd
|
2020-05-10 15:58:05 +02:00
|
|
|
-f download gitstatusd even if there is one locally
|
|
|
|
-n do not download gitstatusd (fail instead)
|
|
|
|
END
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
n)
|
|
|
|
if [ -n "$no_install" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
no_install=1
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
if [ -n "$no_check" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
no_check=1
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
if [ -n "$gitstatus_dir" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$OPTARG" ]; then
|
|
|
|
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
gitstatus_dir="$OPTARG"
|
|
|
|
;;
|
2020-05-12 21:55:25 +02:00
|
|
|
p)
|
|
|
|
if [ -n "$dl_status" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$OPTARG" ]; then
|
|
|
|
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
dl_status="$OPTARG"
|
|
|
|
;;
|
2020-05-10 15:58:05 +02:00
|
|
|
m)
|
|
|
|
if [ -n "$uname_m" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$OPTARG" ]; then
|
|
|
|
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
uname_m="$OPTARG"
|
|
|
|
;;
|
|
|
|
s)
|
|
|
|
if [ -n "$uname_s" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: duplicate option: -$opt"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$OPTARG" ]; then
|
|
|
|
>&2 echo "[error] incorrect value of -$opt: $OPTARG"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
uname_s="$OPTARG"
|
|
|
|
;;
|
|
|
|
\?) >&2 echo "[gitstatus] error: invalid option: -$OPTARG" ; return 1;;
|
|
|
|
:) >&2 echo "[gitstatus] error: missing required argument: -$OPTARG"; return 1;;
|
|
|
|
*) >&2 echo "[gitstatus] internal error: unhandled option: -$opt" ; return 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift "$((OPTIND - 1))"
|
|
|
|
|
|
|
|
: "${gitstatus_dir:=$argv1}"
|
|
|
|
|
|
|
|
if [ -n "$no_check" -a -n "$no_install" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: incompatible options: -f, -n"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$uname_s" ]; then
|
|
|
|
uname_s="$(command uname -s)" || return
|
|
|
|
uname_s="$(printf '%s' "$uname_s" | command tr '[A-Z]' '[a-z]')" || return
|
|
|
|
fi
|
|
|
|
if [ -z "$uname_m" ]; then
|
2020-05-10 16:17:00 +02:00
|
|
|
uname_m="$(command uname -m)" || return
|
2020-05-10 15:58:05 +02:00
|
|
|
uname_m="$(printf '%s' "$uname_m" | command tr '[A-Z]' '[a-z]')" || return
|
|
|
|
fi
|
|
|
|
|
|
|
|
local daemon="${GITSTATUS_DAEMON:-}"
|
|
|
|
local cache_dir="${GITSTATUS_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/gitstatus}"
|
|
|
|
|
|
|
|
if [ -z "$no_check" ]; then
|
|
|
|
if [ -n "${daemon##/*}" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: GITSTATUS_DAEMON is not absolute path: $daemon"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$daemon" ]; then
|
|
|
|
daemon="$gitstatus_dir"/usrbin/gitstatusd
|
|
|
|
if [ ! -e "$daemon" ]; then
|
|
|
|
daemon="$daemon"-"$uname_s"-"$uname_m"
|
|
|
|
if [ ! -e "$daemon" ]; then
|
|
|
|
daemon=
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -n "$daemon" ]; then
|
|
|
|
local gitstatus_version= libgit2_version=
|
|
|
|
if ! . "$gitstatus_dir"/build.info; then
|
|
|
|
>&2 echo "[gitstatus] internal error: failed to source build.info"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$gitstatus_version" ]; then
|
|
|
|
>&2 echo "[gitstatus] internal error: empty gitstatus_version in build.info"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
[ $# = 0 ] || "$@" "$daemon" "$gitstatus_version" 0
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
while IFS= read -r line; do
|
|
|
|
line="${line###*}"
|
|
|
|
[ -n "$line" ] || continue
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
local uname_s_glob= uname_m_glob= file= version= sha256=
|
2020-05-10 15:58:05 +02:00
|
|
|
eval "$line" || return
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
if [ -z "$uname_s_glob" -o \
|
|
|
|
-z "$uname_m_glob" -o \
|
|
|
|
-z "$file" -o \
|
|
|
|
-z "$version" -o \
|
|
|
|
-z "$sha256" ]; then
|
2020-05-10 15:58:05 +02:00
|
|
|
>&2 echo "[gitstatus] internal error: invalid install.info line: $line"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$uname_s" in
|
|
|
|
$uname_s_glob) ;;
|
|
|
|
*) continue;;
|
|
|
|
esac
|
|
|
|
case "$uname_m" in
|
|
|
|
$uname_m_glob) ;;
|
|
|
|
*) continue;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Found a match. The while loop will terminate during this iteration.
|
|
|
|
|
|
|
|
if [ -z "$no_check" ]; then
|
|
|
|
# Check if a suitable gitstatusd already exists.
|
|
|
|
local daemon="$cache_dir"/"$file"
|
|
|
|
if [ -e "$daemon" ]; then
|
|
|
|
[ $# = 0 ] || "$@" "$daemon" "$version" 0
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
daemon="$daemon"-"$uname_s"-"$uname_m"
|
|
|
|
if [ -e "$daemon" ]; then
|
|
|
|
local gitstatus_version= libgit2_version=
|
|
|
|
if ! . "$gitstatus_dir"/build.info; then
|
|
|
|
>&2 echo "[gitstatus] internal error: failed to source build.info"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$gitstatus_version" ]; then
|
|
|
|
>&2 echo "[gitstatus] internal error: empty gitstatus_version in build.info"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
[ $# = 0 ] || "$@" "$daemon" "$gitstatus_version" 0
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# No suitable gitstatusd exists. Need to download.
|
|
|
|
|
|
|
|
if [ -n "$no_install" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: no gitstatusd found and installation is disabled"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local daemon="$cache_dir"/"$file"
|
|
|
|
|
|
|
|
if [ -n "${cache_dir##/*}" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: GITSTATUS_CACHE_DIR is not absolute: $cache_dir"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
[ -d "$cache_dir" ] || mkdir -p -- "$cache_dir" || return
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
local tmpdir
|
|
|
|
if ! command -v mktemp >/dev/null 2>&1 ||
|
|
|
|
! tmpdir="$(command mktemp -d "${TMPDIR:-/tmp}"/gitstatus-install.XXXXXXXXXX)"; then
|
|
|
|
tmpdir="${TMPDIR:-/tmp}/gitstatus-install.tmp.$$"
|
|
|
|
mkdir -p -- "$tmpdir" || return
|
|
|
|
fi
|
|
|
|
|
2020-05-10 15:58:05 +02:00
|
|
|
(
|
2020-05-13 09:40:53 +02:00
|
|
|
if [ -n "${ZSH_VERSION:-}" ]; then
|
|
|
|
builtin cd -q -- "$tmpdir" || exit
|
|
|
|
else
|
|
|
|
cd -- "$tmpdir" || exit
|
|
|
|
fi
|
|
|
|
|
2020-05-12 21:55:25 +02:00
|
|
|
local fetch
|
|
|
|
if command -v curl >/dev/null 2>&1; then
|
2020-05-26 20:51:58 +02:00
|
|
|
fetch="command curl -fsSL --"
|
2020-05-12 21:55:25 +02:00
|
|
|
elif command -v wget >/dev/null 2>&1; then
|
2020-05-26 20:51:58 +02:00
|
|
|
fetch="command wget -O- --"
|
2020-05-12 21:55:25 +02:00
|
|
|
else
|
|
|
|
>&2 echo "[gitstatus] error: please install curl or wget"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local url1="https://github.com/romkatv/gitstatus/releases/download/$version/$file.tar.gz"
|
|
|
|
local url2="https://gitee.com/romkatv/gitstatus/raw/release-$version/release/$file.tar.gz"
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
check_sha256() {
|
|
|
|
local file="$1".tar.gz
|
|
|
|
local hash=
|
|
|
|
if command -v shasum >/dev/null 2>/dev/null; then
|
|
|
|
hash="$(command shasum -b -a 256 -- "$file")" || hash=
|
|
|
|
hash="${hash%% *}"
|
|
|
|
elif command -v sha256sum >/dev/null 2>/dev/null; then
|
|
|
|
hash="$(command sha256sum -b -- "$file")" || hash=
|
|
|
|
hash="${hash%% *}"
|
|
|
|
elif command -v sha256 >/dev/null 2>/dev/null; then
|
|
|
|
hash="$(command sha256 -- "$file" </dev/null)" || hash=
|
|
|
|
# Ignore sha256 output if it's from hashalot. It's incompatible.
|
|
|
|
if [ ${#hash} -lt 64 ]; then
|
|
|
|
hash=
|
|
|
|
else
|
|
|
|
hash="${hash##* }"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
[ "$1" = 1 -a -z "$hash" -o "$hash" = "$sha256" ]
|
2020-05-12 21:55:25 +02:00
|
|
|
}
|
2020-05-11 19:27:10 +02:00
|
|
|
|
2020-05-12 21:55:25 +02:00
|
|
|
local sig='INT QUIT TERM ILL PIPE'
|
2020-05-11 19:27:10 +02:00
|
|
|
|
2020-05-12 21:55:25 +02:00
|
|
|
fetch() {
|
|
|
|
local trapped=
|
|
|
|
trap 'trapped=1' $sig
|
|
|
|
if [ "$1" != 1 ] && command -v sleep >/dev/null 2>/dev/null; then
|
|
|
|
sleep "$1"
|
|
|
|
fi
|
2020-05-26 20:51:58 +02:00
|
|
|
local part=0 url ret pid die
|
|
|
|
while true; do
|
|
|
|
if [ "$part" = 3 ]; then
|
|
|
|
ret=1
|
|
|
|
break
|
|
|
|
elif [ "$part" = 0 ]; then
|
|
|
|
url="$2"
|
|
|
|
else
|
|
|
|
url="$2"."$part"
|
|
|
|
fi
|
|
|
|
$fetch "$url" >>"$1".tar.gz 2>/dev/null &
|
|
|
|
pid=$!
|
|
|
|
die="trap - $sig; kill -- $pid 2>/dev/null; exit 1"
|
|
|
|
trap "$die" $sig
|
|
|
|
[ -z "$trapped" ] || eval "$die"
|
|
|
|
wait -- "$pid" 2>/dev/null
|
|
|
|
ret="$?"
|
|
|
|
trap - $sig
|
|
|
|
[ "$ret" = 0 ] || break
|
|
|
|
check_sha256 "$1" && break
|
|
|
|
part=$((part+1))
|
|
|
|
done
|
2020-05-13 09:40:53 +02:00
|
|
|
echo -n >"$1".status
|
2020-05-12 21:55:25 +02:00
|
|
|
return "$ret"
|
|
|
|
}
|
|
|
|
|
|
|
|
local trapped=
|
|
|
|
trap 'trapped=1' $sig
|
|
|
|
fetch 1 "$url1" &
|
|
|
|
local pid1=$!
|
|
|
|
fetch 2 "$url2" &
|
|
|
|
local pid2=$!
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
local die="trap - $sig; kill -- $pid1 $pid2 2>/dev/null; exit 1"
|
2020-05-12 21:55:25 +02:00
|
|
|
trap "$die" $sig
|
|
|
|
[ -z "$trapped" ] || eval "$die"
|
|
|
|
|
|
|
|
local n=
|
|
|
|
while true; do
|
|
|
|
[ -z "$dl_status" ] || eval "$dl_status" || eval "$die"
|
|
|
|
if command -v sleep >/dev/null 2>/dev/null; then
|
|
|
|
command sleep 1
|
|
|
|
elif command -v true >/dev/null 2>/dev/null; then
|
|
|
|
command true
|
|
|
|
fi
|
2020-05-13 09:40:53 +02:00
|
|
|
if [ -n "$pid1" -a -e 1.status ]; then
|
2020-05-12 21:55:25 +02:00
|
|
|
wait -- "$pid1" 2>/dev/null
|
|
|
|
local ret="$?"
|
|
|
|
pid1=
|
|
|
|
if [ "$ret" = 0 ]; then
|
|
|
|
[ -z "$pid2" ] || kill -- "$pid2" 2>/dev/null
|
|
|
|
n=1
|
|
|
|
break
|
|
|
|
elif [ -z "$pid2" ]; then
|
|
|
|
break
|
|
|
|
fi
|
2020-05-13 09:40:53 +02:00
|
|
|
elif [ -n "$pid2" -a -e 2.status ]; then
|
2020-05-12 21:55:25 +02:00
|
|
|
wait -- "$pid2" 2>/dev/null
|
|
|
|
local ret="$?"
|
|
|
|
pid2=
|
|
|
|
if [ "$ret" = 0 ]; then
|
|
|
|
[ -z "$pid1" ] || kill -- "$pid1" 2>/dev/null
|
|
|
|
n=2
|
|
|
|
break
|
|
|
|
elif [ -z "$pid1" ]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
trap - $sig
|
|
|
|
|
|
|
|
if [ -z "$n" ]; then
|
|
|
|
>&2 echo "[gitstatus] error: failed to download gitstatusd from any mirror"
|
|
|
|
>&2 echo ""
|
|
|
|
>&2 echo " 1. $url1"
|
|
|
|
>&2 echo " 2. $url2"
|
2020-05-11 19:27:10 +02:00
|
|
|
exit 1
|
2020-05-10 15:58:05 +02:00
|
|
|
fi
|
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
command tar -xzf "$n".tar.gz || exit
|
2020-05-10 15:58:05 +02:00
|
|
|
|
2020-05-13 09:40:53 +02:00
|
|
|
local tmpfile
|
|
|
|
if ! command -v mktemp >/dev/null 2>&1 ||
|
|
|
|
! tmpfile="$(command mktemp "$cache_dir"/gitstatusd.XXXXXXXXXX)"; then
|
|
|
|
tmpfile="$cache_dir"/gitstatusd.tmp.$$
|
2020-05-10 15:58:05 +02:00
|
|
|
fi
|
2020-05-13 09:40:53 +02:00
|
|
|
|
|
|
|
command mv -f -- gitstatusd-* "$tmpfile" || exit
|
|
|
|
command mv -f -- "$tmpfile" "$cache_dir"/"$file" && exit
|
|
|
|
command rm -f -- "$cache_dir"/"$file"
|
|
|
|
command mv -f -- "$tmpfile" "$cache_dir"/"$file" && exit
|
|
|
|
command rm -f -- "$tmpfile"
|
|
|
|
exit 1
|
|
|
|
)
|
|
|
|
|
|
|
|
local ret=$?
|
|
|
|
command rm -rf -- "$tmpdir"
|
|
|
|
[ "$ret" = 0 ] || return
|
2020-05-10 15:58:05 +02:00
|
|
|
|
|
|
|
[ $# = 0 ] || "$@" "$daemon" "$version" 1
|
|
|
|
return
|
|
|
|
done <"$gitstatus_dir"/install.info
|
|
|
|
|
|
|
|
>&2 echo "[gitstatus] error: no gitstatusd found for $uname_s $uname_m"
|
2020-05-26 11:14:53 +02:00
|
|
|
>&2 echo ""
|
|
|
|
>&2 echo "See: https://github.com/romkatv/gitstatus/blob/master/README.md#compiling"
|
2020-05-10 15:58:05 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -z "${0##*/*}" ]; then
|
|
|
|
_gitstatus_install_main "${0%/*}" "$@"
|
|
|
|
else
|
|
|
|
_gitstatus_install_main . "$@"
|
|
|
|
fi
|