-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·253 lines (202 loc) · 6.29 KB
/
install.sh
File metadata and controls
executable file
·253 lines (202 loc) · 6.29 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env bash
set -euo pipefail
# Enable debug mode if DEBUG environment variable is set
if [ "${DEBUG:-}" = "1" ]; then
set -x
fi
# Default values
CHANNEL="${MAA_CHANNEL:-stable}"
INSTALL_DIR="${MAA_INSTALL_DIR:-$HOME/.local/bin}"
REPO="MaaAssistantArknights/maa-cli"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
info() {
echo -e "${GREEN}$1${NC}" >&2
}
warn() {
echo -e "${YELLOW}$1${NC}" >&2
}
# Detect platform and architecture
detect_platform() {
local os arch
case "$(uname -s)" in
Linux*) os="linux" ;;
Darwin*) os="apple-darwin" ;;
*) error "Unsupported operating system: $(uname -s)" ;;
esac
case "$(uname -m)" in
x86_64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) error "Unsupported architecture: $(uname -m)" ;;
esac
# Determine full target triple
if [ "$os" = "linux" ]; then
echo "${arch}-unknown-${os}-gnu"
else
echo "${arch}-${os}"
fi
}
# Fetch version info from GitHub (shell-friendly .txt format)
fetch_version_info() {
local channel=$1
local url="https://raw.githubusercontent.com/${REPO}/version/${channel}.txt"
info "Fetching version information from ${channel}"
if command -v curl > /dev/null 2>&1; then
curl -fsSL "$url"
elif command -v wget > /dev/null 2>&1; then
wget -qO- "$url"
else
error "Neither curl nor wget found. Please install one of them."
fi
}
# Download and verify file
download_and_verify() {
local url=$1
local output=$2
local expected_hash=$3
info "Downloading from $url..."
if command -v curl > /dev/null 2>&1; then
curl -fL -o "$output" "$url"
elif command -v wget > /dev/null 2>&1; then
wget -O "$output" "$url"
else
error "Neither curl nor wget found."
fi
info "Verifying checksum..."
local actual_hash
if command -v sha256sum > /dev/null 2>&1; then
actual_hash=$(sha256sum "$output" | awk '{print $1}')
elif command -v shasum > /dev/null 2>&1; then
actual_hash=$(shasum -a 256 "$output" | awk '{print $1}')
else
warn "No SHA256 tool found, skipping verification"
return 0
fi
if [ "$actual_hash" != "$expected_hash" ]; then
error "Checksum verification failed!\nExpected: $expected_hash\nActual: $actual_hash"
fi
info "Checksum verified successfully"
}
# Extract archive
extract_archive() {
local archive=$1
local dest_dir=$2
info "Extracting archive..."
case "$archive" in
*.tar.gz)
tar -xzf "$archive" -C "$dest_dir"
;;
*.zip)
unzip -q "$archive" -d "$dest_dir"
;;
*)
error "Unsupported archive format: $archive"
;;
esac
}
# Main installation function
main() {
local target version archive_name download_url sha256sum
# Detect platform
target=$(detect_platform)
info "Detected platform: $target"
# Fetch version information
local version_info
version_info=$(fetch_version_info "$CHANNEL") || error "Failed to fetch version information"
# Parse the version info (KEY=VALUE format)
# Extract VERSION
version=$(echo "$version_info" | grep "^VERSION=" | cut -d'=' -f2)
# Get asset info for the detected platform
local target_var="${target//-/_}"
target_var=$(echo "$target_var" | tr '[:lower:]' '[:upper:]') # Convert to uppercase
# Extract variables for this target
archive_name=$(echo "$version_info" | grep "^${target_var}_NAME=" | cut -d'=' -f2)
sha256sum=$(echo "$version_info" | grep "^${target_var}_SHA256=" | cut -d'=' -f2)
if [ -z "$archive_name" ]; then
error "No release found for platform: $target"
fi
info "Version: $version"
info "Archive: $archive_name"
# Construct download URL
download_url="https://github.com/${REPO}/releases/download/v${version}/${archive_name}"
# Create temporary directory
tmp_dir=$(mktemp -d)
# shellcheck disable=SC2064
# We want use the current and not the future values,
trap "rm -rf '$tmp_dir'" EXIT
# Download and verify
download_and_verify "$download_url" "${tmp_dir}/${archive_name}" "$sha256sum"
# Extract archive
extract_archive "${tmp_dir}/${archive_name}" "$tmp_dir"
# Find the binary (it might be in a subdirectory)
local binary_path
binary_path=$(find "$tmp_dir" -name "maa" -type f | head -n 1)
if [ -z "$binary_path" ]; then
error "Binary not found in extracted archive"
fi
# Install binary
mkdir -p "$INSTALL_DIR"
install -m 755 "$binary_path" "${INSTALL_DIR}/maa"
info "Successfully installed maa to ${INSTALL_DIR}/maa"
# Check if in PATH
if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then
warn "Warning: ${INSTALL_DIR} is not in your PATH"
warn "Add it by running: export PATH=\"${INSTALL_DIR}:\$PATH\""
fi
}
# Show usage
usage() {
cat << EOF
Usage: $0 [OPTIONS]
Install maa-cli from GitHub releases.
OPTIONS:
-h, --help Show this help message
-c, --channel CHANNEL Specify release channel (stable, beta, alpha) [default: stable]
-d, --dir DIRECTORY Installation directory [default: \$HOME/.local/bin]
ENVIRONMENT VARIABLES:
MAA_CHANNEL Release channel (same as --channel)
MAA_INSTALL_DIR Installation directory (same as --dir)
EXAMPLES:
# Install stable release to default location
$0
# Install beta release
$0 --channel beta
# Install to custom directory
$0 --dir /usr/local/bin
# Using environment variables
MAA_CHANNEL=alpha MAA_INSTALL_DIR=/opt/bin $0
EOF
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
-c|--channel)
CHANNEL="$2"
shift 2
;;
-d|--dir)
INSTALL_DIR="$2"
shift 2
;;
*)
error "Unknown option: $1\nUse --help for usage information"
;;
esac
done
# Validate channel
case "$CHANNEL" in
stable|beta|alpha) ;;
*) error "Invalid channel: $CHANNEL (must be stable, beta, or alpha)" ;;
esac
main