-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·270 lines (227 loc) · 6.99 KB
/
install.sh
File metadata and controls
executable file
·270 lines (227 loc) · 6.99 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
set -euo pipefail
# mdserve installer script
# Usage: curl -sSfL https://raw.githubusercontent.com/jfernandez/mdserve/main/install.sh | bash
# Repository information
REPO_OWNER="jfernandez"
REPO_NAME="mdserve"
BINARY_NAME="mdserve"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Cleanup function
cleanup() {
if [ -n "${TEMP_FILE:-}" ] && [ -f "$TEMP_FILE" ]; then
rm -f "$TEMP_FILE"
fi
}
# Set trap for cleanup
trap cleanup EXIT
# Logging functions
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
fatal() {
error "$1"
exit 1
}
# Check if command exists
has_command() {
command -v "$1" >/dev/null 2>&1
}
# Download function that tries curl first, then wget
download() {
local url="$1"
local output="$2"
if has_command curl; then
curl -sSfL "$url" -o "$output"
elif has_command wget; then
wget -q "$url" -O "$output"
else
fatal "Neither curl nor wget is available. Please install one of them."
fi
}
# Get latest release tag from GitHub API
get_latest_release() {
local api_url="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest"
local response
if has_command curl; then
response=$(curl -sSf "$api_url")
elif has_command wget; then
response=$(wget -qO- "$api_url")
else
fatal "Neither curl nor wget is available. Please install one of them."
fi
# Extract tag_name from JSON response (simple grep/sed approach to avoid jq dependency)
echo "$response" | grep '"tag_name":' | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/'
}
# Detect platform and architecture
detect_platform() {
local os arch
os=$(uname -s)
arch=$(uname -m)
# Normalize OS
case "$os" in
Linux*) os="linux" ;;
Darwin*) fatal "macOS is not supported by this installer. Please install using Homebrew: brew install mdserve" ;;
CYGWIN*|MINGW*|MSYS*) fatal "Windows is not currently supported" ;;
*) fatal "Unsupported operating system: $os" ;;
esac
# Normalize architecture
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) fatal "Unsupported architecture: $arch" ;;
esac
# Map to binary names used in releases
case "$os-$arch" in
linux-x86_64) echo "x86_64-unknown-linux-musl" ;;
*) fatal "No binary available for $os-$arch" ;;
esac
}
# Find the best installation directory
find_install_dir() {
# Check for user override
if [ -n "${MDSERVE_INSTALL_DIR:-}" ]; then
echo "$MDSERVE_INSTALL_DIR"
return
fi
# Try system-wide directory first (if we can write to it)
if [ -w "/usr/local/bin" ] || [ "$EUID" = 0 ]; then
echo "/usr/local/bin"
return
fi
# Try user directories
for dir in "$HOME/.local/bin" "$HOME/bin"; do
if [ -d "$dir" ] && [ -w "$dir" ]; then
echo "$dir"
return
fi
done
# Create ~/.local/bin if it doesn't exist (XDG standard)
local local_bin="$HOME/.local/bin"
if mkdir -p "$local_bin" 2>/dev/null; then
echo "$local_bin"
return
fi
# Final fallback
local fallback_dir="$HOME/.mdserve/bin"
mkdir -p "$fallback_dir"
echo "$fallback_dir"
}
# Check if directory is in PATH
is_in_path() {
local dir="$1"
case ":$PATH:" in
*":$dir:"*) return 0 ;;
*) return 1 ;;
esac
}
# Main installation function
install_mdserve() {
info "Installing $BINARY_NAME..."
# Detect platform
info "Detecting platform..."
local target
target=$(detect_platform)
info "Detected platform: $target"
# Get latest release
info "Fetching latest release information..."
local version
version=$(get_latest_release)
if [ -z "$version" ]; then
fatal "Failed to get latest release information"
fi
info "Latest release: $version"
# Construct download URL
local binary_name="${BINARY_NAME}-${target}"
local download_url="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${version}/${binary_name}"
# Create temporary file
TEMP_FILE=$(mktemp)
# Download binary
info "Downloading $binary_name..."
if ! download "$download_url" "$TEMP_FILE"; then
fatal "Failed to download binary from $download_url"
fi
# Find installation directory
local install_dir
install_dir=$(find_install_dir)
info "Installing to: $install_dir"
# Check if we need sudo for system directory
local use_sudo=""
if [ "$install_dir" = "/usr/local/bin" ] && [ "$EUID" != 0 ] && [ ! -w "$install_dir" ]; then
if has_command sudo; then
info "Administrator privileges required for system installation"
use_sudo="sudo"
else
fatal "Cannot write to $install_dir and sudo is not available"
fi
fi
# Install binary
local install_path="$install_dir/$BINARY_NAME"
if [ -n "$use_sudo" ]; then
$use_sudo cp "$TEMP_FILE" "$install_path"
$use_sudo chmod +x "$install_path"
else
cp "$TEMP_FILE" "$install_path"
chmod +x "$install_path"
fi
# Verify installation
if [ ! -x "$install_path" ]; then
fatal "Installation failed: $install_path is not executable"
fi
# Test the binary
if ! "$install_path" --version >/dev/null 2>&1; then
warn "Binary installed but --version check failed. This might be normal if the binary doesn't support --version."
fi
success "$BINARY_NAME $version installed successfully to $install_path"
# Check PATH
if ! is_in_path "$install_dir"; then
warn "⚠️ $install_dir is not in your PATH"
info "Add it to your PATH by adding this line to your shell profile:"
echo " export PATH=\"$install_dir:\$PATH\""
echo ""
info "Or run the binary directly: $install_path"
else
info "✅ You can now run: $BINARY_NAME"
fi
}
# Script entry point
main() {
# Check for help flag
for arg in "$@"; do
case "$arg" in
-h|--help)
echo "mdserve installer"
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Environment variables:"
echo " MDSERVE_INSTALL_DIR Override installation directory"
echo ""
echo "Examples:"
echo " # Install to default location"
echo " curl -sSfL https://raw.githubusercontent.com/$REPO_OWNER/$REPO_NAME/main/install.sh | bash"
echo ""
echo " # Install to custom directory"
echo " MDSERVE_INSTALL_DIR=~/my-tools curl -sSfL ... | bash"
exit 0
;;
esac
done
install_mdserve
}
# Run main function with all arguments
main "$@"