-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·202 lines (166 loc) · 4.84 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·202 lines (166 loc) · 4.84 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
#!/bin/sh
set -eu
# Colors (with fallbacks for non-color terminals)
BOLD="$(tput bold 2>/dev/null || printf '')"
GREY="$(tput setaf 8 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
RESET="$(tput sgr0 2>/dev/null || printf '')"
info() {
printf '%s\n' "${BOLD}${GREY}>${RESET} $*"
}
success() {
printf '%s\n' "${GREEN}✓${RESET} $*"
}
warn() {
printf '%s\n' "${YELLOW}!${RESET} $*" >&2
}
error() {
printf '%s\n' "${RED}✗${RESET} $*" >&2
exit 1
}
# Check for required commands
has() {
command -v "$1" >/dev/null 2>&1
}
has curl || has wget || error "curl or wget is required"
# Detect OS
detect_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
linux*) echo "linux" ;;
darwin*) echo "darwin" ;;
*) error "Unsupported OS: $os" ;;
esac
}
# Detect architecture
detect_arch() {
arch=$(uname -m)
case "$arch" in
x86_64) echo "amd64" ;;
aarch64) echo "arm64" ;;
arm64) echo "arm64" ;;
*) error "Unsupported architecture: $arch" ;;
esac
}
# Download file
download() {
url="$1"
output="$2"
if has curl; then
curl -fsSL "$url" -o "$output"
else
wget -qO "$output" "$url"
fi
}
# Detect shell profile file
detect_profile() {
case "${SHELL:-}" in
*/zsh) echo "${ZDOTDIR:-$HOME}/.zshrc" ;;
*/bash)
if [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.bashrc"
fi
;;
*/fish) echo "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" ;;
*) echo "$HOME/.profile" ;;
esac
}
# Add directory to PATH in shell profile
add_to_path() {
dir="$1"
profile=$(detect_profile)
# Check if already in PATH
case ":$PATH:" in
*":$dir:"*) return 0 ;;
esac
# Check if profile already has this directory
if [ -f "$profile" ] && grep -q "$dir" "$profile" 2>/dev/null; then
return 0
fi
info "Adding $dir to PATH in $profile"
case "${SHELL:-}" in
*/fish)
mkdir -p "$(dirname "$profile")"
printf '\nfish_add_path %s\n' "$dir" >> "$profile"
;;
*)
printf '\nexport PATH="%s:$PATH"\n' "$dir" >> "$profile"
;;
esac
success "Updated $profile"
NEED_RELOAD=1
}
# Main installation
main() {
NEED_RELOAD=0
printf '%s\n' "${BOLD}fqpack installer${RESET}"
printf '%s\n' ""
OS=$(detect_os)
ARCH=$(detect_arch)
BINARY="fqpack-${OS}-${ARCH}"
info "Detected ${OS}/${ARCH}"
# Get latest version
RELEASES="https://github.com/vertti/fastqpacker/releases"
info "Fetching latest version..."
VERSION=$(download "${RELEASES}/latest" /dev/stdout 2>/dev/null | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | head -1)
if [ -z "$VERSION" ]; then
# Fallback: get from redirect
VERSION=$(curl -fsSL -o /dev/null -w '%{url_effective}' "${RELEASES}/latest" | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+')
fi
[ -z "$VERSION" ] && error "Could not determine latest version"
info "Latest version: ${VERSION}"
URL="${RELEASES}/download/${VERSION}/${BINARY}"
# Determine install directory
if [ -w /usr/local/bin ]; then
INSTALL_DIR="/usr/local/bin"
elif [ -n "${SUDO_USER:-}" ] || [ "$(id -u)" = "0" ]; then
INSTALL_DIR="/usr/local/bin"
else
INSTALL_DIR="$HOME/.local/bin"
mkdir -p "$INSTALL_DIR"
fi
info "Installing to ${INSTALL_DIR}"
# Download
TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT
info "Downloading ${BINARY}..."
download "$URL" "$TMP" || error "Download failed"
chmod +x "$TMP"
# Install (with sudo if needed)
if [ -w "$INSTALL_DIR" ]; then
mv "$TMP" "$INSTALL_DIR/fqpack"
else
info "Requesting sudo access..."
sudo mv "$TMP" "$INSTALL_DIR/fqpack"
fi
success "Installed fqpack to ${INSTALL_DIR}/fqpack"
# Add to PATH if needed
if [ "$INSTALL_DIR" = "$HOME/.local/bin" ]; then
add_to_path "$INSTALL_DIR"
fi
# Verify
printf '%s\n' ""
if has fqpack; then
success "fqpack ${VERSION} is ready!"
fqpack -version
elif [ -x "$INSTALL_DIR/fqpack" ]; then
success "fqpack ${VERSION} installed!"
"$INSTALL_DIR/fqpack" -version
if [ "$NEED_RELOAD" = "1" ]; then
printf '%s\n' ""
info "Run this to use fqpack now:"
printf '%s\n' " ${BOLD}export PATH=\"$INSTALL_DIR:\$PATH\"${RESET}"
printf '%s\n' ""
info "Or restart your shell."
fi
else
error "Installation failed"
fi
}
main