forked from andromedarabbit/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·217 lines (181 loc) · 6.44 KB
/
Copy pathbootstrap.sh
File metadata and controls
executable file
·217 lines (181 loc) · 6.44 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
#!/usr/bin/env bash
#
# bootstrap.sh - Dotfiles installation script
set -o pipefail
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m' # No Color
# Logging functions
log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
#
# Ask for the administrator password upfront.
sudo -v || { log_error "Failed to get sudo access"; exit 1; }
# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done 2>/dev/null &
# Load the configurations
readonly THIS_DIR=$(cd "$(dirname "$0")" && pwd)
readonly CONFIG_FILE="$THIS_DIR/.config"
if [[ ! -f "$CONFIG_FILE" ]]; then
log_error "Configuration file not found: $CONFIG_FILE"
exit 1
fi
# shellcheck source=.config
source "$CONFIG_FILE"
if [[ -z "$BREW_OS" ]]; then
log_error "BREW_OS not set in configuration"
exit 1
fi
# Install Linux prerequisites if needed
install_linux_prereqs() {
case "$BREW_OS" in
ubuntu)
log_info "Installing Ubuntu prerequisites..."
if ! sudo apt update; then
log_error "Failed to update package lists"
return 1
fi
if ! sudo apt install -y build-essential procps curl file git; then
log_error "Failed to install required packages"
return 1
fi
sudo apt autoremove --purge -y || log_warn "Failed to autoremove packages"
;;
fedora)
log_info "Installing Fedora prerequisites..."
if ! sudo dnf install -y gcc gcc-c++ make procps-ng curl file git; then
log_error "Failed to install required packages"
return 1
fi
;;
esac
}
# Install or verify Homebrew
install_homebrew() {
if command -v brew >/dev/null 2>&1; then
log_info "Homebrew already installed"
return 0
fi
log_info "Installing Homebrew..."
if ! /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; then
log_error "Failed to install Homebrew"
return 1
fi
}
# Configure Homebrew PATH for Linux and macOS
configure_brew_path() {
if [[ "$BREW_OS" == "ubuntu" || "$BREW_OS" == "fedora" ]]; then
local brew_shellenv='eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"'
if ! grep -q "linuxbrew" ~/.profile 2>/dev/null; then
log_info "Adding Homebrew to PATH in ~/.profile"
{
echo
echo "$brew_shellenv"
} >> ~/.profile
fi
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
elif [[ "$BREW_OS" == "macos" ]]; then
if [[ -x /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -x /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
}
# Update and install packages
install_packages() {
log_info "Updating Homebrew..."
brew update || { log_error "Failed to update Homebrew"; return 1; }
log_info "Upgrading existing packages..."
brew upgrade || log_warn "Some packages failed to upgrade"
if [[ -f "$THIS_DIR/Brewfile" ]]; then
log_info "Installing base packages from Brewfile..."
brew bundle --file="$THIS_DIR/Brewfile" || log_warn "Some Brewfile packages failed to install"
else
log_warn "No Brewfile found"
fi
}
# Run install scripts
run_installers() {
local installer_count=0
for dir in "shared" "$BREW_OS"; do
if [[ ! -d "$THIS_DIR/$dir" ]]; then
log_warn "Directory not found: $THIS_DIR/$dir"
continue
fi
local installers=()
while IFS= read -r -d '' installer; do
installers+=("$installer")
done < <(find "$THIS_DIR/$dir" -name "install.sh" -print0)
for installer in "${installers[@]}"; do
log_info "Running installer: $installer"
chmod +x "$installer"
if "$installer"; then
log_info "Successfully ran: $installer"
((installer_count++))
else
log_error "Failed to run: $installer"
fi
done
done
log_info "Ran $installer_count installers"
}
# Stow packages with conflict handling
stow_packages() {
local stow_args=(--restow --target="$HOME" --ignore="install*")
# Handle personal configurations
if [[ -d "$THIS_DIR/not-shared" ]]; then
log_info "Stowing personal configurations..."
if stow "${stow_args[@]}" "not-shared"; then
log_info "Successfully stowed not-shared"
else
log_error "Failed to stow not-shared"
fi
fi
# Stow shared packages
if [[ -d "$THIS_DIR/shared" ]]; then
log_info "Stowing shared packages..."
while IFS= read -r -d '' package_dir; do
local package_name=$(basename "$package_dir")
log_info "Stowing shared package: $package_name"
if stow --dir="$THIS_DIR/shared" "${stow_args[@]}" "$package_name"; then
log_info "Successfully stowed: $package_name"
else
log_error "Failed to stow: $package_name"
fi
done < <(find "$THIS_DIR/shared" -mindepth 1 -maxdepth 1 -type d -print0)
fi
# Stow OS-specific packages
if [[ -d "$THIS_DIR/$BREW_OS" ]]; then
log_info "Stowing $BREW_OS packages..."
while IFS= read -r -d '' package_dir; do
local package_name=$(basename "$package_dir")
log_info "Stowing $BREW_OS package: $package_name"
if stow --dir="$THIS_DIR/$BREW_OS" "${stow_args[@]}" "$package_name"; then
log_info "Successfully stowed: $package_name"
else
log_error "Failed to stow: $package_name"
fi
done < <(find "$THIS_DIR/$BREW_OS" -mindepth 1 -maxdepth 1 -type d -print0)
fi
}
# Main execution
main() {
log_info "Starting dotfiles bootstrap..."
install_linux_prereqs || log_error "Linux prerequisites installation failed"
install_homebrew || { log_error "Homebrew installation failed"; exit 1; }
configure_brew_path
install_packages || log_error "Package installation had issues"
run_installers
stow_packages
log_info "Bootstrap completed!"
}
# Run main function
main "$@"