-
Notifications
You must be signed in to change notification settings - Fork 4
/
install_monocore.sh
executable file
·318 lines (260 loc) · 8.5 KB
/
install_monocore.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/sh
# install_monocore.sh
# ------------------
# This script downloads and installs monocore binaries and libraries for the user's platform.
#
# Usage:
# ./install_monocore.sh [options]
#
# Options:
# --version Specify version to install (default: 0.2.1)
# --no-cleanup Skip cleanup of temporary files after installation
#
# The script performs the following tasks:
# 1. Detects OS and architecture
# 2. Downloads appropriate release archive from GitHub
# 3. Verifies checksum
# 4. Installs binaries to ~/.local/bin
# 5. Installs libraries to ~/.local/lib
# 6. Creates unversioned library symlinks
#
# Installation Paths:
# Binaries: ~/.local/bin/
# Libraries: ~/.local/lib/
# Color variables
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
RESET="\033[0m"
# Logging functions
info() {
printf "${GREEN}:: %s${RESET}\n" "$1"
}
warn() {
printf "${YELLOW}:: %s${RESET}\n" "$1"
}
error() {
printf "${RED}:: %s${RESET}\n" "$1"
}
# Default values
VERSION="0.2.1"
NO_CLEANUP=false
TEMP_DIR="/tmp/monocore-install"
GITHUB_REPO="appcypher/monocore"
# Installation directories
BIN_DIR="$HOME/.local/bin"
LIB_DIR="$HOME/.local/lib"
# Parse command line arguments
for arg in "$@"; do
case $arg in
--version=*)
VERSION="${arg#*=}"
shift
;;
--no-cleanup)
NO_CLEANUP=true
shift
;;
esac
done
# Function to check command existence
check_command() {
if ! command -v "$1" >/dev/null 2>&1; then
error "Required command '$1' not found. Please install it first."
exit 1
fi
}
# Check required commands
check_command curl
check_command tar
check_command shasum
# Detect OS and architecture
detect_platform() {
OS="unknown"
ARCH="unknown"
case "$(uname -s)" in
Linux*) OS="linux";;
Darwin*) OS="darwin";;
*) error "Unsupported operating system: $(uname -s)"; exit 1;;
esac
case "$(uname -m)" in
x86_64) ARCH="x86_64";;
arm64) ARCH="arm64";;
aarch64) ARCH="aarch64";;
*) error "Unsupported architecture: $(uname -m)"; exit 1;;
esac
# Normalize architecture for Darwin
if [ "$OS" = "darwin" ] && [ "$ARCH" = "aarch64" ]; then
ARCH="arm64"
fi
PLATFORM="${OS}-${ARCH}"
ARCHIVE_NAME="monocore-${VERSION}-${PLATFORM}.tar.gz"
CHECKSUM_FILE="${ARCHIVE_NAME}.sha256"
}
# Cleanup function
cleanup() {
if [ "$NO_CLEANUP" = true ]; then
info "Skipping cleanup as requested"
return
fi
info "Cleaning up temporary files..."
rm -rf "$TEMP_DIR"
}
# Set up trap for cleanup
trap cleanup EXIT
# Create necessary directories
create_directories() {
info "Creating installation directories..."
mkdir -p "$BIN_DIR" "$LIB_DIR" "$TEMP_DIR"
if [ $? -ne 0 ]; then
error "Failed to create directories"
exit 1
fi
}
# Download files from GitHub
download_files() {
info "Downloading monocore ${VERSION} for ${PLATFORM}..."
BASE_URL="https://github.com/${GITHUB_REPO}/releases/download/monocore-v${VERSION}"
cd "$TEMP_DIR" || exit 1
# Download archive with progress bar
curl -L -# -o "${ARCHIVE_NAME}" "${BASE_URL}/${ARCHIVE_NAME}" || { error "Failed to download archive"; exit 1; }
# Download checksum silently
curl -L -s -o "${CHECKSUM_FILE}" "${BASE_URL}/${CHECKSUM_FILE}" || { error "Failed to download checksum"; exit 1; }
}
# Verify checksum
verify_checksum() {
info "Verifying checksum..."
cd "$TEMP_DIR" || exit 1
# Redirect detailed output to /dev/null but keep the exit status
if ! (shasum -a 256 -c "$CHECKSUM_FILE" >/dev/null 2>&1); then
error "Checksum verification failed"
exit 1
fi
}
# Extract and install files
install_files() {
info "Extracting files..."
cd "$TEMP_DIR" || exit 1
tar xzf "$ARCHIVE_NAME" || { error "Failed to extract archive"; exit 1; }
EXTRACT_DIR="monocore-${VERSION}-${PLATFORM}"
cd "$EXTRACT_DIR" || { error "Failed to enter extract directory"; exit 1; }
# Install binaries
info "Installing binaries..."
install -m 755 monocore "$BIN_DIR/" || { error "Failed to install monocore"; exit 1; }
install -m 755 monokrun "$BIN_DIR/" || { error "Failed to install monokrun"; exit 1; }
# Create mc symlink
ln -sf "$BIN_DIR/monocore" "$BIN_DIR/mc" || { error "Failed to create mc symlink"; exit 1; }
# Install libraries
info "Installing libraries..."
if [ "$OS" = "darwin" ]; then
# Install versioned dylibs
for lib in *.dylib; do
install -m 755 "$lib" "$LIB_DIR/" || { error "Failed to install $lib"; exit 1; }
done
# Create unversioned symlinks
cd "$LIB_DIR" || exit 1
ln -sf libkrun.*.dylib libkrun.dylib
ln -sf libkrunfw.*.dylib libkrunfw.dylib
else
# Install versioned shared objects
for lib in *.so.*; do
install -m 755 "$lib" "$LIB_DIR/" || { error "Failed to install $lib"; exit 1; }
done
# Create unversioned symlinks
cd "$LIB_DIR" || exit 1
ln -sf libkrun.so.* libkrun.so
ln -sf libkrunfw.so.* libkrunfw.so
fi
}
# Function to check if a line exists in a file
line_exists() {
grep -Fxq "$1" "$2" 2>/dev/null
}
# Function to add environment config for sh/bash/zsh
setup_posix_shell() {
local shell_rc="$1"
local shell_name="$2"
local lib_path_var="$3"
info "Setting up $shell_name configuration..."
# Create the file if it doesn't exist
touch "$shell_rc"
# PATH configuration
if ! line_exists 'export PATH="$HOME/.local/bin:$PATH"' "$shell_rc"; then
echo >> "$shell_rc"
echo '# Added by monocore installer' >> "$shell_rc"
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$shell_rc"
fi
# Library path configuration
if ! line_exists "export $lib_path_var=\"\$HOME/.local/lib:\$$lib_path_var\"" "$shell_rc"; then
echo "export $lib_path_var=\"\$HOME/.local/lib:\$$lib_path_var\"" >> "$shell_rc"
fi
}
# Function to set up fish shell
setup_fish() {
local fish_config="$HOME/.config/fish/config.fish"
local lib_path_var="$1"
info "Setting up fish configuration..."
# Create config directory if it doesn't exist
mkdir -p "$(dirname "$fish_config")"
touch "$fish_config"
# PATH configuration
if ! line_exists "set -gx PATH $HOME/.local/bin \$PATH" "$fish_config"; then
echo >> "$fish_config"
echo '# Added by monocore installer' >> "$fish_config"
echo "set -gx PATH $HOME/.local/bin \$PATH" >> "$fish_config"
fi
# Library path configuration
if ! line_exists "set -gx $lib_path_var $HOME/.local/lib \$$lib_path_var" "$fish_config"; then
echo "set -gx $lib_path_var $HOME/.local/lib \$$lib_path_var" >> "$fish_config"
fi
}
# Add this function near the other utility functions
check_shell() {
command -v "$1" >/dev/null 2>&1
}
# Function to configure shell environment
configure_shell_env() {
info "Configuring detected shells..."
# Determine library path variable based on OS
local lib_path_var
case "$(uname -s)" in
Linux*) lib_path_var="LD_LIBRARY_PATH";;
Darwin*) lib_path_var="DYLD_LIBRARY_PATH";;
*) warn "Unsupported OS for environment configuration"; return 1;;
esac
# Configure bash if installed
if check_shell bash; then
setup_posix_shell "$HOME/.bashrc" "bash" "$lib_path_var"
fi
# Configure zsh if installed
if check_shell zsh; then
setup_posix_shell "$HOME/.zshrc" "zsh" "$lib_path_var"
fi
# Configure fish if installed
if check_shell fish; then
setup_fish "$lib_path_var"
fi
# Always configure .profile for POSIX shell compatibility
setup_posix_shell "$HOME/.profile" "sh" "$lib_path_var"
info "All detected shell environments configured. Please restart your shell or source your shell's config file"
}
# Main installation process
main() {
info "Starting monocore installation..."
detect_platform
create_directories
download_files
verify_checksum
install_files
# Configure shell environment
configure_shell_env
if [ $? -ne 0 ]; then
warn "Shell environment configuration failed, but installation completed"
fi
info "Installation completed successfully!"
info "Binaries installed to: $BIN_DIR"
info "Libraries installed to: $LIB_DIR"
info "Please restart your shell or source your shell's config file to use monocore"
}
# Run main installation
main