-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·350 lines (279 loc) · 8.59 KB
/
install.sh
File metadata and controls
executable file
·350 lines (279 loc) · 8.59 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
# Dotfiles and bootstrap installer
# Installs homebrew, core dependencies and chezmoi
set -euo pipefail
trap on_error ERR
e='\033'
RESET="${e}[0m"
BOLD="${e}[1m"
CYAN="${e}[0;96m"
RED="${e}[0;91m"
YELLOW="${e}[0;93m"
GREEN="${e}[0;92m"
_exists() {
command -v "$1" > /dev/null 2>&1
}
# Success reporter
info() {
echo -e "${CYAN}${*}${RESET}"
}
# Error reporter
error() {
echo -e "${RED}${*}${RESET}"
}
# Success reporter
success() {
echo -e "${GREEN}${*}${RESET}"
}
# End section
finish() {
success "Done!"
echo
sleep 1
}
# Set directory
GITHUB_REPO="willscottuk/dotfiles"
HOMEBREW_INSTALLER_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
on_start() {
info " __ __ ____ _ __ "
info " ____/ /____ / /_ / __/(_)/ /___ _____"
info " / __ // __ \ / __// /_ / // // _ \ / ___/"
info " _ / /_/ // /_/ // /_ / __// // // __/(__ ) "
info " (_)\__,_/ \____/ \__//_/ /_//_/ \___//____/ "
info " "
info " by @willscottuk "
info " "
info "👋 This script will setup .dotfiles for you."
echo " It will not install anything without your direct agreement!"
echo
read -p " Do you want to proceed with installation? [y/N] " -n 1 answer
echo
if [ "${answer}" != "y" ]; then
exit 1
fi
}
root_check() {
info "👮♂️ Checking user permissions"
if (( $EUID != 0 )); then
return
else
# We can't install Homebrew as root; we need to create a user
if ! _exists brew; then
read -p "Enter username : " username
read -s -p "Enter password : " password
#echo $username
if id "$username" &>/dev/null; then
error "user ${username} already exists"
exit 1
else
echo 'user does not already exist'
useradd -m -p $(openssl passwd -1 $password) $username
[ $? -eq 0 ] && success "User has been added to system!" || error "Failed to add the user!"
# Add them to sudoers
echo $username' ALL=(ALL:ALL) ALL' >> /etc/sudoers
# Add ssh keys
# Make directory
sudo -u $username mkdir -p /home/$username/.ssh
sudo -u $username touch /home/$username/.ssh/authorized_keys
curl --output >(cat >> /home/$username/.ssh/authorized_keys) https://raw.githubusercontent.com/${GITHUB_REPO}/main/private_dot_ssh/authorized_keys
IPV4_ADDRESS=$(curl -s checkip.amazonaws.com)
error "🧑💻 Now ssh as the new user"
echo " ssh -A ${username}"
exit 0
fi
fi
fi
}
install_homebrew() {
info "🍺 Trying to detect installed Homebrew..."
if ! _exists brew; then
echo " Seems like you don't have Homebrew installed!"
read -p " Proceed with Homebrew installation? [y/N] " -n 1 answer
echo
if [ "${answer}" != "y" ]; then
exit 1
fi
info " Installing Homebrew..."
## TODO - If Linux, install dependencies: sudo apt-get install build-essential
## TODO -
bash -c "$(curl -fsSL ${HOMEBREW_INSTALLER_URL})"
if [ "$(uname)" = "Darwin" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
else
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
else
success "⏭️ You already have Homebrew installed. Skipping..."
fi
finish
}
install_git() {
info "Trying to detect installed Git..."
if ! _exists git; then
echo "Seems like you don't have Git installed!"
read -p "Do you agree to proceed with Git installation? [y/N] " -n 1 answer
echo
if [ "${answer}" != "y" ]; then
exit 1
fi
info "Installing Git..."
if [ "$(uname)" = "Darwin" ]; then
brew install git
elif [ "$(uname)" = "Linux" ]; then
sudo apt-get install git
else
error "Error: Failed to install Git!"
exit 1
fi
else
success "⏭️ You already have Git installed. Skipping..."
fi
finish
}
install_zsh() {
info "Trying to detect installed Zsh..."
if ! _exists zsh; then
echo "Seems like you don't have Zsh installed!"
read -p "Do you agree to proceed with Zsh installation? [y/N] " -n 1 answer
echo
if [ "${answer}" != "y" ]; then
exit 1
fi
info "Installing Zsh..."
if [ "$(uname)" = "Darwin" ]; then
brew install zsh zsh-completions
elif [ "$(uname)" = "Linux" ]; then
brew install zsh zsh-completions
else
error "Error: Failed to install Zsh!"
exit 1
fi
else
success "⏭️ You already have Zsh installed. Skipping..."
fi
if _exists zsh; then
info "Setting up Zsh as default shell..."
echo "The script will ask you the password for sudo:"
echo
echo "1) When changing your default shell via chsh -s"
echo
command -v zsh | sudo tee -a /etc/shells
chsh -s "$(command -v zsh)" || error "Error: Cannot set Zsh as default shell!"
fi
finish
}
install_software() {
info "🍻 Installing from Brewfile"
if ! _exists brew; then
error "🚨 Error: Brew is not available"
return
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHEZMOI_SOURCE="${HOME}/.local/share/chezmoi"
if [ "$(uname)" = "Darwin" ]; then
BREWFILE_NAME="Brewfile"
else
BREWFILE_NAME="Brewfile.linux"
fi
BREWFILE="${SCRIPT_DIR}/setup/${BREWFILE_NAME}"
[ -f "$BREWFILE" ] || BREWFILE="${CHEZMOI_SOURCE}/setup/${BREWFILE_NAME}"
if [ ! -f "$BREWFILE" ]; then
error "🚨 Brewfile not found — skipping"
return
fi
brew bundle --file="$BREWFILE"
finish
}
install_omzsh() {
if [ ! -f ~/.oh-my-zsh/oh-my-zsh.sh ]; then
info "💰 Installing oh-my-zsh"
(yes | sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)")
fi
info "⏭️ You already have Oh My Zsh installed. Skipping..."
finish
}
configure_sudo_ssh() {
info "🔑 Configuring sudo authentication via SSH agent..."
# Preserve SSH_AUTH_SOCK when sudoing
local sudoers_file="/etc/sudoers.d/ssh-auth-sock"
if [ -f "$sudoers_file" ]; then
success "⏭️ SSH agent sudo config already exists. Skipping..."
else
echo 'Defaults env_keep += "SSH_AUTH_SOCK"' | sudo tee "$sudoers_file" > /dev/null
sudo chmod 440 "$sudoers_file"
success "SSH_AUTH_SOCK preserved in sudo environment."
fi
# Linux only: install pam_ssh_agent_auth so sudo authenticates via SSH key
if [ "$(uname)" = "Linux" ]; then
if dpkg -s libpam-ssh-agent-auth &>/dev/null 2>&1; then
success "⏭️ libpam-ssh-agent-auth already installed. Skipping..."
else
info " Installing libpam-ssh-agent-auth..."
sudo apt-get install -y libpam-ssh-agent-auth
fi
local pam_sudo="/etc/pam.d/sudo"
local pam_line="auth sufficient pam_ssh_agent_auth.so file=%h/.ssh/authorized_keys"
if grep -qF "pam_ssh_agent_auth" "$pam_sudo"; then
success "⏭️ PAM sudo already configured. Skipping..."
else
# Insert before the first auth or @include line (Ubuntu uses @include common-auth)
sudo sed -i "0,/^auth\|^@include/s||${pam_line}\n&|" "$pam_sudo"
success "PAM sudo configured to use SSH agent."
fi
fi
finish
}
install_chezmoi() {
if ! _exists chezmoi; then
info "🏡 Installing chezmoi"
brew install chezmoi
else
success "⏭️ You already have Chezmoi installed. Skipping..."
fi
if [ -d "$HOME/.local/share/chezmoi/.git" ]; then
info "🚸 chezmoi already initialized, applying..."
else
info "🚀 Initializing chezmoi from ${GITHUB_REPO}..."
chezmoi init "https://github.com/${GITHUB_REPO}.git"
fi
info "📦 Applying dotfiles..."
chezmoi apply
finish
}
on_finish() {
echo
success "Setup was successfully done!"
success "Happy Coding!"
echo
echo -ne "$RED"'-_-_-_-_-_-_-_-_-_-_-_-_-_-_'
echo -e "$RESET""$BOLD"',------,'"$RESET"
echo -ne "$YELLOW"'-_-_-_-_-_-_-_-_-_-_-_-_-_-_'
echo -e "$RESET""$BOLD"'| /\_/\\'"$RESET"
echo -ne "$GREEN"'-_-_-_-_-_-_-_-_-_-_-_-_-_-'
echo -e "$RESET""$BOLD"'~|__( ^ .^)'"$RESET"
echo -ne "$CYAN"'-_-_-_-_-_-_-_-_-_-_-_-_-_-_'
echo -e "$RESET""$BOLD"'"" ""'"$RESET"
echo
info "P.S: Don't forget to restart a terminal :)"
echo
}
on_error() {
echo
error "Wow... Something serious happened!"
error "Though, I don't know what really happened :("
error "In case, you want to help me fix this problem, raise an issue -> ${CYAN}https://github.com/${GITHUB_REPO}/issues/new${RESET}"
echo
exit 1
}
main() {
on_start "$*"
root_check "$*"
install_homebrew "$*"
install_git "$*"
install_zsh "$*"
install_chezmoi "$*"
install_software "$*"
configure_sudo_ssh "$*"
on_finish "$*"
}
main "$*"