-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·319 lines (261 loc) · 12.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·319 lines (261 loc) · 12.3 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
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
319
#!/usr/bin/env bash
# install.sh - Install dotbak
set -euo pipefail
GITHUB_REPO="${GITHUB_REPO:-darshithedpara/dotbak}"
# Detect piped/remote install (curl | bash) vs local install
REMOTE_INSTALL=false
if [[ -z "${BASH_SOURCE[0]:-}" ]] || [[ "${BASH_SOURCE[0]}" == "bash" ]]; then
SCRIPT_DIR=""
REMOTE_INSTALL=true
else
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ ! -f "$SCRIPT_DIR/build.sh" ]] && [[ ! -f "$SCRIPT_DIR/dotbak" ]]; then
REMOTE_INSTALL=true
fi
fi
# ── Argument parsing ──────────────────────────────────────────────────────────
INSTALL_YES=false
for _arg in "$@"; do
case "$_arg" in
--yes|-y) INSTALL_YES=true ;;
esac
done
case "${DOTBAK_YES:-}" in
1|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) INSTALL_YES=true ;;
esac
unset _arg
# Auto-accept when piped (curl | bash) — the user already consented by piping
if [[ ! -t 0 ]]; then
INSTALL_YES=true
fi
_confirm_yes() {
local prompt="$1"
if [[ "$INSTALL_YES" == "true" ]]; then return 0; fi
read -p "$prompt [Y/n] " -n 1 -r
echo ""
[[ $REPLY =~ ^[Yy]$|^$ ]]
}
_confirm_no() {
local prompt="$1"
if [[ "$INSTALL_YES" == "true" ]]; then return 0; fi
read -p "$prompt [y/N] " -n 1 -r
echo ""
[[ $REPLY =~ ^[Yy]$ ]]
}
# Colors (disabled if NO_COLOR is set or stdout is not a terminal)
if [[ -z "${NO_COLOR:-}" ]] && [[ -t 1 ]]; then
_green='\033[0;32m' _red='\033[0;31m' _yellow='\033[1;33m'
_blue='\033[0;34m' _nc='\033[0m'
else
_green='' _red='' _yellow='' _blue='' _nc=''
fi
_step() { echo -e " ${_green}✓${_nc} $*"; }
_info() { echo -e " ${_blue}→${_nc} $*"; }
_warn() { echo -e " ${_yellow}⚠${_nc} $*"; }
_fail() { echo -e " ${_red}✗${_nc} $*" >&2; exit 1; }
# ── Header ────────────────────────────────────────────────────────────────────
echo ""
echo " ┌───────────────────────────────────┐"
echo " │ dotbak installer │"
echo " │ Simple Dotfiles Manager │"
echo " └───────────────────────────────────┘"
echo ""
# ── Download (remote install only) ────────────────────────────────────────────
if [[ "$REMOTE_INSTALL" == "true" ]]; then
command -v curl >/dev/null 2>&1 || _fail "curl is required for remote install"
WORK_DIR=$(mktemp -d /tmp/dotbak-install.XXXXXX)
trap 'rm -rf "$WORK_DIR"' EXIT INT TERM
echo " Fetching latest release..."
LATEST_JSON=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${GITHUB_REPO}/releases/latest" 2>/dev/null) || true
if [[ -z "$LATEST_JSON" ]]; then
_fail "Could not reach GitHub. Install from source instead:
git clone https://github.com/${GITHUB_REPO} && cd dotbak && ./install.sh"
fi
TAG=$(echo "$LATEST_JSON" | grep '"tag_name"' | head -1 | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')
# Detect platform
INSTALL_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
INSTALL_ARCH=$(uname -m)
case "${INSTALL_OS}-${INSTALL_ARCH}" in
linux-x86_64) ARTIFACT_NAME="dotbak-linux-x86_64" ;;
darwin-arm64) ARTIFACT_NAME="dotbak-darwin-arm64" ;;
*) ARTIFACT_NAME="dotbak-portable" ;;
esac
BASE_URL="https://github.com/${GITHUB_REPO}/releases/download/${TAG}"
echo " Downloading ${TAG} (${INSTALL_OS}/${INSTALL_ARCH})..."
curl -fsSL "${BASE_URL}/${ARTIFACT_NAME}" -o "$WORK_DIR/dotbak" 2>/dev/null \
|| _fail "Download failed: ${BASE_URL}/${ARTIFACT_NAME}"
chmod +x "$WORK_DIR/dotbak"
# Verify checksum
curl -fsSL "${BASE_URL}/SHA256SUMS" -o "$WORK_DIR/SHA256SUMS" 2>/dev/null \
|| _fail "Could not download SHA256SUMS — refusing unverified install"
EXPECTED=$(grep " ${ARTIFACT_NAME}$" "$WORK_DIR/SHA256SUMS" | awk '{print $1}')
[[ -n "$EXPECTED" ]] || _fail "No checksum for '${ARTIFACT_NAME}' in SHA256SUMS"
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL=$(sha256sum "$WORK_DIR/dotbak" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
ACTUAL=$(shasum -a 256 "$WORK_DIR/dotbak" | awk '{print $1}')
else
_fail "sha256sum or shasum required to verify download"
fi
[[ "$ACTUAL" == "$EXPECTED" ]] || _fail "SHA256 mismatch — download may be corrupted"
_step "Downloaded and verified (SHA256)"
SCRIPT_DIR="$WORK_DIR"
fi
# ── Build (local install only) ────────────────────────────────────────────────
if [[ ! -f "$SCRIPT_DIR/dotbak" ]]; then
if [[ -f "$SCRIPT_DIR/build.sh" ]]; then
echo " Building from source..."
"$SCRIPT_DIR/build.sh" || _fail "Build failed"
echo ""
else
_fail "No dotbak binary found and no build.sh to build from"
fi
fi
# ── Preflight checks ─────────────────────────────────────────────────────────
echo ""
echo " Checking requirements..."
bash_version=$(bash --version | head -1 | sed 's/.*version \([0-9][0-9]*\.[0-9][0-9]*\).*/\1/')
bash_major=$(echo "$bash_version" | cut -d. -f1)
[[ $bash_major -ge 4 ]] || _fail "Bash 4.0+ required (found $bash_version)"
_step "bash $bash_version"
missing_commands=()
for cmd in ln cp mv grep find zip unzip; do
command -v "$cmd" >/dev/null 2>&1 || missing_commands+=("$cmd")
done
[[ ${#missing_commands[@]} -eq 0 ]] || _fail "Missing commands: ${missing_commands[*]}"
_step "All required commands available"
[[ -n "${HOME:-}" ]] || _fail "HOME is not set"
# ── Determine install location ────────────────────────────────────────────────
INSTALL_DIR=""
USE_SUDO=false
if sudo -n true 2>/dev/null; then
INSTALL_DIR="/usr/local/bin"
USE_SUDO=true
elif [[ -w "/usr/local/bin" ]]; then
INSTALL_DIR="/usr/local/bin"
else
INSTALL_DIR="$HOME/.local/bin"
mkdir -p "$INSTALL_DIR"
fi
# Get version of the binary we're about to install
NEW_VERSION=$("$SCRIPT_DIR/dotbak" --version 2>/dev/null | awk '{print $2}') || NEW_VERSION="unknown"
# ── Existing installation check ───────────────────────────────────────────────
EXISTING_VERSION=""
EXISTING_INSTALL=false
echo ""
if [[ -f "$INSTALL_DIR/dotbak" ]]; then
EXISTING_INSTALL=true
EXISTING_VERSION=$("$INSTALL_DIR/dotbak" --version 2>/dev/null | awk '{print $2}') || EXISTING_VERSION="unknown"
echo " Existing installation found:"
echo " Installed: $EXISTING_VERSION"
echo " Available: $NEW_VERSION"
echo " Location: $INSTALL_DIR/dotbak"
echo ""
if [[ "$EXISTING_VERSION" == "$NEW_VERSION" ]] && [[ "$NEW_VERSION" != "unknown" ]]; then
if ! _confirm_no " Same version — reinstall anyway?"; then
echo " Skipped. Already up to date."; echo ""; exit 0
fi
elif [[ "$EXISTING_VERSION" != "unknown" ]] && [[ "$NEW_VERSION" != "unknown" ]]; then
if ! _confirm_yes " Upgrade $EXISTING_VERSION → $NEW_VERSION?"; then
echo " Cancelled."; exit 0
fi
else
if ! _confirm_yes " Continue?"; then
echo " Cancelled."; exit 0
fi
fi
else
if [[ "$INSTALL_DIR" == "$HOME/.local/bin" ]]; then
echo " Installing dotbak $NEW_VERSION → $INSTALL_DIR (user-level)"
else
echo " Installing dotbak $NEW_VERSION → $INSTALL_DIR"
fi
fi
# ── Install binary ────────────────────────────────────────────────────────────
if [[ "$USE_SUDO" == "true" ]]; then
sudo cp "$SCRIPT_DIR/dotbak" "$INSTALL_DIR/dotbak.new"
sudo chmod +x "$INSTALL_DIR/dotbak.new"
sudo mv -f "$INSTALL_DIR/dotbak.new" "$INSTALL_DIR/dotbak"
else
cp "$SCRIPT_DIR/dotbak" "$INSTALL_DIR/dotbak.new"
chmod +x "$INSTALL_DIR/dotbak.new"
mv -f "$INSTALL_DIR/dotbak.new" "$INSTALL_DIR/dotbak"
fi
_step "Binary installed"
# ── Install shell completions ─────────────────────────────────────────────────
COMPLETIONS_INSTALLED=()
_install_completion() {
local shell_name="$1" src="$2" dest="$3" needs_sudo="${4:-false}"
[[ -f "$src" ]] || return 0
mkdir -p "$(dirname "$dest")"
if [[ "$needs_sudo" == "true" ]]; then
sudo cp "$src" "$dest"
else
cp "$src" "$dest"
fi
COMPLETIONS_INSTALLED+=("$shell_name")
}
# Bash
if [[ "$USE_SUDO" == "true" ]] && [[ -d "/usr/share/bash-completion/completions" ]]; then
_install_completion bash "$SCRIPT_DIR/completions/bash/dotbak" "/usr/share/bash-completion/completions/dotbak" true
elif [[ -d "/etc/bash_completion.d" ]] && [[ -w "/etc/bash_completion.d" ]]; then
_install_completion bash "$SCRIPT_DIR/completions/bash/dotbak" "/etc/bash_completion.d/dotbak"
else
_install_completion bash "$SCRIPT_DIR/completions/bash/dotbak" "$HOME/.local/share/bash-completion/completions/dotbak"
fi
# Zsh
if command -v zsh >/dev/null 2>&1; then
if [[ "$USE_SUDO" == "true" ]] && [[ -d "/usr/local/share/zsh/site-functions" ]]; then
_install_completion zsh "$SCRIPT_DIR/completions/zsh/_dotbak" "/usr/local/share/zsh/site-functions/_dotbak" true
else
_install_completion zsh "$SCRIPT_DIR/completions/zsh/_dotbak" "$HOME/.local/share/zsh/site-functions/_dotbak"
fi
fi
# Fish
if command -v fish >/dev/null 2>&1; then
_install_completion fish "$SCRIPT_DIR/completions/fish/dotbak.fish" "$HOME/.config/fish/completions/dotbak.fish"
fi
if [[ ${#COMPLETIONS_INSTALLED[@]} -gt 0 ]]; then
_step "Shell completions (${COMPLETIONS_INSTALLED[*]})"
fi
# ── PATH check ────────────────────────────────────────────────────────────────
if [[ "$USE_SUDO" == "false" ]] && [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
echo ""
_warn "$INSTALL_DIR is not in your PATH"
echo " Add to your shell profile:"
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
fi
# ── Optional: initialize dotbak ───────────────────────────────────────────────
INIT_RAN=false
if [[ ! -d "$HOME/.dotbak" ]]; then
echo ""
if _confirm_yes " Initialize dotbak now?"; then
echo ""
# Indent init output to match installer style
"$INSTALL_DIR/dotbak" init 2>&1 | sed 's/^/ /'
INIT_RAN=true
fi
fi
# ── Summary ───────────────────────────────────────────────────────────────────
echo ""
echo " ┌───────────────────────────────────┐"
if [[ "$EXISTING_INSTALL" == "true" ]]; then
if [[ "$EXISTING_VERSION" != "$NEW_VERSION" ]] && [[ "$EXISTING_VERSION" != "unknown" ]] && [[ "$NEW_VERSION" != "unknown" ]]; then
printf " │ Updated: %-12s → %-8s │\n" "$EXISTING_VERSION" "$NEW_VERSION"
else
printf " │ Reinstalled: %-19s │\n" "$NEW_VERSION"
fi
else
printf " │ Installed: dotbak %-14s │\n" "$NEW_VERSION"
fi
echo " └───────────────────────────────────┘"
if [[ "$INIT_RAN" == "false" ]]; then
echo ""
echo " Next steps:"
echo " dotbak init # initialize dotbak"
echo " dotbak add ~/.zshrc # start tracking a file"
echo " dotbak --help # see all commands"
fi
echo ""