-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·302 lines (225 loc) · 8.49 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·302 lines (225 loc) · 8.49 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
#!/usr/bin/env bash
# macOS bootstrap — idempotent + shell agnostic
#
# Usage:
# bash <(curl -fsSL https://raw.githubusercontent.com/rbhanot4739/dotfiles/main/install.sh)
#
# Safe to re-run anytime from:
# - bash
# - zsh
# - sh (as long as bash exists for execution)
#
# Guarantees:
# - no duplicate shell config entries
# - no duplicate SSH uploads
# - no repeated installs
# - safe chezmoi re-apply
# - works across Intel + Apple Silicon
set -Eeuo pipefail
###############################################################################
# UI
###############################################################################
bold="$(tput bold 2>/dev/null || true)"
reset="$(tput sgr0 2>/dev/null || true)"
info() { printf "\n${bold}→ %s${reset}\n" "$*"; }
ok() { printf "✓ %s\n" "$*"; }
warn() { printf "⚠ %s\n" "$*"; }
error() { printf "✗ %s\n" "$*" >&2; }
echo ""
echo "╔══════════════════════════════════════════════════════╗"
echo "║ dotfiles bootstrap — fresh macOS setup ║"
echo "╚══════════════════════════════════════════════════════╝"
echo ""
###############################################################################
# REQUIRE INTERACTIVE TERMINAL
###############################################################################
if [[ ! -t 0 || ! -t 1 ]]; then
error "This script requires an interactive terminal."
exit 1
fi
TTY="/dev/tty"
###############################################################################
# HELPERS
###############################################################################
append_if_missing() {
local line="$1"
local file="$2"
mkdir -p "$(dirname "$file")"
touch "$file"
grep -Fqs "$line" "$file" || printf '\n%s\n' "$line" >> "$file"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
brew_bin() {
if [[ -x /opt/homebrew/bin/brew ]]; then
echo "/opt/homebrew/bin/brew"
elif [[ -x /usr/local/bin/brew ]]; then
echo "/usr/local/bin/brew"
else
return 1
fi
}
###############################################################################
# GITHUB USERNAME
###############################################################################
if [[ -z "${GITHUB_USERNAME:-}" ]]; then
printf "GitHub username: "
read -r GITHUB_USERNAME < "${TTY}"
fi
export GITHUB_USERNAME
ok "Using GitHub username: ${GITHUB_USERNAME}"
###############################################################################
# SUDO
###############################################################################
info "Requesting sudo access..."
sudo -v < "${TTY}"
(
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done
) 2>/dev/null &
SUDO_KEEPALIVE_PID=$!
cleanup() {
kill "${SUDO_KEEPALIVE_PID}" 2>/dev/null || true
}
trap cleanup EXIT
###############################################################################
# XCODE CLT
###############################################################################
if ! xcode-select -p >/dev/null 2>&1; then
info "Installing Xcode Command Line Tools..."
xcode-select --install || true
info "Waiting for Xcode Command Line Tools installation..."
until xcode-select -p >/dev/null 2>&1; do
sleep 5
done
ok "Xcode Command Line Tools installed."
else
ok "Xcode Command Line Tools already installed."
fi
###############################################################################
# HOMEBREW
###############################################################################
if ! command_exists brew; then
info "Installing Homebrew..."
NONINTERACTIVE=1 /bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
ok "Homebrew installed."
else
ok "Homebrew already installed."
fi
BREW_BIN="$(brew_bin)"
if [[ -z "${BREW_BIN:-}" ]]; then
error "brew not found after installation."
exit 1
fi
eval "$("${BREW_BIN}" shellenv)"
###############################################################################
# PERSIST BREW SHELLENV (IDEMPOTENT)
###############################################################################
BREW_SHELLENV_LINE="eval \"\$(${BREW_BIN} shellenv)\""
append_if_missing "${BREW_SHELLENV_LINE}" "${HOME}/.zprofile"
# bash users
append_if_missing "${BREW_SHELLENV_LINE}" "${HOME}/.bash_profile"
###############################################################################
# HOMEBREW PACKAGES
###############################################################################
brew_install_if_missing() {
local formula="$1"
if brew list --formula | grep -Fxq "$formula"; then
ok "${formula} already installed."
else
info "Installing ${formula}..."
brew install "${formula}"
fi
}
brew_install_if_missing gh
brew_install_if_missing chezmoi
###############################################################################
# GITHUB AUTH
###############################################################################
if gh auth status >/dev/null 2>&1; then
ok "Already authenticated with GitHub."
else
info "GitHub authentication required."
info "A browser window may open."
gh auth login --web --git-protocol ssh
fi
###############################################################################
# SSH SETUP
###############################################################################
SSH_DIR="${HOME}/.ssh"
SSH_KEY="${SSH_DIR}/id_ed25519"
SSH_PUB="${SSH_KEY}.pub"
HOSTNAME_SHORT="$(hostname -s)"
mkdir -p "${SSH_DIR}"
chmod 700 "${SSH_DIR}"
if [[ ! -f "${SSH_KEY}" ]]; then
info "Generating SSH key..."
ssh-keygen \
-t ed25519 \
-C "${GITHUB_USERNAME}@${HOSTNAME_SHORT}" \
-f "${SSH_KEY}" \
-N ""
ok "SSH key generated."
else
ok "SSH key already exists."
fi
###############################################################################
# SSH AGENT
###############################################################################
if [[ -z "${SSH_AUTH_SOCK:-}" ]]; then
eval "$(ssh-agent -s)" >/dev/null
fi
ssh-add --apple-use-keychain "${SSH_KEY}" >/dev/null 2>&1 || true
###############################################################################
# SSH CONFIG (IDEMPOTENT)
###############################################################################
SSH_CONFIG="${SSH_DIR}/config"
append_if_missing "Host github.com" "${SSH_CONFIG}"
append_if_missing " AddKeysToAgent yes" "${SSH_CONFIG}"
append_if_missing " UseKeychain yes" "${SSH_CONFIG}"
append_if_missing " IdentityFile ~/.ssh/id_ed25519" "${SSH_CONFIG}"
chmod 600 "${SSH_CONFIG}"
###############################################################################
# ADD SSH KEY TO GITHUB (IDEMPOTENT)
###############################################################################
PUBKEY_CONTENT="$(cat "${SSH_PUB}")"
if gh ssh-key list | grep -Fq "${PUBKEY_CONTENT}"; then
ok "SSH key already uploaded to GitHub."
else
info "Uploading SSH key to GitHub..."
gh ssh-key add "${SSH_PUB}" \
--title "${HOSTNAME_SHORT}"
ok "SSH key uploaded."
fi
###############################################################################
# VERIFY SSH ACCESS
###############################################################################
info "Verifying GitHub SSH access..."
ssh -o StrictHostKeyChecking=accept-new \
-T git@github.com || true
###############################################################################
# CHEZMOI
###############################################################################
DOTFILES_REPO="git@github.com:${GITHUB_USERNAME}/dotfiles.git"
if [[ -d "${HOME}/.local/share/chezmoi/.git" ]]; then
info "Updating existing chezmoi repo..."
chezmoi update
else
info "Initializing chezmoi..."
chezmoi init --apply "${DOTFILES_REPO}"
fi
###############################################################################
# DONE
###############################################################################
echo ""
echo "╔══════════════════════════════════════════════════════╗"
echo "║ ✅ Bootstrap complete! ║"
echo "║ Re-run this script anytime safely. ║"
echo "║ Open a new terminal to reload your environment. ║"
echo "╚══════════════════════════════════════════════════════╝"
echo ""