Repository navigation
Expand file tree
/
Copy pathsetup
More file actions
executable file
Β·256 lines (215 loc) Β· 7.49 KB
/
Copy pathsetup
File metadata and controls
executable file
Β·256 lines (215 loc) Β· 7.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
#!/usr/bin/env bash
# Aman's Ultimate Environment Bootstrapper (Interactive & Automatable)
set -euo pipefail
# ==========================================
# βοΈ 1. ARGUMENT PARSING & FLAGS
# ==========================================
AUTO_MODE=false
ENV_TYPE=""
INSTALL_GUI=false
while [[ "$#" -gt 0 ]]; do
case $1 in
--auto) AUTO_MODE=true ;;
--gui) INSTALL_GUI=true ;;
--env=*) ENV_TYPE="${1#*=}" ;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
# ==========================================
# π§ SMART AUTO-DETECTION & TTY CHECK
# ==========================================
DEFAULT_ENV="linux"
if [ -n "${TERMUX_VERSION:-}" ]; then
DEFAULT_ENV="termux"
elif [ -f /.dockerenv ] || [ -f /run/.containerenv ]; then
DEFAULT_ENV="container"
elif grep -qi microsoft /proc/version 2>/dev/null; then
DEFAULT_ENV="wsl"
elif [[ "$OSTYPE" == "darwin"* ]]; then
DEFAULT_ENV="mac"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
DEFAULT_ENV="windows"
fi
# If stdin is not a terminal (e.g., curl | bash), force AUTO_MODE
if [ ! -t 0 ]; then
echo "β οΈ Piped execution detected. Forcing Auto Mode."
AUTO_MODE=true
fi
export ENV_TYPE
# ==========================================
# π£οΈ 2. INTERACTIVE MENU (If not in --auto mode)
# ==========================================
if [ "$AUTO_MODE" = false ]; then
echo ""
echo "π€ Welcome to the Environment Setup."
if [ -z "$ENV_TYPE" ]; then
echo "Select your target environment:"
echo " 1) Linux (Native/Debian/Arch)"
echo " 2) WSL (Windows Subsystem for Linux)"
echo " 3) macOS"
echo " 4) DevPod / Container (Ephemeral)"
echo " 5) Windows Git Bash"
# Force read from tty just in case
read -p "Enter choice [1-5] (Default: $DEFAULT_ENV): " env_choice </dev/tty
case $env_choice in
1) ENV_TYPE="linux" ;;
2) ENV_TYPE="wsl" ;;
3) ENV_TYPE="mac" ;;
4) ENV_TYPE="container" ;;
5) ENV_TYPE="windows" ;;
*) ENV_TYPE="$DEFAULT_ENV" ;;
esac
fi
if [[ "$ENV_TYPE" =~ ^(linux|mac|wsl)$ ]]; then
read -p "π₯οΈ Install GUI Desktop Apps (Kitty, Zathura, etc.)? (y/n): " gui_choice </dev/tty
if [[ "$gui_choice" =~ ^[Yy]$ ]]; then
INSTALL_GUI=true
fi
fi
else
# In AUTO_MODE, if ENV_TYPE wasn't explicitly passed via flag, use the auto-detected one
if [ -z "$ENV_TYPE" ]; then
ENV_TYPE="$DEFAULT_ENV"
echo "π€ Auto-detected environment: $ENV_TYPE"
fi
fi
export ENV_TYPE
export INSTALL_GUI
export AUTO_MODE
echo "========================================"
echo "π Starting System Bootstrap..."
echo "========================================"
# --- π± Termux Pre-Flight Check ---
if [ -n "${TERMUX_VERSION:-}" ]; then
ENV_TYPE="termux"
if ! command -v git >/dev/null 2>&1; then
echo "π± Termux detected without Git. Installing prerequisite..."
pkg update -y && pkg install git -y
fi
fi
DOTFILES_DIR="$HOME/.dotfiles"
# ==========================================
# π¦ 3. CLONE DOTFILES
# ==========================================
if [ ! -d "$DOTFILES_DIR" ]; then
echo "π¦ Cloning dotfiles repository..."
git clone https://github.com/Aman1337g/dotfiles.git "$DOTFILES_DIR"
else
echo "β
Dotfiles repository already exists at $DOTFILES_DIR"
fi
cd "$DOTFILES_DIR" || exit 1
chmod +x install_tools link setup
# ==========================================
# π§ Helper: Centralized Backup Management
# ==========================================
BACKUP_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}/dotfiles/backups"
KEEP_BACKUPS="${DOTFILES_KEEP_BACKUPS:-5}"
setup_backup_dest_for() {
local dest="$1"
local rel
if [[ "$dest" == "$HOME"/* ]]; then
rel="${dest#"$HOME"/}"
else
rel="${dest#/}"
rel="${rel//:/_}"
fi
printf '%s/%s' "$BACKUP_ROOT/$TIMESTAMP" "$rel"
}
setup_backup_existing() {
local dest="$1"
local backup_dest
backup_dest="$(setup_backup_dest_for "$dest")"
mkdir -p "$(dirname "$backup_dest")"
echo " π¦ Backing up $dest to $backup_dest"
mv "$dest" "$backup_dest"
}
setup_prune_old_backups() {
local keep="$1"
local backup_count=0
local old_backups=()
local backup_dir
[ -d "$BACKUP_ROOT" ] || return 0
while IFS= read -r backup_dir; do
backup_count=$((backup_count + 1))
if [ "$backup_count" -gt "$keep" ]; then
old_backups+=("$backup_dir")
fi
done < <(find "$BACKUP_ROOT" -mindepth 1 -maxdepth 1 -type d | sort -r)
if [ "${#old_backups[@]}" -eq 0 ]; then
return 0
fi
echo "π§Ή Pruning old backup runs (keeping newest $keep)..."
for backup_dir in "${old_backups[@]}"; do
echo " ποΈ Removing $backup_dir"
rm -rf "$backup_dir"
done
}
# ==========================================
# π 4. LINKING (Done BEFORE installing tools so Mise has its config)
# ==========================================
echo ""
echo "========================================"
echo "π Linking Dotfiles Configuration"
echo "========================================"
# Ensure stow is installed before trying to use it if on a brand new container
if ! command -v stow >/dev/null 2>&1; then
echo "β οΈ GNU Stow not found. We will install it during the tool phase."
echo "Falling back to safe link script for initial bootstrap..."
./link
else
echo "β
GNU Stow detected. Proceeding with modular links..."
echo " -> π§Ή Clearing runway for Stow (Handling old symlinks and defaults)..."
SHOULD_INSTALL_GUI=false
# 1. Expanded list of ALL targets (CLI + GUI)
STOW_TARGETS=(
~/.bashrc ~/.bash_aliases ~/.profile ~/.gitconfig ~/.tmux.conf ~/.ssh/config
~/.config/nvim ~/.config/starship.toml ~/.config/fastfetch ~/scripts ~/.config/mise
~/.config/qutebrowser ~/.config/kitty ~/.config/sway ~/.config/yazi ~/.config/zathura
)
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
mkdir -p "$BACKUP_ROOT"
for target in "${STOW_TARGETS[@]}"; do
if [ -L "$target" ]; then
rm "$target" # Safely remove old symlinks
elif [ -f "$target" ] || [ -d "$target" ]; then
setup_backup_existing "$target"
fi
done
echo " -> π» Stowing Core CLI Tools..."
stow --dir="$DOTFILES_DIR" --target="$HOME" bash scripts git nvim tmux starship fastfetch mise
if [ "$INSTALL_GUI" = true ]; then
SHOULD_INSTALL_GUI=true
elif [[ "$ENV_TYPE" =~ ^(linux|mac)$ ]] && { [ -n "${WAYLAND_DISPLAY:-}" ] || [ -n "${DISPLAY:-}" ]; }; then
SHOULD_INSTALL_GUI=true
fi
# Stow GUI only if requested
if [ "$SHOULD_INSTALL_GUI" = true ]; then
echo " -> π₯οΈ GUI Environment detected! Stowing Desktop Apps..."
stow --dir="$DOTFILES_DIR" --target="$HOME" code kitty fonts sway yazi zathura qutebrowser
if [ -f "scripts/scripts/install_themes" ]; then
chmod +x scripts/scripts/install_themes
./scripts/scripts/install_themes
fi
fi
setup_prune_old_backups "$KEEP_BACKUPS"
echo "ποΈ Backups stored under $BACKUP_ROOT"
fi
# ==========================================
# π οΈ 5. UNIVERSAL TOOL INSTALLER (Mise First)
# ==========================================
echo ""
echo "========================================"
echo "π οΈ Running Universal Tool Installer..."
echo "========================================"
# Pass the flags down to the install script
INSTALL_ARGS=("--env=$ENV_TYPE")
if [ "$INSTALL_GUI" = true ]; then INSTALL_ARGS+=("--gui"); fi
if [ "$AUTO_MODE" = true ]; then INSTALL_ARGS+=("--auto"); fi
./install_tools "${INSTALL_ARGS[@]}"
echo "========================================"
echo "β¨ Bootstrap Complete! Restart your terminal to see the magic."
echo "========================================"