Repository navigation
Expand file tree
/
Copy pathsetup
More file actions
executable file
·438 lines (404 loc) · 17.9 KB
/
Copy pathsetup
File metadata and controls
executable file
·438 lines (404 loc) · 17.9 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/sh
# Copyright © 2026 Gornskew Enterprises
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. Distributed WITHOUT
# ANY WARRANTY; see <https://www.gnu.org/licenses/agpl-3.0.html>.
# setup — POSIX sh; works with bash, dash, zsh, and macOS /bin/sh
# =============================================================================
# PORTABLE REALPATH
# realpath(1) is not available on macOS or older Linux without coreutils.
# readlink -f also absent on macOS BSD readlink.
# This pure-shell fallback works everywhere.
# =============================================================================
if ! command -v realpath > /dev/null 2>&1; then
realpath() {
# Resolve to absolute path without requiring the target to exist.
# If the path exists, cd into its directory and print; else compose manually.
case "$1" in
/*) printf '%s\n' "$1" ;;
*) printf '%s/%s\n' "$(pwd)" "$1" ;;
esac
}
fi
# =============================================================================
# HELP
# =============================================================================
show_help() {
echo ""
echo "Skewed Emacs Config Setup"
echo "========================="
echo "Sets up your home directory with symlinks to dot-files in this repo."
echo ""
echo "Usage: ./setup [OPTIONS]"
echo ""
echo "Options:"
echo " --help, -h Show this help message and exit"
echo " --dry-run Show what would happen without making changes"
echo " --force, -f Force replacement of existing valid symlinks"
echo ""
echo " --with-mcp Enable the Lisply MCP server in your HOST Emacs"
echo " (opt-in; prompts to confirm; grants an LLM arbitrary"
echo " code execution on this machine; see docs/HOST_EMACS_MCP.md)"
echo ""
echo " --shadow-suffix=NAME Create symlinks with a \"-NAME\" suffix"
echo " --shadow-suffix NAME (e.g., --shadow-suffix=test -> ~/.emacs.d-test)"
echo ""
echo " --scrub-shadow-suffix=NAME Remove all symlinks with the \"-NAME\" suffix"
echo " --scrub-shadow-suffix NAME"
echo ""
echo " --scrub-shadow-suffix=\"\" Remove default symlinks (no suffix)"
echo ""
echo "The script detects and replaces broken symlinks, backs up existing"
echo "dotfiles with a \"-pre-skewed-emacs\" suffix, and skips tilde backup files."
echo ""
echo "Examples:"
echo " ./setup --dry-run"
echo " ./setup"
echo " ./setup --shadow-suffix=shadow"
echo " ./setup --scrub-shadow-suffix=test"
echo " ./setup --scrub-shadow-suffix=\"\" --shadow-suffix=work"
echo " ./setup --force"
echo ""
echo "Warning: Existing ~/.emacs.d and dotfiles will be backed up, not deleted."
echo "Run with --dry-run first to preview."
}
# =============================================================================
# ARGUMENT PARSING
# =============================================================================
shadow_suffix=""
shadow_suffix_set=false # distinguish "not passed" from "passed as empty"
dry_run=false
force=false
with_mcp=false
scrub_suffix=""
scrub_suffix_set=false
# Check for help first
for _arg in "$@"; do
case "$_arg" in
--help|-help|-h) show_help; exit 0 ;;
esac
done
# Full parse — two-pass: handle both "--opt=val" and "--opt val" forms
while [ $# -gt 0 ]; do
_arg="$1"
case "$_arg" in
--shadow-suffix=*|-shadow-suffix=*)
_val="${_arg#*=}"
shadow_suffix_set=true
[ -n "$_val" ] && shadow_suffix="-${_val}" || shadow_suffix=""
;;
--shadow-suffix|-shadow-suffix)
shadow_suffix_set=true
shift
[ -n "$1" ] && shadow_suffix="-${1}" || shadow_suffix=""
;;
--scrub-shadow-suffix=*|-scrub-shadow-suffix=*)
_val="${_arg#*=}"
scrub_suffix_set=true
[ -n "$_val" ] && scrub_suffix="-${_val}" || scrub_suffix=""
;;
--scrub-shadow-suffix|-scrub-shadow-suffix)
scrub_suffix_set=true
shift
[ -n "$1" ] && scrub_suffix="-${1}" || scrub_suffix=""
;;
--with-mcp|-with-mcp) with_mcp=true ;;
--dry-run|-dry-run) dry_run=true ;;
--force|-force|-f) force=true ;;
esac
shift
done
# =============================================================================
# REPO / DOT-FILES LOCATION
# =============================================================================
SCRIPT_PATH="$(realpath "$0")"
REPO_DIR="$(dirname "$SCRIPT_PATH")"
dot_files="$REPO_DIR/dot-files/"
echo ""
echo "Skewed Emacs Config Setup"
echo "========================="
echo "Sets up your home directory with symlinks to dot-files in this repo."
echo ""
echo "Using repository at: $REPO_DIR"
if [ "$dry_run" = "true" ]; then
echo "Running in DRY RUN mode - no changes will be made"
fi
if [ ! -d "$dot_files" ]; then
echo >&2 "ERROR: Could not find dot-files directory at $dot_files"
echo >&2 "Please run this script from the root of the skewed-emacs repository."
exit 1
fi
# =============================================================================
# PREREQUISITES CHECK
# =============================================================================
for _prereq in emacs git; do
if ! command -v "$_prereq" > /dev/null 2>&1; then
echo >&2 "WARNING: '$_prereq' not found. Your environment may not function correctly."
fi
done
if command -v node > /dev/null 2>&1; then
_node_ver="$(node --version | sed 's/v//' | cut -d. -f1)"
if [ "$_node_ver" -lt 22 ] 2>/dev/null; then
echo >&2 "WARNING: Node.js $_node_ver detected; version 22+ recommended for AI integrations."
fi
else
echo >&2 "WARNING: Node.js not found; version 22+ required for AI integrations."
fi
if command -v emacs > /dev/null 2>&1; then
_emacs_ver="$(emacs --version 2>/dev/null | sed -n '1p' | awk '{print $3}' | cut -d. -f1)"
if [ "$_emacs_ver" -lt 29 ] 2>/dev/null; then
echo >&2 "WARNING: Emacs $_emacs_ver detected; version 29+ recommended."
fi
fi
echo ""
# =============================================================================
# SCRUB MODE
# =============================================================================
if [ "$scrub_suffix_set" = "true" ]; then
echo "Running in SCRUB MODE - removing symlinks with suffix '${scrub_suffix}'"
_found_files=false
if [ -z "$scrub_suffix" ]; then
# Empty suffix: only remove symlinks that point into our dot-files dir
for _nf in "${dot_files}"*; do
_nf="$(basename "$_nf")"
case "$_nf" in *~) continue ;; esac
_dp="$HOME/.${_nf}"
if [ -h "$_dp" ]; then
_tgt="$(readlink "$_dp")"
case "$_tgt" in
*"$REPO_DIR/dot-files/"*)
_found_files=true
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would remove $_dp"
else
echo "Removing $_dp"
rm "$_dp"
fi
;;
esac
fi
done
else
# Non-empty suffix: iterate files in dot-files dir, build suffixed target names
for _nf in "${dot_files}"*; do
_nf="$(basename "$_nf")"
case "$_nf" in *~) continue ;; esac
_dp="$HOME/.${_nf}${scrub_suffix}"
if [ -h "$_dp" ]; then
_found_files=true
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would remove $_dp"
else
echo "Removing $_dp"
rm "$_dp"
fi
else
[ -e "$_dp" ] && echo "Skipping $_dp (not a symlink)"
fi
done
fi
if [ "$_found_files" = "false" ]; then
echo "No dotfiles with suffix '${scrub_suffix}' found."
fi
if [ "$dry_run" = "true" ]; then
echo ""
echo "DRY RUN scrubbing complete. No changes were made."
echo "Run without --dry-run to apply changes."
else
echo ""
echo "Scrubbing complete."
fi
echo ""
# If no shadow install also requested, we're done
[ "$shadow_suffix_set" = "false" ] && exit 0
echo "Continuing with installation..."
fi
# Report mode
if [ -n "$shadow_suffix" ]; then
echo "Running in SHADOW MODE - symlinks will use suffix '${shadow_suffix}'"
echo "e.g. ~/.emacs.d${shadow_suffix} instead of ~/.emacs.d"
fi
# Safety note for same-suffix scrub+install
same_suffix_op=false
if [ "$scrub_suffix_set" = "true" ] && [ "$shadow_suffix_set" = "true" ] && \
[ "$scrub_suffix" = "$shadow_suffix" ] && [ -n "$scrub_suffix" ]; then
same_suffix_op=true
echo "Note: scrub and install use the same suffix '${scrub_suffix}' — old links removed before new ones created."
fi
# =============================================================================
# MAIN INSTALL LOOP
# =============================================================================
for _nf in "${dot_files}"*; do
_nf="$(basename "$_nf")"
# Skip tilde backup files
case "$_nf" in
*~) echo "Skipping backup file $_nf"; continue ;;
esac
echo "Processing $_nf"
_target_file=".${_nf}${shadow_suffix}"
_target_path="$HOME/${_target_file}"
_source_path="${dot_files}${_nf}"
# Resolve both paths for comparison
_source_real="$(realpath "$_source_path" 2>/dev/null)"
# --- Already correctly linked? ---
if [ -h "$_target_path" ]; then
_existing_real="$(realpath "$(readlink "$_target_path")" 2>/dev/null)"
if [ "$_existing_real" = "$_source_real" ]; then
if [ "$same_suffix_op" = "true" ]; then
# scrub already removed it; should not reach here, but handle gracefully
:
elif [ "$force" = "true" ]; then
_bak="${_target_file}-pre-skewed-emacs-$(date +%Y%m%d-%H%M%S)"
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would rename ~/${_target_file} -> ~/${_bak} and relink"
else
echo "Force: renaming ~/${_target_file} -> ~/${_bak}"
mv "$_target_path" "$HOME/${_bak}"
ln -s "$_source_path" "$_target_path"
echo "Created symlink ~/${_target_file} -> $_source_path"
fi
else
echo "~/${_target_file} already linked correctly — skipping."
fi
echo ""
continue
fi
fi
# --- Handle existing file / wrong-target symlink ---
if [ -e "$_target_path" ] || [ -h "$_target_path" ]; then
if [ "$force" = "true" ]; then
_bak="${_target_file}-pre-skewed-emacs-$(date +%Y%m%d-%H%M%S)"
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would move ~/${_target_file} -> ~/${_bak} and relink"
else
echo "Force: moving ~/${_target_file} -> ~/${_bak}"
mv "$_target_path" "$HOME/${_bak}"
fi
elif [ -h "$_target_path" ]; then
# Wrong-target or broken symlink — back it up
_old_tgt="$(readlink "$_target_path")"
_status="wrong target"
[ ! -e "$_old_tgt" ] && _status="broken"
_bak="${_target_file}-pre-skewed-emacs"
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would move ~/${_target_file} (${_status}, -> ${_old_tgt}) -> ~/${_bak}"
else
echo "Moving ~/${_target_file} (${_status}) -> ~/${_bak}"
rm -f "$HOME/${_bak}"
mv "$_target_path" "$HOME/${_bak}"
fi
else
# Regular file/dir — don't touch without --force
echo "~/${_target_file} exists and is not a symlink. Use --force to replace."
echo ""
continue
fi
fi
# --- Create the symlink ---
if [ ! -e "$_target_path" ]; then
if [ "$dry_run" = "true" ]; then
echo "[DRY RUN] Would create symlink ~/${_target_file} -> $_source_path"
else
ln -s "$_source_path" "$_target_path"
echo "Created symlink ~/${_target_file} -> $_source_path"
fi
fi
echo ""
done
# =============================================================================
# OPTIONAL: enable Lisply MCP server in the HOST Emacs (--with-mcp)
# =============================================================================
if [ "$with_mcp" = "true" ]; then
_emacs_local="$HOME/.emacs-local"
echo ""
echo "============================ SECURITY WARNING ============================"
echo "Enabling the Lisply MCP server in your HOST Emacs opens an endpoint that"
echo "evaluates arbitrary Emacs Lisp sent by any MCP client that can reach it."
echo "Because Emacs Lisp can read/write files, run shell commands, and drive your"
echo "desktop, this effectively grants an LLM FULL ACCESS TO THIS MACHINE with your"
echo "privileges. The container path (./compose-dev up) sandboxes this; the host"
echo "path does NOT. It binds to loopback (127.0.0.1:7080) by default."
echo "See docs/HOST_EMACS_MCP.md."
echo "========================================================================="
if [ "$dry_run" = "true" ]; then
echo "[dry-run] Would append the host Lisply opt-in to $_emacs_local, and clone/build"
echo " the branch-matched lisply-mcp middleware, after confirmation."
elif grep -q 'lisply-host-server-enable' "$_emacs_local" 2>/dev/null; then
echo "Host Lisply opt-in already present in $_emacs_local - nothing to do."
else
printf 'Type YES to enable the host Lisply MCP server: '
read _confirm
if [ "$_confirm" = "YES" ]; then
{
echo ""
echo ';; --- Added by skewed-emacs `./setup --with-mcp` ---'
echo ';; Auto-start the Lisply MCP server (arbitrary elisp eval) in this host Emacs.'
echo ';; Remove these two lines to disable. See docs/HOST_EMACS_MCP.md.'
echo '(setq lisply-host-server-enable t'
echo ' lisply-host-server-risk-acknowledged t)'
} >> "$_emacs_local"
echo "Enabled. Wrote opt-in to $_emacs_local (restart Emacs to take effect)."
# --- Provision the lisply-mcp node middleware, branch-matched to this
# skewed-emacs checkout (mirrors docker/Dockerfile). The middleware
# is a SEPARATE repo, so the host path needs its own copy. ---
_mcp_dir="$(dirname "$REPO_DIR")/lisply-mcp"
_branch="$(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null || echo master)"
echo ""
echo "Provisioning lisply-mcp middleware (branch: $_branch) at $_mcp_dir ..."
if ! command -v git > /dev/null 2>&1; then
echo "WARNING: git not found; skipping clone. See docs/HOST_EMACS_MCP.md."
elif [ -d "$_mcp_dir/.git" ]; then
echo " Already present; checking out $_branch ..."
if git -C "$_mcp_dir" fetch --depth 1 origin "$_branch" > /dev/null 2>&1; then
git -C "$_mcp_dir" checkout "$_branch" > /dev/null 2>&1 || true
git -C "$_mcp_dir" reset --hard "origin/$_branch" > /dev/null 2>&1 || true
else
echo " (branch $_branch unavailable; leaving current checkout in place)"
fi
elif ! git clone --depth 1 --branch "$_branch" https://github.com/genworks/lisply-mcp.git "$_mcp_dir" > /dev/null 2>&1; then
echo " Branch $_branch not found in lisply-mcp; falling back to master."
git clone --depth 1 --branch master https://github.com/genworks/lisply-mcp.git "$_mcp_dir" > /dev/null 2>&1 || echo "WARNING: failed to clone lisply-mcp; see docs/HOST_EMACS_MCP.md."
fi
if [ -d "$_mcp_dir/scripts" ]; then
if command -v npm > /dev/null 2>&1; then
echo " Installing node dependencies (npm ci --omit=dev) ..."
( cd "$_mcp_dir/scripts" && npm ci --omit=dev ) || echo "WARNING: npm ci failed in $_mcp_dir/scripts; run it yourself."
else
echo "WARNING: npm not found; run: cd $_mcp_dir/scripts && npm ci --omit=dev"
fi
fi
echo ""
echo "Connect an MCP client to the running host server with:"
echo " node $_mcp_dir/scripts/mcp-wrapper.js \\"
echo " --server-name emacs-host \\"
echo " --backend-host 127.0.0.1 --http-host-port 7080"
else
echo "Not enabled (confirmation not received)."
fi
fi
fi
# =============================================================================
# SUMMARY
# =============================================================================
if [ "$dry_run" = "true" ]; then
echo ""
echo "DRY RUN complete. No changes were made."
echo "Run without --dry-run to apply changes."
else
echo ""
echo "Setup complete! Configuration files linked from $REPO_DIR"
echo ""
echo "Next steps:"
if [ -n "$shadow_suffix" ]; then
echo " Shadow installation mode:"
echo " emacs -q --load \"~/.emacs.d${shadow_suffix}/init.el\""
echo " . ~/.bash_profile${shadow_suffix}"
else
echo " Standard installation mode:"
echo " killall -9 emacs emacs-gtk # restart any running daemon"
echo " . ~/.bash_profile # or open a new terminal"
fi
fi