-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-linux.sh
More file actions
executable file
·239 lines (214 loc) · 5.7 KB
/
Copy pathsetup-linux.sh
File metadata and controls
executable file
·239 lines (214 loc) · 5.7 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage:
./setup-linux.sh [--agent prompt|codex|claude|pi|all|none]
This script writes agent rule files and installs user-local Linux helpers. It does not install system packages or modify shell profiles.
USAGE
}
agent="prompt"
while (($#)); do
case "$1" in
--agent)
agent="${2:-}"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done
case "$agent" in
prompt|codex|claude|pi|all|none) ;;
*)
echo "--agent must be one of: prompt, codex, claude, pi, all, none" >&2
exit 2
;;
esac
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
install_linux_helpers() {
local bin_dir="$HOME/.local/bin"
local clip_run="$bin_dir/clip-run"
mkdir -p -- "$bin_dir"
backup_file "$clip_run"
install -m 0755 "$script_dir/scripts/clip-run" "$clip_run"
echo "Installed $clip_run"
}
is_arch_like() {
[[ -r /etc/os-release ]] || return 1
local id="" id_like=""
# shellcheck disable=SC1091
source /etc/os-release
id="${ID:-}"
id_like="${ID_LIKE:-}"
[[ " $id $id_like " == *" arch "* ]]
}
linux_rule_files() {
local files=(AGENTS.shared.md AGENTS.linux.md)
if is_arch_like; then
files+=(AGENTS.linux-arch.md)
fi
printf '%s\n' "${files[@]}"
}
join_rules() {
local active_tags="$1"
shift
local first=1
for file in "$@"; do
if [[ ! -f "$script_dir/rules/$file" ]]; then
echo "Missing rule file: rules/$file" >&2
exit 1
fi
if [[ "$first" -eq 0 ]]; then
printf '\n\n'
fi
filter_rule_file "$active_tags" "$script_dir/rules/$file" | sed -e '${/^$/d;}'
first=0
done
printf '\n'
}
filter_rule_file() {
local active_tags="$1"
local path="$2"
awk -v active_csv="$active_tags" '
BEGIN {
split(active_csv, tags, ",")
for (i in tags) {
if (tags[i] != "") active[tags[i]] = 1
}
in_block = 0
keep_block = 1
}
function is_condition_marker(line) {
return line ~ /^[[:space:]]*<!--[[:space:]]*(:[[:alnum:]-]+-only[[:space:]]*)+-->[[:space:]]*$/
}
function is_end_marker(line) {
return line ~ /^[[:space:]]*<!--[[:space:]]*:end[[:space:]]*-->[[:space:]]*$/
}
{
if (is_condition_marker($0)) {
condition = $0
sub(/^[[:space:]]*<!--[[:space:]]*/, "", condition)
sub(/[[:space:]]*-->[[:space:]]*$/, "", condition)
count = split(condition, parts, /[[:space:]]+/)
keep_block = 0
for (i = 1; i <= count; i++) {
tag = parts[i]
sub(/^:/, "", tag)
sub(/-only$/, "", tag)
if (tag in active) keep_block = 1
}
in_block = 1
next
}
if (is_end_marker($0)) {
in_block = 0
keep_block = 1
next
}
if (!in_block || keep_block) print
}
' "$path"
}
active_tags_for() {
local agent_name="$1"
local tags="linux,$agent_name"
if is_arch_like; then
tags="$tags,arch"
fi
printf '%s\n' "$tags"
}
backup_file() {
local path="$1"
if [[ -e "$path" ]]; then
local backup="${path}.bak-$(date +%Y%m%d%H%M%S)"
cp -a -- "$path" "$backup"
echo "Backed up $path -> $backup"
fi
}
write_codex() {
local dir="$HOME/.codex"
local path="$dir/AGENTS.md"
mkdir -p -- "$dir"
backup_file "$path"
mapfile -t base_rules < <(linux_rule_files)
join_rules "$(active_tags_for codex)" "${base_rules[@]}" AGENTS.codex.md AGENTS.linux-initial-setup.md > "$path"
echo "Wrote $path"
}
write_claude() {
local dir="$HOME/.claude"
local path="$dir/CLAUDE.md"
mkdir -p -- "$dir"
backup_file "$path"
mapfile -t base_rules < <(linux_rule_files)
join_rules "$(active_tags_for claude)" "${base_rules[@]}" CLAUDE.md AGENTS.linux-initial-setup.md > "$path"
echo "Wrote $path"
}
write_pi() {
local dir="$HOME/.pi/agent"
local path="$dir/AGENTS.md"
mkdir -p -- "$dir"
backup_file "$path"
mapfile -t base_rules < <(linux_rule_files)
join_rules "$(active_tags_for pi)" "${base_rules[@]}" AGENTS.pi.md AGENTS.linux-initial-setup.md > "$path"
echo "Wrote $path"
local ext_source="$script_dir/configs/pi/extensions/pi-footer.ts"
if [ -f "$ext_source" ]; then
local ext_dir="$dir/extensions"
mkdir -p -- "$ext_dir"
local ext_target="$ext_dir/pi-footer.ts"
backup_file "$ext_target"
cp -- "$ext_source" "$ext_target"
echo "Wrote $ext_target"
fi
}
select_agent() {
if [[ "$agent" != "prompt" ]]; then
printf '%s\n' "$agent"
return
fi
local tty_fd
if ! { exec {tty_fd}<>/dev/tty; } 2>/dev/null; then
printf '%s\n' "No interactive terminal available; writing no agent files. Pass --agent codex|claude|pi|all to choose explicitly." >&2
printf '%s\n' "none"
return
fi
printf '%s\n' 'Which coding agent should be configured?' >&"$tty_fd"
printf '%s\n' ' 1. Codex' >&"$tty_fd"
printf '%s\n' ' 2. Claude Code' >&"$tty_fd"
printf '%s\n' ' 3. Pi' >&"$tty_fd"
printf '%s\n' ' 4. All (Codex + Claude + Pi)' >&"$tty_fd"
printf '%s\n' ' 5. Write no agent files' >&"$tty_fd"
printf '%s' 'Select [1-5] (default: 3): ' >&"$tty_fd"
local answer
IFS= read -r answer <&"$tty_fd" || answer=""
exec {tty_fd}<&-
case "$answer" in
1) printf '%s\n' 'codex' ;;
2) printf '%s\n' 'claude' ;;
3) printf '%s\n' 'pi' ;;
4) printf '%s\n' 'all' ;;
5) printf '%s\n' 'none' ;;
*) printf '%s\n' 'pi' ;;
esac
}
target_agent="$(select_agent)"
case "$agent" in
prompt) ;;
*) target_agent="$agent" ;;
esac
case "$target_agent" in
codex) write_codex ;;
claude) write_claude ;;
pi) write_pi ;;
all) write_codex; write_claude; write_pi ;;
none) echo "No agent rule files written." ;;
esac
install_linux_helpers