-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
230 lines (202 loc) · 5.97 KB
/
install.sh
File metadata and controls
230 lines (202 loc) · 5.97 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
#!/usr/bin/env bash
set -euo pipefail
# ------------------------------------------------------------
# install.sh
# Stateless dotfiles installer (Bash)
# ------------------------------------------------------------
# ------------------------------------------------------------
# Guards
# ------------------------------------------------------------
require_command() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "[ERROR] Required command not found: $cmd" >&2
echo "Install it with:" >&2
echo " sudo apt install $cmd" >&2
exit 1
fi
}
require_command jq
# ------------------------------------------------------------
# Args
# ------------------------------------------------------------
DRY_RUN=false
CHECK=false
FORCE=false
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--check) CHECK=true ;;
--force) FORCE=true ;;
*)
echo "[ERROR] Unknown argument: $arg"
exit 1
;;
esac
done
# ------------------------------------------------------------
# Paths
# ------------------------------------------------------------
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_ROOT="$REPO_ROOT/dotfiles"
MAP_FILE="$REPO_ROOT/dotfiles.map.json"
BACKUP_ROOT="$REPO_ROOT/.backup"
TIMESTAMP="$(date +"%Y-%m-%d_%H-%M-%S")"
BACKUP_RUN_DIR="$BACKUP_ROOT/$TIMESTAMP"
TRANSCRIPT="$BACKUP_RUN_DIR/transcript.txt"
# ------------------------------------------------------------
# Logging
# ------------------------------------------------------------
info() { echo "[INFO] $*"; }
warn() { echo "[WARN] $*" >&2; }
err() { echo "[ERROR] $*" >&2; }
# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------
resolve_path() {
local path="$1"
if command -v realpath >/dev/null 2>&1; then
realpath "$path"
elif command -v python3 >/dev/null 2>&1; then
python3 - <<'PY' "$path"
import os,sys
print(os.path.realpath(sys.argv[1]))
PY
else
warn "Unable to resolve path; realpath/python3 not found: $path"
printf '%s\n' "$path"
fi
}
resolve_home() {
case "$1" in
\~/*) echo "$HOME/${1:2}" ;;
*) echo "$1" ;;
esac
}
ensure_dir() {
if [[ ! -d "$1" ]]; then
if $DRY_RUN || $CHECK; then
info "Would create directory: $1"
else
mkdir -p "$1"
fi
fi
}
is_repo_owned_link() {
local target="$1"
[[ -L "$target" ]] || return 1
local resolved
resolved="$(resolve_path "$target")"
[[ "$resolved" == "$REPO_ROOT"* ]]
}
backup_target() {
local target="$1"
local rel="${target#/}"
local dest="$BACKUP_RUN_DIR/$rel"
ensure_dir "$(dirname "$dest")"
if $DRY_RUN; then
info "Would backup: $target -> $dest"
else
if [[ -L "$target" ]]; then
cp -aL "$target" "$dest"
else
cp -a "$target" "$dest"
fi
info "Backed up: $target"
fi
}
# ------------------------------------------------------------
# Validation
# ------------------------------------------------------------
[[ -f "$MAP_FILE" ]] || { err "Mapping file not found: $MAP_FILE"; exit 1; }
jq -e '
to_entries | all(
.key and (.value.target | type == "string")
)
' "$MAP_FILE" >/dev/null || { err "Invalid map file format: $MAP_FILE"; exit 1; }
# ------------------------------------------------------------
# Transcript (not in --check)
# ------------------------------------------------------------
if ! $CHECK && ! $DRY_RUN; then
ensure_dir "$BACKUP_RUN_DIR"
if ! exec > >(tee -a "$TRANSCRIPT") 2>&1; then
err "Failed to start transcript: $TRANSCRIPT"
exit 1
fi
fi
if [[ $CHECK == true ]]; then
MODE="CHECK"
elif [[ $DRY_RUN == true ]]; then
MODE="DRY-RUN"
else
MODE="INSTALL"
fi
info "Mode : $MODE"
info "RepoRoot : $REPO_ROOT"
# ------------------------------------------------------------
# Process mappings
# ------------------------------------------------------------
jq -r 'to_entries[] | "\(.key)|\(.value.target)|\(.value.platforms // empty | join(","))"' "$MAP_FILE" |
while IFS="|" read -r key target platforms; do
SOURCE="$DOTFILES_ROOT/$key"
TARGET="$(resolve_home "$target")"
[[ -e "$SOURCE" ]] || { warn "Source missing: $SOURCE"; continue; }
if [[ -n "$platforms" ]]; then
uname_s="$(uname | tr '[:upper:]' '[:lower:]')"
if ! printf '%s\n' "$platforms" | tr ',' '\n' | grep -Fxq "$uname_s"; then
continue
fi
fi
info "Processing: $key"
if $CHECK; then
if [[ ! -e "$TARGET" ]]; then
warn "MISSING : $TARGET"
elif ! is_repo_owned_link "$TARGET"; then
warn "FOREIGN : $TARGET"
else
info "OK : $TARGET"
fi
continue
fi
if [[ -e "$TARGET" ]] && ! is_repo_owned_link "$TARGET"; then
if $FORCE; then
info "Force enabled; backing up then overwriting: $TARGET"
fi
backup_target "$TARGET"
if $DRY_RUN; then
info "Would remove: $TARGET"
else
rm -rf "$TARGET"
fi
fi
ensure_dir "$(dirname "$TARGET")"
if $DRY_RUN; then
info "Would link: $TARGET -> $SOURCE"
else
ln -sfn "$SOURCE" "$TARGET"
info "Linked: $TARGET -> $SOURCE"
# If the source is a regular file and looks like a script (shebang), has
# a common script extension, or already has the executable bit, ensure
# the real file is executable. For symlinks the source file is the real
# file; for copies (not used on this path) the target should be chmod'd.
if [[ -f "$SOURCE" ]]; then
first_line=$(head -n1 "$SOURCE" 2>/dev/null || true)
ext="${SOURCE##*.}"
case "$ext" in
sh|bash|zsh|ksh|py|pl|rb|awk|sed|ps1|js) is_ext=true ;;
*) is_ext=false ;;
esac
if [[ "$first_line" == \#!* ]] || [[ -x "$SOURCE" ]] || [[ "$is_ext" == true ]]; then
if $DRY_RUN; then
info "Would set executable on source: $SOURCE"
else
# Prefer setting executable on the source (the real file).
if ! chmod +x "$SOURCE" 2>/dev/null; then
warn "chmod failed on source: $SOURCE"
fi
fi
fi
fi
fi
done
info "Done."