forked from can1357/c2switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·517 lines (460 loc) · 17.1 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·517 lines (460 loc) · 17.1 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env bash
# setup.sh - Unified installer for c2switcher and Cinnamon applet
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/incompletebiped/c2switcher-mint/master/setup.sh | bash
# curl -fsSL ... | bash -s -- --all
# ./setup.sh # Interactive mode (asks what to install)
# ./setup.sh --all # Install everything (CLI + applet + status line)
# ./setup.sh --cli # Install CLI only
# ./setup.sh --applet # Install Cinnamon applet only (requires CLI)
# ./setup.sh --statusline # Install Claude Code status line only (requires CLI)
# ./setup.sh --uninstall # Uninstall everything
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
REPO_URL="https://github.com/incompletebiped/c2switcher-mint.git"
# Determine source directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd)" || SCRIPT_DIR=""
CLONED_DIR=""
if [[ -z "$SCRIPT_DIR" || ! -f "$SCRIPT_DIR/pyproject.toml" ]]; then
# Running via curl pipe or from wrong directory — need to clone the repo
if ! command -v git &> /dev/null; then
echo -e "${RED}Error: git is required. Install it with: sudo apt install git${NC}"
exit 1
fi
CLONED_DIR="$(mktemp -d)"
trap 'rm -rf "$CLONED_DIR"' EXIT
echo -e "${BLUE}Cloning c2switcher-mint...${NC}"
git clone --depth 1 "$REPO_URL" "$CLONED_DIR/c2switcher-mint" 2>&1 | tail -1
SCRIPT_DIR="$CLONED_DIR/c2switcher-mint"
echo -e "${GREEN}✓ Repository cloned${NC}"
echo
fi
APPLET_SRC="$SCRIPT_DIR/applet"
APPLET_UUID="c2switcher@c2switcher-mint"
APPLET_DEST="$HOME/.local/share/cinnamon/applets/$APPLET_UUID"
# Ensure pipx is available (required for PEP 668 / externally-managed Python)
ensure_pipx() {
if command -v pipx &> /dev/null; then
return 0
fi
echo -e "${BLUE}Installing pipx (required for Python CLI tools on this system)...${NC}"
if command -v apt &> /dev/null; then
sudo apt update -qq && sudo apt install -y pipx
else
python3 -m pip install --user pipx 2>/dev/null || {
echo -e "${RED}Error: Could not install pipx. Please install it manually:${NC}"
echo " sudo apt install pipx"
exit 1
}
fi
pipx ensurepath &> /dev/null || true
# Re-check
if ! command -v pipx &> /dev/null; then
# pipx may have been installed to ~/.local/bin which isn't on PATH yet
export PATH="$HOME/.local/bin:$PATH"
fi
if ! command -v pipx &> /dev/null; then
echo -e "${RED}Error: pipx installed but not found on PATH.${NC}"
echo " Try restarting your terminal, then run this script again."
exit 1
fi
echo -e "${GREEN}✓ pipx installed${NC}"
}
# Parse arguments
INSTALL_CLI=false
INSTALL_APPLET=false
INSTALL_STATUSLINE=false
UNINSTALL=false
INTERACTIVE=true
for arg in "$@"; do
case "$arg" in
--all)
INSTALL_CLI=true
INSTALL_APPLET=true
INSTALL_STATUSLINE=true
INTERACTIVE=false
;;
--cli)
INSTALL_CLI=true
INTERACTIVE=false
;;
--applet)
INSTALL_APPLET=true
INTERACTIVE=false
;;
--statusline)
INSTALL_STATUSLINE=true
INTERACTIVE=false
;;
--uninstall)
UNINSTALL=true
INTERACTIVE=false
;;
*)
echo -e "${RED}Unknown option: $arg${NC}"
echo "Usage: $0 [--all|--cli|--applet|--statusline|--uninstall]"
exit 1
;;
esac
done
# Header
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${CYAN} C2Switcher Setup (Mint Cinnamon Edition)${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo
# Uninstall mode
if [[ "$UNINSTALL" == true ]]; then
echo -e "${YELLOW}Uninstalling c2switcher...${NC}"
echo
echo -e "${BLUE}Removing CLI package...${NC}"
if command -v pipx &> /dev/null && pipx list 2>/dev/null | grep -q c2switcher; then
pipx uninstall c2switcher
echo -e "${GREEN}✓ CLI package removed (pipx)${NC}"
elif python3 -m pip show c2switcher &> /dev/null 2>&1; then
sudo python3 -m pip uninstall -y c2switcher
echo -e "${GREEN}✓ CLI package removed (pip)${NC}"
else
echo -e "${YELLOW}c2switcher package not found${NC}"
fi
# Remove c2claude wrapper
if [[ -f "$HOME/.local/bin/c2claude" ]]; then
rm "$HOME/.local/bin/c2claude"
echo -e "${GREEN}✓ c2claude removed from ~/.local/bin${NC}"
fi
if [[ -f "/usr/local/bin/c2claude" ]]; then
sudo rm /usr/local/bin/c2claude
echo -e "${GREEN}✓ c2claude removed from /usr/local/bin${NC}"
fi
# Remove Cinnamon applet
if [[ -d "$APPLET_DEST" ]]; then
echo -e "${BLUE}Removing Cinnamon applet...${NC}"
rm -rf "$APPLET_DEST"
echo -e "${GREEN}✓ Cinnamon applet removed${NC}"
fi
# Remove status line script and settings entry
if [[ -f "$HOME/.claude/statusline-command.sh" ]]; then
rm "$HOME/.claude/statusline-command.sh"
echo -e "${GREEN}✓ Status line script removed${NC}"
fi
if [[ -f "$HOME/.claude/settings.json" ]]; then
python3 -c "
import json, os
p = os.path.expanduser('~/.claude/settings.json')
with open(p) as f:
try:
s = json.load(f)
except Exception:
s = {}
s.pop('statusLine', None)
with open(p, 'w') as f:
json.dump(s, f, indent=2)
f.write('\n')
" && echo -e "${GREEN}✓ statusLine removed from ~/.claude/settings.json${NC}"
fi
echo
echo -e "${GREEN}Uninstall complete!${NC}"
echo -e "${YELLOW}Note: Database at ~/.c2switcher.db was NOT removed${NC}"
echo " Remove it manually if desired: rm ~/.c2switcher.db"
echo
exit 0
fi
# Interactive mode — only possible when running from a local checkout (stdin is a terminal)
if [[ "$INTERACTIVE" == true ]]; then
if [[ -n "$CLONED_DIR" ]]; then
# Piped install — can't prompt, default to --all
echo -e "${YELLOW}Piped install detected, installing everything (CLI + applet)${NC}"
echo
INSTALL_CLI=true
INSTALL_APPLET=true
else
echo "What would you like to install?"
echo " 1) CLI tools only"
echo " 2) CLI tools + Cinnamon applet"
echo " 3) CLI tools + Cinnamon applet + Claude Code status line"
echo " 4) Cinnamon applet only (requires CLI already installed)"
echo " 5) Claude Code status line only (requires CLI already installed)"
echo " 6) Uninstall everything"
echo
read -p "Enter choice [1-6]: " choice
case "$choice" in
1)
INSTALL_CLI=true
;;
2)
INSTALL_CLI=true
INSTALL_APPLET=true
;;
3)
INSTALL_CLI=true
INSTALL_APPLET=true
INSTALL_STATUSLINE=true
;;
4)
INSTALL_APPLET=true
;;
5)
INSTALL_STATUSLINE=true
;;
6)
exec "$0" --uninstall
;;
*)
echo -e "${RED}Invalid choice${NC}"
exit 1
;;
esac
echo
fi
fi
# Install Claude Code status line
install_statusline() {
echo -e "${GREEN}Installing Claude Code status line...${NC}"
echo
mkdir -p "$HOME/.claude"
cat > "$HOME/.claude/statusline-command.sh" << 'STATUSLINE_EOF'
#!/usr/bin/env bash
cat > /dev/null # consume Claude Code's JSON input
# Use $'...' for actual escape bytes so printf '%s' is safe with % in paths/values
RESET=$'\033[00m'
PURPLE=$'\033[38;5;141m'
GREEN=$'\033[38;5;82m'
YELLOW=$'\033[38;5;226m'
RED=$'\033[38;5;196m'
usage_color() {
local pct="${1%%%*}"; pct="${pct%.*}"
if [ "$pct" -ge 90 ] 2>/dev/null; then printf '%s' "$RED"
elif [ "$pct" -ge 70 ] 2>/dev/null; then printf '%s' "$YELLOW"
else printf '%s' "$GREEN"
fi
}
account=""
usage_str=""
if command -v c2switcher &>/dev/null; then
account=$(c2switcher current --format=prompt 2>/dev/null)
if [ -n "$account" ]; then
idx=$(c2switcher current --json 2>/dev/null | python3 -c \
"import sys,json; print(json.load(sys.stdin).get('index',''))" 2>/dev/null)
if [ -n "$idx" ]; then
usage_json=$(c2switcher usage --json 2>/dev/null)
if [ -n "$usage_json" ]; then
read -r five_h seven_d <<< "$(echo "$usage_json" | python3 -c "
import sys, json
data = json.load(sys.stdin)
idx = int('$idx')
for acc in data:
if acc.get('index') == idx:
u = acc.get('usage', {})
fh = u.get('five_hour', {}).get('utilization')
sd = u.get('seven_day', {}).get('utilization')
print(
(str(round(fh)) + '%') if fh is not None else '',
(str(round(sd)) + '%') if sd is not None else ''
)
break
" 2>/dev/null)"
if [ -n "$five_h" ] || [ -n "$seven_d" ]; then
[ -n "$five_h" ] && usage_str+="$(usage_color "$five_h")5h:${five_h}${RESET}"
[ -n "$five_h" ] && [ -n "$seven_d" ] && usage_str+=" "
[ -n "$seven_d" ] && usage_str+="$(usage_color "$seven_d")7d:${seven_d}${RESET}"
fi
fi
fi
fi
fi
if [ -n "$account" ]; then
line="${PURPLE}${account}${RESET}"
[ -n "$usage_str" ] && line+=" ${usage_str}"
printf '%s' "$line"
fi
STATUSLINE_EOF
chmod +x "$HOME/.claude/statusline-command.sh"
echo -e "${GREEN}✓ Status line script written to ~/.claude/statusline-command.sh${NC}"
# Merge statusLine key into ~/.claude/settings.json (preserves existing keys)
python3 -c "
import json, os, sys
p = os.path.expanduser('~/.claude/settings.json')
s = {}
if os.path.exists(p):
with open(p) as f:
try:
s = json.load(f)
except Exception:
pass
s['statusLine'] = {'type': 'command', 'command': sys.argv[1]}
with open(p, 'w') as f:
json.dump(s, f, indent=2)
f.write('\n')
" "bash $HOME/.claude/statusline-command.sh"
echo -e "${GREEN}✓ ~/.claude/settings.json updated with statusLine config${NC}"
echo
echo -e "${YELLOW}Status line will show:${NC}"
echo " /your/working/dir [0] user@example.com"
echo
}
# Install CLI
if [[ "$INSTALL_CLI" == true ]]; then
echo -e "${GREEN}Installing CLI tools...${NC}"
echo
ensure_pipx
if [[ -n "$CLONED_DIR" ]]; then
# Remote install: standard install (temp dir cleaned up after)
echo -e "${BLUE}Installing c2switcher package...${NC}"
pipx install "$SCRIPT_DIR" --force
echo -e "${GREEN}✓ c2switcher installed${NC}"
else
# Local checkout: editable install so source changes apply immediately
echo -e "${BLUE}Installing c2switcher package (editable)...${NC}"
pipx install --editable "$SCRIPT_DIR" --force
echo -e "${GREEN}✓ c2switcher installed (editable - source changes apply immediately)${NC}"
fi
echo
# Install c2claude wrapper
echo -e "${BLUE}Installing c2claude wrapper...${NC}"
mkdir -p "$HOME/.local/bin"
if [[ -n "$CLONED_DIR" ]]; then
# Remote install: copy the wrapper (source dir is temporary)
cp -f "$SCRIPT_DIR/c2claude" "$HOME/.local/bin/c2claude"
chmod +x "$HOME/.local/bin/c2claude"
echo -e "${GREEN}✓ c2claude copied to ~/.local/bin/c2claude${NC}"
else
# Local checkout: symlink so updates propagate
ln -sf "$SCRIPT_DIR/c2claude" "$HOME/.local/bin/c2claude"
echo -e "${GREEN}✓ c2claude symlinked to ~/.local/bin/c2claude${NC}"
fi
echo
# Ensure ~/.local/bin is on PATH
export PATH="$HOME/.local/bin:$PATH"
# Verify command visibility
if ! command -v c2switcher &> /dev/null; then
echo -e "${YELLOW}Warning: 'c2switcher' is not currently on your PATH${NC}"
echo "You may need to add ~/.local/bin to your PATH:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.bashrc"
echo
else
echo -e "${GREEN}✓ c2switcher command available at $(command -v c2switcher)${NC}"
fi
if ! command -v c2claude &> /dev/null; then
echo -e "${YELLOW}Warning: 'c2claude' is not currently on your PATH${NC}"
echo "You may need to add ~/.local/bin to your PATH:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.bashrc"
echo
else
echo -e "${GREEN}✓ c2claude command available at $(command -v c2claude)${NC}"
fi
# Auto-import existing Claude credentials if present
if [[ -f "$HOME/.claude/.credentials.json" ]]; then
echo
echo -e "${BLUE}Found existing Claude credentials, importing...${NC}"
if c2switcher add 2>/dev/null; then
echo -e "${GREEN}✓ Existing Claude account imported${NC}"
else
echo -e "${YELLOW}Could not auto-import (account may already be added)${NC}"
fi
fi
fi
# Install Cinnamon applet
if [[ "$INSTALL_APPLET" == true ]]; then
echo -e "${GREEN}Installing Cinnamon applet...${NC}"
echo
# Check if c2switcher is installed
if ! command -v c2switcher &> /dev/null; then
echo -e "${YELLOW}⚠ Warning: c2switcher command not found${NC}"
echo " The applet requires c2switcher to be installed first."
if [[ -z "$CLONED_DIR" ]]; then
echo
read -p "Continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
else
echo " Continuing anyway (install CLI first if needed)."
echo
fi
fi
# Check that we're running Cinnamon
if ! command -v cinnamon &> /dev/null; then
echo -e "${YELLOW}⚠ Warning: Cinnamon desktop not detected${NC}"
echo " This applet is designed for the Cinnamon desktop environment."
if [[ -z "$CLONED_DIR" ]]; then
echo
read -p "Continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
else
echo " Continuing anyway."
echo
fi
fi
# Install applet
mkdir -p "$APPLET_DEST"
if [[ -d "$APPLET_DEST" && -f "$APPLET_DEST/metadata.json" ]]; then
echo -e "${BLUE}Applet already installed, upgrading...${NC}"
else
echo -e "${BLUE}Installing applet to $APPLET_DEST...${NC}"
fi
cp -f "$APPLET_SRC/metadata.json" "$APPLET_DEST/"
cp -f "$APPLET_SRC/applet.js" "$APPLET_DEST/"
cp -f "$APPLET_SRC/stylesheet.css" "$APPLET_DEST/"
echo -e "${GREEN}✓ Cinnamon applet installed${NC}"
echo
echo -e "${YELLOW}To activate the applet:${NC}"
echo " 1. Right-click on your panel"
echo " 2. Click 'Applets'"
echo " 3. Find 'Claude Code Usage' and enable it"
echo
echo -e "${YELLOW}If upgrading, you may need to restart Cinnamon:${NC}"
echo " Press Alt+F2, type 'r', and press Enter"
echo
fi
# Install status line
if [[ "$INSTALL_STATUSLINE" == true ]]; then
install_statusline
fi
# Final message
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✅ Installation complete!${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo
if [[ "$INSTALL_CLI" == true ]]; then
echo -e "${BLUE}CLI Tools:${NC}"
echo " c2switcher - Manage Claude Code accounts"
echo " c2claude - Run Claude with account switching"
echo
echo -e "${BLUE}Quick Start:${NC}"
echo " 1. Add your first account:"
echo " $ c2switcher add"
echo
echo " 2. Check usage across accounts:"
echo " $ c2switcher usage"
echo
echo " 3. Run Claude with optimal account:"
echo " $ c2claude"
echo
fi
if [[ "$INSTALL_APPLET" == true ]]; then
echo -e "${BLUE}Cinnamon Applet:${NC}"
echo " 1. Right-click your panel → 'Applets'"
echo " 2. Find 'Claude Code Usage' and enable it"
echo " 3. The applet shows average Sonnet usage on the panel"
echo " 4. Click it for detailed usage and account switching"
echo
fi
if [[ "$INSTALL_STATUSLINE" == true ]]; then
echo -e "${BLUE}Claude Code Status Line:${NC}"
echo " Active account and usage appear in the Claude Code status line:"
echo " [0] work 5h:42% 7d:18%"
echo " Usage is color-coded (green/yellow/red) and refreshes with the 5-min cache."
echo
fi
echo -e "${CYAN}Documentation: ${NC}https://github.com/incompletebiped/c2switcher-mint"
echo