Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·137 lines (116 loc) · 3.07 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·137 lines (116 loc) · 3.07 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
set -e
REPO="bjarneo/ku"
NAME="ku"
need() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "install: $1 is required" >&2
exit 1
fi
}
download() {
url=$1
dest=$2
if command -v curl >/dev/null 2>&1; then
curl -fSL -o "$dest" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$dest" "$url"
else
echo "install: curl or wget is required" >&2
exit 1
fi
}
choose_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
printf '%s\n' "$INSTALL_DIR"
return
fi
local_bin="$HOME/.local/bin"
case ":$PATH:" in
*":$local_bin:"*)
mkdir -p "$local_bin"
printf '%s\n' "$local_bin"
return
;;
esac
if [ -d /usr/local/bin ]; then
printf '%s\n' /usr/local/bin
return
fi
last_path=$(printf '%s' "$PATH" | tr ':' '\n' | awk 'NF { last=$0 } END { print last }')
if [ -n "$last_path" ]; then
printf '%s\n' "$last_path"
return
fi
echo "install: could not determine an install directory" >&2
exit 1
}
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) echo "install: unsupported architecture: $ARCH" >&2; exit 1 ;;
esac
case "$OS" in
linux|darwin) ;;
mingw*|msys*|cygwin*) OS="windows" ;;
*) echo "install: unsupported OS: $OS" >&2; exit 1 ;;
esac
ASSET="${NAME}-${OS}-${ARCH}"
TARGET_NAME="$NAME"
if [ "$OS" = "windows" ]; then
ASSET="${ASSET}.exe"
TARGET_NAME="${NAME}.exe"
fi
INSTALL_DIR=$(choose_install_dir)
URL="https://github.com/${REPO}/releases/latest/download/${ASSET}"
CHECKSUM_URL="https://github.com/${REPO}/releases/latest/download/checksums.txt"
TMP=$(mktemp)
CHECKSUMS=$(mktemp)
cleanup() {
rm -f "$TMP" "$CHECKSUMS"
}
trap cleanup EXIT INT TERM
echo "Downloading ${ASSET}..."
download "$URL" "$TMP"
echo "Verifying checksum..."
download "$CHECKSUM_URL" "$CHECKSUMS"
EXPECTED=$(awk -v asset="$ASSET" '$2 == asset { print $1 }' "$CHECKSUMS")
if [ -z "$EXPECTED" ]; then
echo "install: checksum for ${ASSET} not found" >&2
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL=$(sha256sum "$TMP" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
ACTUAL=$(shasum -a 256 "$TMP" | awk '{print $1}')
else
echo "install: sha256sum or shasum is required" >&2
exit 1
fi
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "install: checksum mismatch" >&2
echo " expected: $EXPECTED" >&2
echo " got: $ACTUAL" >&2
exit 1
fi
chmod +x "$TMP"
if [ ! -d "$INSTALL_DIR" ]; then
if ! mkdir -p "$INSTALL_DIR" 2>/dev/null; then
need sudo
sudo mkdir -p "$INSTALL_DIR"
fi
fi
DEST="${INSTALL_DIR}/${TARGET_NAME}"
if [ -w "$INSTALL_DIR" ]; then
install -m 0755 "$TMP" "$DEST"
else
need sudo
sudo install -m 0755 "$TMP" "$DEST"
fi
echo "Installed ${NAME} to ${DEST}"
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*) echo "Add ${INSTALL_DIR} to PATH to run ${NAME} from anywhere." ;;
esac