forked from helicopterrun/claude-code-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·264 lines (243 loc) · 9.94 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·264 lines (243 loc) · 9.94 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
#!/usr/bin/env bash
# setup.sh — set up tmux session picker + Claude Code statusline on this machine.
#
# Usage: git clone <repo> && cd <repo> && ./setup.sh
#
# Idempotent and conservative: it shows what it changes, skips work already done,
# and never disables SSH password auth (that's left to you, on purpose).
set -u
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$HERE/scripts"
c_bold=$'\033[1m'; c_dim=$'\033[2m'; c_grn=$'\033[32m'; c_ylw=$'\033[33m'; c_red=$'\033[31m'; c_rst=$'\033[0m'
say() { printf '%s\n' "${c_bold}==>${c_rst} $*"; }
note() { printf '%s\n' " ${c_dim}$*${c_rst}"; }
ok() { printf '%s\n' " ${c_grn}✓${c_rst} $*"; }
warn() { printf '%s\n' " ${c_ylw}!${c_rst} $*"; }
ask() { # ask "prompt" "default" -> echoes answer
local q="$1" def="${2:-}" a
if [ -n "$def" ]; then printf '%s [%s]: ' "$q" "$def" >&2; else printf '%s: ' "$q" >&2; fi
read -r a || a=""
[ -z "$a" ] && a="$def"
printf '%s' "$a"
}
confirm() { # confirm "prompt" -> returns 0 if yes
local a; a="$(ask "$1 (y/N)" "")"; case "$a" in y|Y|yes|YES) return 0;; *) return 1;; esac
}
write_codex_status() {
local config="$1" tmp
local value='status_line = ["current-dir", "git-branch", "model-with-reasoning", "context-remaining"]'
mkdir -p "$(dirname "$config")"
[ -f "$config" ] || : > "$config"
tmp="$(mktemp)"
awk -v value="$value" '
BEGIN { in_tui=0; saw_tui=0; wrote=0 }
/^[[:space:]]*\[[^]]+\][[:space:]]*$/ {
if (in_tui && !wrote) { print value; wrote=1 }
in_tui = ($0 ~ /^[[:space:]]*\[tui\][[:space:]]*$/)
if (in_tui) saw_tui=1
}
in_tui && /^[[:space:]]*status_line[[:space:]]*=/ {
if (!wrote) { print value; wrote=1 }
next
}
{ print }
END {
if (in_tui && !wrote) print value
if (!saw_tui) {
print ""
print "[tui]"
print value
}
}
' "$config" > "$tmp" && mv "$tmp" "$config"
}
if [ "${1:-}" = "--write-codex-status" ]; then
[ -n "${2:-}" ] || { echo "usage: $0 --write-codex-status CONFIG"; exit 2; }
write_codex_status "$2"
exit 0
fi
mode="auto"
case "${1:-}" in
"") ;;
--codex) mode="codex" ;;
--claude) mode="claude" ;;
--both) mode="both" ;;
-h|--help)
echo "usage: $0 [--codex|--claude|--both]"
exit 0
;;
*) echo "unknown option: $1" >&2; exit 2 ;;
esac
[ -d "$SCRIPTS" ] || { printf '%s\n' "${c_red}error:${c_rst} run this from the cloned repo (scripts/ not found next to setup.sh)"; exit 1; }
printf '\n%s\n\n' "${c_bold}AI coding agent machine setup${c_rst}"
note "Files this can create/modify:"
note " ~/.tmux-login.sh, ~/.profile, ~/.tmux.conf, ~/.codex/config.toml,"
note " ~/.claude/statusline.sh, ~/.claude/settings.json, ~/.ssh/authorized_keys"
echo
if [ "$mode" = "auto" ]; then
if command -v codex >/dev/null 2>&1 && command -v claude >/dev/null 2>&1; then mode="both"
elif command -v codex >/dev/null 2>&1; then mode="codex"
elif command -v claude >/dev/null 2>&1; then mode="claude"
else mode="codex"
fi
fi
want_codex=0; want_claude=0
case "$mode" in
codex) want_codex=1 ;;
claude) want_claude=1 ;;
both) want_codex=1; want_claude=1 ;;
esac
note "agent mode: $mode"
echo
# --- Part 0: prerequisites -------------------------------------------------
say "Checking prerequisites (tmux, jq, git)"
PKG=""
if command -v brew >/dev/null 2>&1; then PKG="brew install"
elif command -v apt-get >/dev/null 2>&1; then PKG="sudo apt-get install -y"
elif command -v dnf >/dev/null 2>&1; then PKG="sudo dnf install -y"
elif command -v pacman >/dev/null 2>&1; then PKG="sudo pacman -S --noconfirm"
elif command -v apk >/dev/null 2>&1; then PKG="sudo apk add"
elif command -v zypper >/dev/null 2>&1; then PKG="sudo zypper install -y"
fi
# Drop sudo if we're already root.
[ "$(id -u)" = "0" ] && PKG="${PKG#sudo }"
missing=""
for t in tmux jq git; do command -v "$t" >/dev/null 2>&1 || missing="$missing $t"; done
if [ -n "$missing" ]; then
warn "missing:$missing"
if [ -n "$PKG" ] && confirm "Install with: $PKG$missing ?"; then
# shellcheck disable=SC2086
$PKG $missing && ok "installed$missing" || warn "install failed — install$missing manually and re-run"
else
warn "skipping install — these are required for the statusline/picker to work"
fi
else
ok "tmux, jq, git all present"
fi
echo
# --- Part 1: SSH key login (optional) --------------------------------------
say "SSH key login"
note "On YOUR laptop (the client), to use a 1Password-managed key:"
note " 1) 1Password → Settings → Developer → enable 'Use the SSH agent'"
note " 2) Create/import an SSH key item (Ed25519 is the default — good)"
note " 3) In ~/.ssh/config on your laptop: Host * -> IdentityAgent ~/.1password/agent.sock"
note " (macOS socket: '~/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock')"
note " 4) Copy the PUBLIC key (the 'ssh-ed25519 AAAA…' string) and paste it below."
echo
if confirm "Add a public key to ~/.ssh/authorized_keys on this machine now?"; then
pubkey="$(ask "Paste public key" "")"
if [ -n "$pubkey" ]; then
mkdir -p "$HOME/.ssh"; chmod 700 "$HOME/.ssh"
touch "$HOME/.ssh/authorized_keys"; chmod 600 "$HOME/.ssh/authorized_keys"
if grep -qF "$pubkey" "$HOME/.ssh/authorized_keys" 2>/dev/null; then
ok "key already present — nothing to do"
else
printf '%s\n' "$pubkey" >> "$HOME/.ssh/authorized_keys"
ok "key added to ~/.ssh/authorized_keys"
fi
warn "TEST IT from a new terminal before closing this session."
warn "Disabling password auth is intentionally NOT done here — do it manually once key login works."
else
note "no key entered — skipping"
fi
else
note "skipping SSH key step"
fi
echo
# --- Part 2: tmux session picker -------------------------------------------
say "tmux session picker (~/.tmux-login.sh)"
note "Define your projects. Each line: 'key name directory'"
note " key = menu letter, name = tmux session name (no '.' or ':'), directory = start dir"
note "Enter one per line; blank line to finish. Press enter immediately to use the default (root only)."
proj_lines=""
while :; do
line="$(ask " project (key name dir)" "")"
[ -z "$line" ] && break
proj_lines="${proj_lines}${line}"$'\n'
done
if [ -z "$proj_lines" ]; then
proj_block='projects="r root $HOME"' # single-quoted: $HOME stays literal, expands at login
note "using default: root only"
else
proj_block="projects=\"$(printf '%s' "$proj_lines" | sed '/^$/d')\""
fi
if [ -f "$HOME/.tmux-login.sh" ] && ! confirm "~/.tmux-login.sh exists — overwrite?"; then
note "keeping existing ~/.tmux-login.sh"
else
# Copy the canonical script, swapping in the chosen projects= assignment.
awk -v repl="$proj_block" '/^projects="/{print repl; skip=1; next} skip&&/^$/{skip=0} !skip{print}' \
"$SCRIPTS/tmux-login.sh" > "$HOME/.tmux-login.sh"
ok "wrote ~/.tmux-login.sh"
fi
# Wire into the login profile.
prof="$HOME/.profile"
[ -f "$HOME/.bash_profile" ] && prof="$HOME/.bash_profile"
if grep -q 'tmux-login.sh' "$prof" 2>/dev/null; then
ok "login hook already present in $prof"
else
{ echo ""; cat "$SCRIPTS/profile-snippet.sh"; } >> "$prof"
ok "appended login hook to $prof"
fi
note "If your shell doesn't source $prof on login, add '. $prof' to the file it does source."
echo
# --- Part 2b: tmux copy/scrollback/history config --------------------------
say "tmux config (~/.tmux.conf — mouse, scrollback, history)"
tmuxconf="$HOME/.tmux.conf"
if [ ! -f "$tmuxconf" ]; then
install -m 0644 "$SCRIPTS/tmux.conf" "$tmuxconf"
ok "wrote ~/.tmux.conf"
else
# Append only the settings that aren't already configured (idempotent).
added=0
grep -q '^[[:space:]]*set\(w\)\?[[:space:]].*mouse' "$tmuxconf" || { echo 'set -g mouse on' >> "$tmuxconf"; added=1; }
grep -q '^[[:space:]]*set\(w\)\?[[:space:]].*history-limit' "$tmuxconf" || { echo 'set -g history-limit 50000' >> "$tmuxconf"; added=1; }
if [ "$added" = 1 ]; then ok "appended missing tmux settings to existing ~/.tmux.conf"; else ok "~/.tmux.conf already has mouse + history-limit"; fi
fi
note "Reload in a running tmux with: tmux source-file ~/.tmux.conf"
echo
# --- Part 3: agent status information --------------------------------------
if [ "$want_codex" = 1 ]; then
say "Codex CLI"
if command -v codex >/dev/null 2>&1; then
ok "found $(codex --version 2>/dev/null || echo codex)"
else
warn "Codex is not installed."
note "Official installer: curl -fsSL https://chatgpt.com/codex/install.sh | sh"
note "Install it, then re-run this setup. It is not run automatically."
fi
codex_config="${CODEX_HOME:-$HOME/.codex}/config.toml"
if write_codex_status "$codex_config"; then
ok "configured Codex native footer in $codex_config"
note "footer: directory · git branch · model/reasoning · context remaining"
else
warn "couldn't update $codex_config"
fi
note "Run 'codex login' if needed, then start Codex in a project directory."
note "Useful commands: /status · /model · /permissions · /review · /exit"
echo
fi
if [ "$want_claude" = 1 ]; then
say "Claude Code statusline (~/.claude/statusline.sh)"
mkdir -p "$HOME/.claude"
install -m 0755 "$SCRIPTS/statusline.sh" "$HOME/.claude/statusline.sh"
ok "installed ~/.claude/statusline.sh"
settings="$HOME/.claude/settings.json"
[ -f "$settings" ] || echo '{}' > "$settings"
if command -v jq >/dev/null 2>&1; then
tmp="$(mktemp)"
if jq '.statusLine = {type:"command", command:"~/.claude/statusline.sh", padding:0}' "$settings" > "$tmp" 2>/dev/null; then
mv "$tmp" "$settings"; ok "registered statusLine in settings.json"
else
rm -f "$tmp"; warn "couldn't parse $settings — add the statusLine block manually (see README)"
fi
else
warn "jq missing — add the statusLine block to $settings manually (see README)"
fi
echo
fi
# --- Done ------------------------------------------------------------------
say "Done"
ok "Open a NEW SSH session to see the tmux picker (keep this one open as a safety net)."
[ "$want_codex" = 1 ] && ok "The configured footer appears in the next Codex session."
[ "$want_claude" = 1 ] && ok "The statusline appears at the bottom of Claude Code on its next render."
note "Add projects later by editing the projects=\"...\" block in ~/.tmux-login.sh."