-
Notifications
You must be signed in to change notification settings - Fork 100
/
install.sh
91 lines (76 loc) · 2.34 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
set -e
if [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "x86_64" ]; then
target="x86_64-apple-darwin"
elif [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "arm64" ]; then
target="aarch64-apple-darwin"
elif [ "$(uname -s)" = "Linux" ] && [ "$(uname -m)" = "x86_64" ]; then
target="x86_64-unknown-linux-musl"
elif [ "$(uname -s)" = "Linux" ] && [ "$(uname -m)" = "aarch64" ]; then
target="aarch64-unknown-linux-musl"
elif [ "$(uname -s)" = "Linux" ] && ( uname -m | grep -q -e '^arm' ); then
target="arm-unknown-linux-gnueabihf"
else
echo "Unsupported OS or architecture"
exit 1
fi
fetch()
{
if which curl > /dev/null; then
if [ "$#" -eq 2 ]; then curl -fL -o "$1" "$2"; else curl -fsSL "$1"; fi
elif which wget > /dev/null; then
if [ "$#" -eq 2 ]; then wget -O "$1" "$2"; else wget -nv -O - "$1"; fi
else
echo "Can't find curl or wget, can't download package"
exit 1
fi
}
echo "Detected target: $target"
releases=$(fetch https://api.github.com/repos/ducaale/xh/releases/latest)
url=$(echo "$releases" | grep -wo -m1 "https://.*$target.tar.gz" || true)
if ! test "$url"; then
echo "Could not find release info"
exit 1
fi
echo "Downloading xh..."
temp_dir=$(mktemp -dt xh.XXXXXX)
trap 'rm -rf "$temp_dir"' EXIT INT TERM
cd "$temp_dir"
if ! fetch xh.tar.gz "$url"; then
echo "Could not download tarball"
exit 1
fi
user_bin="$HOME/.local/bin"
case $PATH in
*:"$user_bin":* | "$user_bin":* | *:"$user_bin")
default_bin=$user_bin
;;
*)
default_bin='/usr/local/bin'
;;
esac
_read_installdir() {
printf "Install location [default: %s]: " "$default_bin"
read -r xh_installdir < /dev/tty
xh_installdir=${xh_installdir:-$default_bin}
}
if [ -z "$XH_BINDIR" ]; then
_read_installdir
while ! test -d "$xh_installdir"; do
echo "Directory $xh_installdir does not exist"
_read_installdir
done
else
xh_installdir=${XH_BINDIR}
fi
tar xzf xh.tar.gz
if test -w "$xh_installdir" || [ -n "$XH_BINDIR" ]; then
mv xh-*/xh "$xh_installdir/"
ln -sf "$xh_installdir/xh" "$xh_installdir/xhs"
else
sudo mv xh-*/xh "$xh_installdir/"
sudo ln -sf "$xh_installdir/xh" "$xh_installdir/xhs"
fi
echo "$("$xh_installdir"/xh -V) has been installed to:"
echo " • $xh_installdir/xh"
echo " • $xh_installdir/xhs"