-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbootstrap.sh
More file actions
296 lines (251 loc) · 7.03 KB
/
bootstrap.sh
File metadata and controls
296 lines (251 loc) · 7.03 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
#!/usr/bin/env bash
set -euo pipefail
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Options
FORCE=false
INSTALL_PACKAGES=false
DRY_RUN=false
# Colors
info() { echo -e "\033[34m[INFO]\033[0m $*"; }
success() { echo -e "\033[32m[OK]\033[0m $*"; }
warn() { echo -e "\033[33m[WARN]\033[0m $*"; }
error() { echo -e "\033[31m[ERROR]\033[0m $*" >&2; exit 1; }
# Detect OS
detect_os() {
case "$(uname -s)" in
Darwin) echo "mac" ;;
Linux) echo "linux" ;;
*) echo "unknown" ;;
esac
}
# Detect Linux package manager
detect_package_manager() {
if command -v apt-get &>/dev/null; then
echo "apt"
elif command -v dnf &>/dev/null; then
echo "dnf"
elif command -v yum &>/dev/null; then
echo "yum"
else
echo "unknown"
fi
}
# Create common directories
create_directories() {
info "Creating directories..."
local dirs=(
~/.config/nvim
~/.cache/tmp
~/.zsh
~/repo
~/work
~/tmp
~/.vim/undo
~/.vim/UltiSnips
)
for dir in "${dirs[@]}"; do
if [[ "$DRY_RUN" == true ]]; then
info "[DRY-RUN] mkdir -p $dir"
else
mkdir -p "$dir"
fi
done
success "Directories created"
}
# Create symlink
create_symlink() {
local src="$1"
local dst="$2"
if [[ ! -f "$src" && ! -d "$src" ]]; then
warn "Source not found: $src"
return 1
fi
if [[ "$DRY_RUN" == true ]]; then
info "[DRY-RUN] ln -sf $src -> $dst"
return 0
fi
ln -sf "$src" "$dst"
success "$dst -> $src"
}
# Setup for macOS
setup_mac() {
info "Setting up for macOS..."
# Symlinks
create_symlink "$DOTFILES_DIR/mac/zshrc" ~/.zshrc
create_symlink "$DOTFILES_DIR/mac/bashrc" ~/.bashrc
create_symlink "$DOTFILES_DIR/mac/vimrc" ~/.vimrc
create_symlink "$DOTFILES_DIR/mac/tmux.conf" ~/.tmux.conf
create_symlink "$DOTFILES_DIR/mac/curlrc" ~/.curlrc
create_symlink "$DOTFILES_DIR/mac/psqlrc" ~/.psqlrc
create_symlink "$DOTFILES_DIR/mac/mysqlrc" ~/.mysqlrc
create_symlink "$DOTFILES_DIR/mac/screenrc" ~/.screenrc
create_symlink "$DOTFILES_DIR/mac/git_ignore" ~/.gitignore_global
create_symlink "$DOTFILES_DIR/mac/sshrc" ~/.ssh/config
# Package installation
if [[ "$INSTALL_PACKAGES" == true ]]; then
install_mac_packages
fi
success "macOS setup complete"
}
# Setup for Linux
setup_linux() {
info "Setting up for Linux..."
# Symlinks
create_symlink "$DOTFILES_DIR/linux/zshrc" ~/.zshrc
create_symlink "$DOTFILES_DIR/linux/vimrc" ~/.vimrc
create_symlink "$DOTFILES_DIR/linux/tmux.conf" ~/.tmux.conf
create_symlink "$DOTFILES_DIR/linux/gdbinit" ~/.gdbinit
# Package installation
if [[ "$INSTALL_PACKAGES" == true ]]; then
install_linux_packages
fi
success "Linux setup complete"
}
# Install Homebrew packages (macOS)
install_mac_packages() {
info "Installing macOS packages..."
# Check/Install Homebrew
if ! command -v brew &>/dev/null; then
info "Installing Homebrew..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY-RUN] Install Homebrew"
else
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
fi
# Install from brew.list
local brew_list="$DOTFILES_DIR/mac/brew.list"
if [[ -f "$brew_list" ]]; then
info "Installing packages from brew.list..."
if [[ "$DRY_RUN" == true ]]; then
info "[DRY-RUN] brew install < $brew_list"
else
while IFS= read -r package || [[ -n "$package" ]]; do
[[ -z "$package" || "$package" =~ ^# ]] && continue
brew install "$package" 2>/dev/null || warn "Failed to install: $package"
done < "$brew_list"
fi
fi
success "macOS packages installed"
}
# Install Linux packages
install_linux_packages() {
info "Installing Linux packages..."
local pkg_manager
pkg_manager=$(detect_package_manager)
if [[ "$pkg_manager" == "unknown" ]]; then
warn "Unknown package manager. Skipping package installation."
return 1
fi
local packages=(
git
vim
tmux
zsh
curl
wget
jq
fzf
ripgrep
htop
tree
)
if [[ "$DRY_RUN" == true ]]; then
info "[DRY-RUN] $pkg_manager install ${packages[*]}"
return 0
fi
case "$pkg_manager" in
apt)
sudo apt-get update
sudo apt-get install -y "${packages[@]}"
;;
dnf)
sudo dnf install -y "${packages[@]}"
;;
yum)
sudo yum install -y "${packages[@]}"
;;
esac
success "Linux packages installed"
}
# Show help
show_help() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Setup dotfiles for macOS or Linux.
OPTIONS:
-f, --force Run without confirmation prompt
-p, --packages Also install packages (Homebrew/apt/dnf/yum)
-n, --dry-run Show what would be done without making changes
-h, --help Show this help message
EXAMPLES:
$(basename "$0") # Interactive setup
$(basename "$0") -f # Force setup without confirmation
$(basename "$0") -f -p # Force setup with package installation
$(basename "$0") -n # Dry run to see what would happen
EOF
}
# Parse arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-f|--force)
FORCE=true
shift
;;
-p|--packages)
INSTALL_PACKAGES=true
shift
;;
-n|--dry-run)
DRY_RUN=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
error "Unknown option: $1"
;;
esac
done
}
# Main
main() {
parse_args "$@"
local os
os=$(detect_os)
info "Detected OS: $os"
info "Dotfiles directory: $DOTFILES_DIR"
if [[ "$os" == "unknown" ]]; then
error "Unsupported OS: $(uname -s)"
fi
# Confirmation
if [[ "$FORCE" != true && "$DRY_RUN" != true ]]; then
read -rp "This may overwrite existing files. Continue? (y/n) " -n 1
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
info "Aborted."
exit 0
fi
fi
# Update dotfiles
if [[ "$DRY_RUN" != true ]]; then
info "Updating dotfiles repository..."
git -C "$DOTFILES_DIR" pull origin master 2>/dev/null || \
git -C "$DOTFILES_DIR" pull origin main 2>/dev/null || \
warn "Failed to pull latest changes"
fi
# Create directories
create_directories
# OS-specific setup
case "$os" in
mac) setup_mac ;;
linux) setup_linux ;;
esac
echo
success "Dotfiles setup complete!"
info "Restart your shell or run: source ~/.zshrc"
}
main "$@"