-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·669 lines (616 loc) · 20.9 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·669 lines (616 loc) · 20.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#!/bin/bash
set -e
# ---------------------------------------------------------------------------
# ./setup.sh Interactive setup / update wizard
# ./setup.sh clean Remove cache and Docker image (keeps database)
# ./setup.sh clean --all Remove everything including the database
# ---------------------------------------------------------------------------
# Colors (skip if not a terminal)
if [ -t 1 ]; then
C_RESET='\033[0m'
C_BOLD='\033[1m'
C_DIM='\033[2m'
C_CYAN='\033[1;36m'
C_GREEN='\033[0;32m'
C_RED='\033[0;31m'
C_YELLOW='\033[0;33m'
else
C_RESET='' C_BOLD='' C_DIM='' C_CYAN='' C_GREEN='' C_RED='' C_YELLOW=''
fi
info() { printf "${C_CYAN}%s${C_RESET}\n" "$*"; }
ok() { printf "${C_GREEN}✓${C_RESET} %s\n" "$*"; }
err() { printf "${C_RED}✗ %s${C_RESET}\n" "$*" >&2; }
warn() { printf "${C_YELLOW}! %s${C_RESET}\n" "$*"; }
header(){ printf "\n${C_BOLD}${C_CYAN}=== %s ===${C_RESET}\n\n" "$*"; }
spinner() {
local pid=$1 msg=$2
local frames='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
local i=0 start=$(date +%s)
while kill -0 "$pid" 2>/dev/null; do
local elapsed=$(( $(date +%s) - start ))
printf "\r ${C_DIM}${frames:i%10:1}${C_RESET} %s (%ds)" "$msg" "$elapsed"
sleep 0.1
i=$((i + 1))
done
printf "\r\033[K"
wait "$pid"
return $?
}
# --- Clean command ---
if [ "${1:-}" = "clean" ]; then
header "Clean"
# Docker needs sudo?
if docker info > /dev/null 2>&1; then
COMPOSE="docker compose"
DOCKER="docker"
else
COMPOSE="sudo docker compose"
DOCKER="sudo docker"
fi
# Always: stop container, remove image, remove cache
$COMPOSE down 2>/dev/null && ok "Stopped container" || true
$COMPOSE down --rmi all 2>/dev/null && ok "Removed Docker image" || true
if [ -d .local/cache ]; then
rm -rf .local/cache
ok "Removed Showdown cache"
fi
if [ -f .local/viewer-index.db ]; then
rm -f .local/viewer-index.db
ok "Removed search index"
fi
# --all: also remove database and config
if [ "${2:-}" = "--all" ]; then
echo ""
warn "This will delete your Pokemon database and all configuration."
printf "Are you sure? [y/N]: "
read -r confirm
case "$confirm" in
[yY]*)
rm -rf .local .env
ok "Removed database, config, and .env"
;;
*)
echo " Database kept."
;;
esac
else
echo ""
info "Database and config are untouched."
echo " To also remove the database: ./setup.sh clean --all"
fi
echo ""
exit 0
fi
# --- Prerequisites ---
missing=""
for cmd in docker curl; do
if ! command -v "$cmd" > /dev/null 2>&1; then
missing="$missing $cmd"
fi
done
if [ -n "$missing" ]; then
echo ""
err "Looks like some required tools are missing:$missing"
echo ""
for cmd in $missing; do
case "$cmd" in
docker)
printf " Install Docker: ${C_CYAN}https://docs.docker.com/get-docker/${C_RESET}\n"
;;
curl)
printf " Install curl: ${C_CYAN}sudo apt install curl${C_RESET}\n"
;;
esac
done
echo ""
echo "Please install them and run this script again."
exit 1
fi
if ! docker compose version > /dev/null 2>&1; then
echo ""
err "Docker Compose v2 is required but not installed."
echo ""
printf " Install it here: ${C_CYAN}https://docs.docker.com/compose/install/${C_RESET}\n"
exit 1
fi
# Use sudo for docker if the current user can't access it directly
if docker info > /dev/null 2>&1; then
COMPOSE="docker compose"
DOCKER="docker"
else
warn "Docker requires elevated permissions, using sudo."
echo ""
COMPOSE="sudo docker compose"
DOCKER="sudo docker"
fi
IP=$(hostname -I 2>/dev/null | awk '{print $1}')
# Fix ownership of .local/ files (Docker creates them as root)
if [ -d .local ] && [ "$(find .local -not -user "$(id -u)" -print -quit 2>/dev/null)" ]; then
sudo chown -R "$(id -u):$(id -g)" .local
fi
# --- Check existing setup ---
if [ -f .local/config.json ]; then
# ===========================================
# Already set up - check for updates
# ===========================================
header "GPSS-in-a-Box"
RUNNING=false
if $DOCKER ps --filter name=local-gpss --format '{{.Names}}' 2>/dev/null | grep -q local-gpss; then
RUNNING=true
fi
IMAGE_EXISTS=false
IMAGE_NAME=$($COMPOSE images -q 2>/dev/null)
if [ -n "$IMAGE_NAME" ]; then
IMAGE_EXISTS=true
fi
CURRENT_VERSION=""
if [ -f .local/pkhex_version ]; then
CURRENT_VERSION=$(cat .local/pkhex_version)
fi
LATEST_TAG=$(curl -sL https://api.github.com/repos/santacrab2/PKHeX-Plugins/releases/latest | grep '"tag_name"' | head -1 | cut -d'"' -f4)
NEED_BUILD=false
UPDATE_LEGALITY=false
PKHEX_TAG="${CURRENT_VERSION}"
RECHECK=false
# Image missing - need to rebuild with whatever was last used
if [ "$IMAGE_EXISTS" = "false" ]; then
warn "Docker image not found. Needs to be rebuilt."
echo ""
NEED_BUILD=true
if [ -n "$CURRENT_VERSION" ] && [ "$CURRENT_VERSION" != "bundled" ]; then
UPDATE_LEGALITY=true
fi
fi
# Check for engine updates
if [ -z "$CURRENT_VERSION" ] || [ "$CURRENT_VERSION" = "bundled" ]; then
if [ -n "$LATEST_TAG" ]; then
info "You're using the bundled legality engine (Dec 2025)."
info "Latest version available: $LATEST_TAG"
echo ""
printf "Build the latest engine? [Y/n]: "
read -r choice
case "$choice" in
[nN]*)
echo " Keeping bundled engine"
;;
*)
NEED_BUILD=true
UPDATE_LEGALITY=true
PKHEX_TAG="$LATEST_TAG"
echo " Will build latest engine"
;;
esac
else
info "Legality engine: bundled (Dec 2025)"
warn "Could not check for updates"
fi
elif [ -n "$LATEST_TAG" ] && [ "$LATEST_TAG" != "$CURRENT_VERSION" ]; then
info "Legality engine update available: $CURRENT_VERSION → $LATEST_TAG"
echo ""
printf "Update? [Y/n]: "
read -r choice
case "$choice" in
[nN]*)
echo " Keeping current version"
;;
*)
NEED_BUILD=true
UPDATE_LEGALITY=true
PKHEX_TAG="$LATEST_TAG"
echo " Will update engine"
;;
esac
else
ok "Legality engine is up to date ($CURRENT_VERSION)."
fi
# Re-check if updating engine and database exists
if [ "$UPDATE_LEGALITY" = "true" ] && [ -f .local/local-gpss.db ]; then
echo ""
echo "Re-check all Pokemon with the new engine?"
echo "This runs in the background after the server starts."
echo ""
printf "Re-check? [Y/n]: "
read -r recheck_choice
case "$recheck_choice" in
[nN]*)
echo " Skipping re-check"
;;
*)
RECHECK=true
echo " Will re-check after start"
;;
esac
fi
# Check for empty database — offer to download community backup
DOWNLOAD_DB=false
if [ -f .local/local-gpss.db ]; then
DB_SIZE=$(stat -c%s .local/local-gpss.db 2>/dev/null || stat -f%z .local/local-gpss.db 2>/dev/null || echo 0)
if [ "$DB_SIZE" -lt 100000 ]; then
echo ""
warn "Database is nearly empty ($(( DB_SIZE / 1024 ))KB)."
echo "The community Pokemon backup (~91k Pokemon) can be downloaded"
echo "to give you a full library to browse."
echo ""
printf "Download Pokemon backup? [Y/n]: "
read -r dl_choice
case "$dl_choice" in
[nN]*)
echo " Skipping download"
;;
*)
DOWNLOAD_DB=true
echo " Will download Pokemon backup"
;;
esac
fi
else
echo ""
echo "No database found. The community Pokemon backup (~91k Pokemon)"
echo "can be downloaded so your server has Pokemon ready to browse."
echo ""
printf "Download Pokemon backup? [Y/n]: "
read -r dl_choice
case "$dl_choice" in
[nN]*)
echo " Skipping download"
;;
*)
DOWNLOAD_DB=true
echo " Will download Pokemon backup"
;;
esac
fi
# Update config
cat > .local/config.json << EOF
{
"fancy_screen": false,
"database": {
"db_type": "sqlite",
"connection_string": "file:local-gpss.db?cache=shared&_pragma=foreign_keys(1)"
},
"http": {
"port": 8082,
"listening_addr": "0.0.0.0"
},
"misc": {
"recheck_legality": $RECHECK,
"migrate_original_db": $DOWNLOAD_DB,
"download_original_db": $DOWNLOAD_DB
}
}
EOF
# Build if needed
if [ "$NEED_BUILD" = "true" ]; then
if [ "$RUNNING" = "true" ]; then
$COMPOSE down 2>/dev/null || true
RUNNING=false
fi
cat > .env << EOF
BUILD_TARGET=runtime
UPDATE_LEGALITY=$UPDATE_LEGALITY
PKHEX_TAG=${PKHEX_TAG:-unused}
EOF
echo ""
$COMPOSE build > /tmp/gpss-build.log 2>&1 &
BUILD_PID=$!
if spinner $BUILD_PID "Building Docker image"; then
ok "Build complete"
else
err "Build failed. Last 20 lines:"
tail -20 /tmp/gpss-build.log
echo ""
if grep -q "Could not resolve host" /tmp/gpss-build.log 2>/dev/null; then
warn "Could not reach GitHub. Check your internet connection."
elif grep -q "no space left" /tmp/gpss-build.log 2>/dev/null; then
warn "Disk full — check available space: df -h"
fi
exit 1
fi
if [ "$UPDATE_LEGALITY" = "true" ]; then
echo "$PKHEX_TAG" > .local/pkhex_version
else
echo "bundled" > .local/pkhex_version
fi
fi
# Start / restart / already running
if [ "$NEED_BUILD" = "true" ]; then
echo ""
info "Starting server..."
$COMPOSE up -d
if [ "$RECHECK" = "true" ]; then
echo ""
info "The Pokemon re-check is running in the background."
echo "You can use the server right away. To see progress:"
echo ""
echo " $COMPOSE logs -f"
fi
elif [ "$RUNNING" = "true" ]; then
echo ""
ok "Server is already running."
else
echo ""
printf "Start the server? [Y/n]: "
read -r start_choice
case "$start_choice" in
[nN]*)
echo ""
echo "Run '$COMPOSE up -d' when ready."
;;
*)
echo ""
$COMPOSE up -d
;;
esac
fi
if [ -n "$IP" ]; then
echo "$IP" > .local/host_ip
echo ""
header "All done!"
echo ""
printf " ${C_BOLD}Server URL:${C_RESET}\n"
printf "\n"
printf " ┌─────────────────────────────────┐\n"
SUMMARY_URL="http://${IP}:8082/"
printf " │ │\n"
printf " │ ${C_BOLD}%s${C_RESET}" "$SUMMARY_URL"
SUMMARY_PAD=$((31 - ${#SUMMARY_URL}))
printf "%*s│\n" "$SUMMARY_PAD" ""
printf " │ │\n"
printf " └─────────────────────────────────┘\n"
echo ""
printf " ${C_BOLD}In PKSM:${C_RESET}\n"
printf " Set server URL to: ${C_CYAN}http://${IP}:8082/${C_RESET}\n"
printf " ${C_DIM}(include the / at the end)${C_RESET}\n"
echo ""
printf " ${C_BOLD}Quick reference:${C_RESET}\n"
printf " ${C_DIM}start${C_RESET} $COMPOSE up -d\n"
printf " ${C_DIM}stop${C_RESET} $COMPOSE down\n"
printf " ${C_DIM}logs${C_RESET} $COMPOSE logs -f\n"
printf " ${C_DIM}update${C_RESET} ./setup.sh\n"
echo ""
fi
echo ""
else
# ===========================================
# First time setup
# ===========================================
header "GPSS-in-a-Box Setup"
echo "This sets up a local GPSS server for PKSM on your 3DS."
echo "The official server shut down in January 2026. This"
echo "runs a replacement on your own machine."
echo ""
# --- Legality engine ---
echo "--- Legality Engine ---"
echo ""
echo "The legality engine checks if Pokemon are valid and powers"
echo "the auto-legalization feature in PKSM. You can build the"
echo "latest version from source, or use the one bundled with"
echo "the original release (from December 2025)."
echo ""
echo " Yes = Build latest (recommended, adds ~30s to build)"
echo " No = Use bundled version from Dec 2025"
echo ""
printf "Build latest legality engine? [Y/n]: "
read -r legality_choice
case "$legality_choice" in
[nN]*)
UPDATE_LEGALITY=false
echo " Using bundled engine (Dec 2025)"
;;
*)
UPDATE_LEGALITY=true
PKHEX_TAG=$(curl -sL https://api.github.com/repos/santacrab2/PKHeX-Plugins/releases/latest | grep '"tag_name"' | head -1 | cut -d'"' -f4)
if [ -z "$PKHEX_TAG" ]; then
warn "Could not fetch latest version. Using bundled engine instead"
UPDATE_LEGALITY=false
else
ok "Will build latest engine (version $PKHEX_TAG)"
fi
;;
esac
# --- Backup database ---
if [ -f .local/local-gpss.db ]; then
DOWNLOAD_DB=false
echo ""
ok "Existing Pokemon database found. Will use it."
else
echo ""
echo "--- Pokemon Database ---"
echo ""
echo "Before the official server shut down, the community had"
echo "uploaded about 91,000 Pokemon. You can download this"
echo "backup so your server has Pokemon ready to browse and"
echo "download right away."
echo ""
echo " Yes = Download the backup (recommended)"
echo " No = Start with an empty server"
echo ""
printf "Download Pokemon backup? [Y/n]: "
read -r db_choice
case "$db_choice" in
[nN]*)
DOWNLOAD_DB=false
echo " Starting with empty database"
;;
*)
DOWNLOAD_DB=true
echo " Will download Pokemon backup"
;;
esac
fi
# --- Re-check legality ---
RECHECK=false
HAS_DB=false
if [ "$DOWNLOAD_DB" = "true" ] || [ -f .local/local-gpss.db ]; then
HAS_DB=true
fi
if [ "$UPDATE_LEGALITY" = "true" ] && [ "$HAS_DB" = "true" ]; then
echo ""
echo "--- Re-check Pokemon ---"
echo ""
echo "The backup Pokemon were checked with an older engine."
echo "You can re-check them with the new one to make sure"
echo "their legal/illegal status is accurate. This happens"
echo "in the background after the server starts. You can"
echo "use the server normally while it works."
echo ""
echo " Yes = Re-check all Pokemon (recommended)"
echo " No = Keep original data (faster first start)"
echo ""
printf "Re-check Pokemon? [Y/n]: "
read -r recheck_choice
case "$recheck_choice" in
[nN]*)
RECHECK=false
echo " Keeping original data"
;;
*)
RECHECK=true
echo " Will re-check after first start"
;;
esac
fi
# --- Start server ---
echo ""
printf "Start the server after building? [Y/n]: "
read -r start_choice
case "$start_choice" in
[nN]*)
START=false
echo " Will build only"
;;
*)
START=true
echo " Will start after build"
;;
esac
# --- Summary ---
echo ""
header "Ready to build"
echo " Legality engine: $([ "$UPDATE_LEGALITY" = "true" ] && echo "Latest (v$PKHEX_TAG)" || echo "Bundled (Dec 2025)")"
if [ -f .local/local-gpss.db ]; then
echo " Pokemon database: Existing"
elif [ "$DOWNLOAD_DB" = "true" ]; then
echo " Pokemon backup: Download"
else
echo " Pokemon backup: No (empty server)"
fi
if [ "$UPDATE_LEGALITY" = "true" ] && [ "$HAS_DB" = "true" ]; then
echo " Re-check Pokemon: $([ "$RECHECK" = "true" ] && echo "Yes (runs in background)" || echo "No")"
fi
echo " Start after build: $([ "$START" = "true" ] && echo "Yes" || echo "No")"
echo ""
printf "Look good? [Y/n]: "
read -r confirm
case "$confirm" in
[nN]*)
echo "Setup cancelled."
exit 0
;;
esac
# --- Stop existing container if running ---
$COMPOSE down 2>/dev/null || true
# --- Generate config ---
mkdir -p .local
cat > .local/config.json << EOF
{
"fancy_screen": false,
"database": {
"db_type": "sqlite",
"connection_string": "file:local-gpss.db?cache=shared&_pragma=foreign_keys(1)"
},
"http": {
"port": 8082,
"listening_addr": "0.0.0.0"
},
"misc": {
"recheck_legality": $RECHECK,
"migrate_original_db": $DOWNLOAD_DB,
"download_original_db": $DOWNLOAD_DB
}
}
EOF
if [ "$UPDATE_LEGALITY" = "true" ]; then
echo "$PKHEX_TAG" > .local/pkhex_version
else
echo "bundled" > .local/pkhex_version
fi
if [ -n "$IP" ]; then
echo "$IP" > .local/host_ip
fi
# Save build args so 'docker compose up --build' uses the right ones
cat > .env << EOF
BUILD_TARGET=runtime
UPDATE_LEGALITY=$UPDATE_LEGALITY
PKHEX_TAG=${PKHEX_TAG:-unused}
EOF
# --- Build ---
echo ""
$COMPOSE build > /tmp/gpss-build.log 2>&1 &
BUILD_PID=$!
if spinner $BUILD_PID "Building Docker image (this takes about a minute the first time)"; then
ok "Build complete"
else
err "Build failed. Last 20 lines:"
tail -20 /tmp/gpss-build.log
echo ""
if grep -q "Could not resolve host" /tmp/gpss-build.log 2>/dev/null; then
warn "Could not reach GitHub. Check your internet connection."
elif grep -q "no space left" /tmp/gpss-build.log 2>/dev/null; then
warn "Disk full — check available space: df -h"
fi
exit 1
fi
# --- Start ---
if [ "$START" = "true" ]; then
echo ""
info "Starting server..."
$COMPOSE up -d
if [ "$RECHECK" = "true" ]; then
echo ""
info "The Pokemon re-check is running in the background."
echo "You can use the server right away. To see progress:"
echo ""
echo " $COMPOSE logs -f"
elif [ "$DOWNLOAD_DB" = "true" ]; then
echo ""
info "The Pokemon backup is importing in the background."
echo "To see progress:"
echo ""
echo " $COMPOSE logs -f"
fi
else
echo ""
header "Build complete!"
echo "When you're ready to start the server, run:"
echo ""
echo " $COMPOSE up -d"
fi
if [ -n "$IP" ]; then
echo ""
header "All done!"
echo ""
printf " ${C_BOLD}Server URL:${C_RESET}\n"
printf "\n"
printf " ┌─────────────────────────────────┐\n"
SUMMARY_URL="http://${IP}:8082/"
printf " │ │\n"
printf " │ ${C_BOLD}%s${C_RESET}" "$SUMMARY_URL"
SUMMARY_PAD=$((31 - ${#SUMMARY_URL}))
printf "%*s│\n" "$SUMMARY_PAD" ""
printf " │ │\n"
printf " └─────────────────────────────────┘\n"
echo ""
printf " ${C_BOLD}In PKSM:${C_RESET}\n"
printf " Set server URL to: ${C_CYAN}http://${IP}:8082/${C_RESET}\n"
printf " ${C_DIM}(include the / at the end)${C_RESET}\n"
echo ""
printf " ${C_BOLD}Quick reference:${C_RESET}\n"
printf " ${C_DIM}start${C_RESET} $COMPOSE up -d\n"
printf " ${C_DIM}stop${C_RESET} $COMPOSE down\n"
printf " ${C_DIM}logs${C_RESET} $COMPOSE logs -f\n"
printf " ${C_DIM}update${C_RESET} ./setup.sh\n"
echo ""
fi
echo ""
fi