Repository navigation
Expand file tree
/
Copy pathinstaller.sh
More file actions
executable file
·2191 lines (2044 loc) · 65.1 KB
/
Copy pathinstaller.sh
File metadata and controls
executable file
·2191 lines (2044 loc) · 65.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Copyright (C) 2026 Yota Hamada
# SPDX-License-Identifier: GPL-3.0-or-later
set -euo pipefail
RELEASES_URL="https://github.com/dagucloud/dagu/releases"
GITHUB_API_URL="https://api.github.com/repos/dagucloud/dagu/releases/latest"
FILE_BASENAME="dagu"
WINSW_VERSION="v2.12.0"
GUM_VERSION="0.17.0"
BOLD='\033[1m'
ACCENT='\033[38;2;11;93;68m'
INFO='\033[38;2;92;108;122m'
SUCCESS='\033[38;2;18;184;134m'
WARN='\033[38;2;194;124;14m'
ERROR='\033[38;2;196;62;62m'
MUTED='\033[38;2;120;128;142m'
NC='\033[0m'
TMPFILE_REGISTRY="$(mktemp)"
cleanup_tmpfiles() {
local f
if [[ -f "$TMPFILE_REGISTRY" ]]; then
while IFS= read -r f; do
rm -rf "$f" 2>/dev/null || true
done <"$TMPFILE_REGISTRY"
rm -f "$TMPFILE_REGISTRY" 2>/dev/null || true
fi
}
trap cleanup_tmpfiles EXIT
mktempdir() {
local d
if [[ -n "${WORKING_ROOT_DIR:-}" ]]; then
mkdir -p "${WORKING_ROOT_DIR}" >/dev/null 2>&1 || true
d="$(mktemp -d "${WORKING_ROOT_DIR%/}/dagu-installer.XXXXXX")"
else
d="$(mktemp -d)"
fi
printf '%s\n' "$d" >>"$TMPFILE_REGISTRY"
printf '%s\n' "$d"
}
mktempfile() {
local d f
d="$(mktempdir)"
f="${d}/tmpfile"
: >"$f"
printf '%s\n' "$f"
}
WORKING_ROOT_DIR=""
DAGU_INSTALL_DIR="${DAGU_INSTALL_DIR:-}"
VERSION=""
NO_PROMPT=0
DRY_RUN=0
VERBOSE=0
UNINSTALL_MODE=0
PURGE_DATA=0
REMOVE_SKILL=0
SERVICE_MODE=""
SERVICE_SCOPE=""
HOST=""
PORT=""
SKILL_MODE=""
declare -a EXPLICIT_SKILL_DIRS=()
ADMIN_USERNAME=""
ADMIN_PASSWORD=""
OPEN_BROWSER=""
GUM=""
GUM_STATUS="skipped"
GUM_REASON=""
DOWNLOADER=""
OS=""
ARCH=""
INSTALL_OWNER="${SUDO_USER:-$(id -un 2>/dev/null || printf '%s' "${USER:-unknown}")}"
INSTALL_GROUP="$(id -gn "${INSTALL_OWNER}" 2>/dev/null || printf '%s' "${INSTALL_OWNER}")"
SERVICE_ENV_FILE=""
SERVICE_BOOTSTRAP_FILE=""
SERVICE_UNIT_FILE=""
SERVICE_PLIST_FILE=""
SERVICE_LABEL="local.dagu.server"
DAGU_HOME_DIR=""
INSTALL_PATH=""
SERVICE_URL=""
SERVICE_PATH=""
SKILL_DETECTED_COUNT=0
SCRUB_BACKUP_FILE=""
declare -a UNINSTALL_INSTALL_PATHS=()
declare -a UNINSTALL_DAGU_HOMES=()
declare -a UNINSTALL_PATH_PROFILES=()
declare -a UNINSTALL_SKILL_DIRS=()
declare -a UNINSTALL_COPILOT_FILES=()
declare -a UNINSTALL_SERVICE_SCOPES=()
UNINSTALL_MAC_SERVICE=0
UNINSTALL_MULTIPLE_INSTALLS_CONFIRMED=0
usage() {
cat <<'EOF'
Dagu installer wizard
Options:
--uninstall Remove Dagu, its background service, and installer PATH changes
--purge-data Also delete the detected Dagu data directory
--remove-skill Also remove Dagu AI skill installs
--version <tag> Install a specific version (for example: v1.24.0)
--install-dir <path> Install to a custom directory
--prefix <path> Alias for --install-dir
--working-dir <path> Store temporary files under this directory
--service <yes|no> Install and start Dagu as a background service
--service-scope <scope> Service scope: user or system
--host <host> Host address for the Dagu web UI
--port <port> Port for the Dagu web UI
--skills-dir <path> Deprecated; use gh skill install dagucloud/dagu dagu instead
--admin-username <name> Initial admin username for builtin auth bootstrap
--admin-password <pass> Initial admin password for builtin auth bootstrap
--open-browser <yes|no> Open the Dagu URL after successful setup
--no-prompt Disable interactive prompts
--dry-run Print the plan without making changes
--verbose Show command output during install
--help Show this help message
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
shift
VERSION="${1:-}"
;;
--install-dir|--prefix)
shift
DAGU_INSTALL_DIR="${1:-}"
;;
--working-dir)
shift
WORKING_ROOT_DIR="${1:-}"
;;
--service)
shift
SERVICE_MODE="${1:-}"
;;
--service-scope)
shift
SERVICE_SCOPE="${1:-}"
;;
--host)
shift
HOST="${1:-}"
;;
--port)
shift
PORT="${1:-}"
;;
--skills-dir)
shift
EXPLICIT_SKILL_DIRS+=("${1:-}")
;;
--admin-username)
shift
ADMIN_USERNAME="${1:-}"
;;
--admin-password)
shift
ADMIN_PASSWORD="${1:-}"
;;
--open-browser)
shift
OPEN_BROWSER="${1:-}"
;;
--no-prompt)
NO_PROMPT=1
;;
--dry-run)
DRY_RUN=1
;;
--verbose)
VERBOSE=1
;;
--uninstall)
UNINSTALL_MODE=1
;;
--purge-data)
PURGE_DATA=1
;;
--remove-skill)
REMOVE_SKILL=1
;;
--help|-h)
usage
exit 0
;;
*)
printf '%s\n' "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
shift
done
detect_downloader() {
if command -v curl >/dev/null 2>&1; then
DOWNLOADER="curl"
return 0
fi
if command -v wget >/dev/null 2>&1; then
DOWNLOADER="wget"
return 0
fi
printf '%s\n' "Missing downloader: install curl or wget." >&2
exit 1
}
download_file() {
local url="$1"
local output="$2"
detect_downloader
if [[ "$DOWNLOADER" == "curl" ]]; then
curl -fsSL --proto '=https' --tlsv1.2 --retry 3 --retry-delay 1 --retry-connrefused -o "$output" "$url"
else
wget -q --https-only --secure-protocol=TLSv1_2 --tries=3 --timeout=20 -O "$output" "$url"
fi
}
download_stdout() {
local url="$1"
detect_downloader
if [[ "$DOWNLOADER" == "curl" ]]; then
curl -fsSL --proto '=https' --tlsv1.2 --retry 3 --retry-delay 1 --retry-connrefused "$url"
else
wget -qO- --https-only --secure-protocol=TLSv1_2 --tries=3 --timeout=20 "$url"
fi
}
is_non_interactive_shell() {
if [[ "$NO_PROMPT" == "1" ]]; then
return 0
fi
if [[ ! -t 0 || ! -t 1 ]]; then
return 0
fi
return 1
}
gum_is_tty() {
if [[ -n "${NO_COLOR:-}" ]]; then
return 1
fi
if [[ "${TERM:-dumb}" == "dumb" ]]; then
return 1
fi
if [[ -t 2 || -t 1 ]]; then
return 0
fi
if [[ -r /dev/tty && -w /dev/tty ]]; then
return 0
fi
return 1
}
verify_sha256sum_file() {
local checksums="$1"
if command -v sha256sum >/dev/null 2>&1; then
# NOTE: Callers must pre-filter $checksums to contain only the entry
# for the file being verified. This avoids --ignore-missing, which is
# not supported by all sha256sum implementations (e.g., BusyBox).
sha256sum -c "$checksums" >/dev/null 2>&1
return $?
fi
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 -c "$checksums" >/dev/null 2>&1
return $?
fi
return 1
}
quote_env_value() {
local value="$1"
value=${value//\'/\'\\\'\'}
printf "'%s'" "$value"
}
xml_escape() {
local value="$1"
value=${value//&/&}
value=${value//</<}
value=${value//>/>}
value=${value//\"/"}
value=${value//\'/'}
printf '%s' "$value"
}
json_escape() {
local value="$1"
value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//$'\n'/\\n}
value=${value//$'\r'/\\r}
value=${value//$'\t'/\\t}
printf '%s' "$value"
}
has_admin_bootstrap() {
[[ -n "${ADMIN_USERNAME}" && -n "${ADMIN_PASSWORD}" ]]
}
validate_admin_bootstrap() {
if [[ -n "${ADMIN_USERNAME}" && -z "${ADMIN_PASSWORD}" ]]; then
ui_error "An admin password is required when --admin-username is provided."
exit 2
fi
if [[ -z "${ADMIN_USERNAME}" && -n "${ADMIN_PASSWORD}" ]]; then
ui_error "An admin username is required when --admin-password is provided."
exit 2
fi
if has_admin_bootstrap && [[ "${#ADMIN_PASSWORD}" -lt 8 ]]; then
ui_error "The admin password must be at least 8 characters."
exit 1
fi
}
bootstrap_gum_temp() {
local os_name gum_arch asset base gum_tmp asset_sum
GUM=""
GUM_STATUS="skipped"
GUM_REASON=""
if is_non_interactive_shell; then
GUM_REASON="non-interactive shell"
return 1
fi
if ! gum_is_tty; then
GUM_REASON="terminal does not support gum"
return 1
fi
if command -v gum >/dev/null 2>&1; then
GUM="gum"
GUM_STATUS="found"
GUM_REASON="already installed"
return 0
fi
if ! command -v tar >/dev/null 2>&1; then
GUM_REASON="tar not available"
return 1
fi
case "$(uname -s 2>/dev/null || true)" in
Darwin) os_name="Darwin" ;;
Linux) os_name="Linux" ;;
*) GUM_REASON="unsupported OS"; return 1 ;;
esac
case "$(uname -m 2>/dev/null || true)" in
x86_64|amd64) gum_arch="x86_64" ;;
arm64|aarch64) gum_arch="arm64" ;;
i386|i686) gum_arch="i386" ;;
armv7l|armv7) gum_arch="armv7" ;;
armv6l|armv6) gum_arch="armv6" ;;
*) GUM_REASON="unsupported architecture"; return 1 ;;
esac
gum_tmp="$(mktempdir)"
asset="gum_${GUM_VERSION}_${os_name}_${gum_arch}.tar.gz"
base="https://github.com/charmbracelet/gum/releases/download/v${GUM_VERSION}"
if ! download_file "${base}/${asset}" "${gum_tmp}/${asset}"; then
GUM_REASON="download failed"
return 1
fi
if ! download_file "${base}/checksums.txt" "${gum_tmp}/checksums.txt"; then
GUM_REASON="checksum download failed"
return 1
fi
asset_sum="${gum_tmp}/checksums-${asset}.txt"
grep " ${asset}\$" "${gum_tmp}/checksums.txt" >"${asset_sum}" 2>/dev/null || true
if [[ ! -s "${asset_sum}" ]]; then
GUM_REASON="checksum entry missing"
return 1
fi
if ! (cd "${gum_tmp}" && verify_sha256sum_file "$(basename "${asset_sum}")"); then
GUM_REASON="checksum verification failed"
return 1
fi
if ! tar -xzf "${gum_tmp}/${asset}" -C "${gum_tmp}" >/dev/null 2>&1; then
GUM_REASON="extract failed"
return 1
fi
GUM="$(find "${gum_tmp}" -type f -name gum 2>/dev/null | head -n1 || true)"
if [[ -z "${GUM}" || ! -f "${GUM}" ]]; then
GUM_REASON="gum binary missing"
return 1
fi
chmod +x "${GUM}" >/dev/null 2>&1 || true
GUM_STATUS="installed"
GUM_REASON="temporary verified install"
return 0
}
ui_info() {
local msg="$*"
if [[ -n "$GUM" ]]; then
"$GUM" log --level info "$msg"
else
printf '%b·%b %s\n' "${MUTED}" "${NC}" "$msg"
fi
}
ui_warn() {
local msg="$*"
if [[ -n "$GUM" ]]; then
"$GUM" log --level warn "$msg"
else
printf '%b!%b %s\n' "${WARN}" "${NC}" "$msg"
fi
}
ui_success() {
local msg="$*"
if [[ -n "$GUM" ]]; then
local mark
mark="$("$GUM" style --foreground "#12b886" --bold "✓")"
printf '%s %s\n' "$mark" "$msg"
else
printf '%b✓%b %s\n' "${SUCCESS}" "${NC}" "$msg"
fi
}
ui_error() {
local msg="$*"
if [[ -n "$GUM" ]]; then
"$GUM" log --level error "$msg"
else
printf '%b✗%b %s\n' "${ERROR}" "${NC}" "$msg"
fi
}
ui_section() {
local title="$1"
if [[ -n "$GUM" ]]; then
"$GUM" style --bold --foreground "#0b5d44" --padding "1 0" "$title"
else
printf '\n%b%b%s%b\n' "${ACCENT}" "${BOLD}" "$title" "${NC}"
fi
}
ui_kv() {
local key="$1"
local value="$2"
if [[ -n "$GUM" ]]; then
local key_part value_part
key_part="$("$GUM" style --foreground "#78808e" --width 20 "$key")"
value_part="$("$GUM" style --bold "$value")"
"$GUM" join --horizontal "$key_part" "$value_part"
else
printf '%b%s:%b %s\n' "${MUTED}" "$key" "${NC}" "$value"
fi
}
print_banner() {
if [[ -n "$GUM" ]]; then
local title tagline card
title="$("$GUM" style --foreground "#0b5d44" --bold "Dagu Installer")"
tagline="$("$GUM" style --foreground "#5c6c7a" "Install Dagu, set it up as a background app, and get you to the UI quickly.")"
card="$(printf '%s\n%s' "$title" "$tagline")"
"$GUM" style --border rounded --border-foreground "#0b5d44" --padding "1 2" "$card"
printf '\n'
else
printf '%b%bDagu Installer%b\n' "${ACCENT}" "${BOLD}" "${NC}"
printf '%bInstall Dagu, set it up as a background app, and get you to the UI quickly.%b\n\n' "${INFO}" "${NC}"
fi
}
run_with_spinner() {
local title="$1"
shift
if [[ -n "$GUM" && "$VERBOSE" != "1" ]]; then
"$GUM" spin --spinner dot --title "$title" -- "$@"
else
"$@"
fi
}
run_quiet_step() {
local title="$1"
shift
local log
if [[ "$VERBOSE" == "1" ]]; then
run_with_spinner "$title" "$@"
return $?
fi
log="$(mktempfile)"
if run_with_spinner "$title" bash -c "$(printf '%q ' "$@") >$(printf '%q' "$log") 2>&1"; then
return 0
fi
ui_error "${title} failed"
if [[ -s "$log" ]]; then
tail -n 80 "$log" >&2 || true
fi
return 1
}
is_promptable() {
if [[ "$NO_PROMPT" == "1" ]]; then
return 1
fi
if [[ -r /dev/tty && -w /dev/tty ]]; then
return 0
fi
return 1
}
prompt_line() {
local prompt="$1"
local answer=""
if ! is_promptable; then
return 1
fi
printf '%s' "$prompt" > /dev/tty
IFS= read -r answer < /dev/tty || true
printf '%s' "$answer"
}
prompt_yes_no() {
local prompt="$1"
local default_answer="$2"
local answer=""
if ! is_promptable; then
[[ "$default_answer" == "yes" ]]
return $?
fi
if [[ "$default_answer" == "yes" ]]; then
answer="$(prompt_line "${prompt} [Y/n]: " || true)"
case "${answer:-}" in
n|N|no|NO) return 1 ;;
*) return 0 ;;
esac
else
answer="$(prompt_line "${prompt} [y/N]: " || true)"
case "${answer:-}" in
y|Y|yes|YES) return 0 ;;
*) return 1 ;;
esac
fi
}
prompt_text() {
local label="$1"
local default_value="$2"
local answer=""
if ! is_promptable; then
printf '%s' "$default_value"
return 0
fi
if [[ -n "$default_value" ]]; then
answer="$(prompt_line "${label} [${default_value}]: " || true)"
printf '%s' "${answer:-$default_value}"
else
answer="$(prompt_line "${label}: " || true)"
printf '%s' "$answer"
fi
}
prompt_secret_confirm() {
local label="$1"
local first="" second=""
if ! is_promptable; then
return 1
fi
while true; do
printf '%s: ' "$label" > /dev/tty
IFS= read -r -s first < /dev/tty || true
printf '\nConfirm %s: ' "$label" > /dev/tty
IFS= read -r -s second < /dev/tty || true
printf '\n' > /dev/tty
if [[ "$first" != "$second" ]]; then
ui_warn "Passwords did not match. Try again."
continue
fi
printf '%s' "$first"
return 0
done
}
resolve_version() {
if [[ -n "$VERSION" ]]; then
if [[ "${VERSION}" == "latest" ]]; then
VERSION=""
elif [[ "${VERSION}" != v* ]]; then
VERSION="v${VERSION}"
return 0
else
return 0
fi
fi
if [[ "$DRY_RUN" == "1" ]]; then
VERSION="latest"
return 0
fi
local body
body="$(download_stdout "$GITHUB_API_URL" 2>/dev/null || true)"
VERSION="$(printf '%s' "$body" | grep -o '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' | head -n1 | sed 's/.*"\([^"]*\)".*/\1/')"
if [[ -n "$VERSION" ]]; then
return 0
fi
VERSION="$(download_stdout "${RELEASES_URL}/latest" 2>/dev/null | sed -n 's#.*/tag/\([^"]*\).*#\1#p' | head -n1 || true)"
if [[ -z "$VERSION" ]]; then
ui_error "Failed to determine the latest Dagu version."
exit 1
fi
}
detect_os_arch() {
case "$(uname -s 2>/dev/null || true)" in
Darwin) OS="macos"; ARCHIVE_OS="darwin" ;;
Linux) OS="linux"; ARCHIVE_OS="linux" ;;
*) ui_error "Unsupported operating system. Use the Windows PowerShell installer on Windows."; exit 1 ;;
esac
case "$(uname -m 2>/dev/null || true)" in
x86_64|amd64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
armv7l|armv7) ARCH="armv7" ;;
armv6l|armv6) ARCH="armv6" ;;
i386|i686) ARCH="386" ;;
ppc64le) ARCH="ppc64le" ;;
s390x) ARCH="s390x" ;;
*) ui_error "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
}
detect_skill_targets() {
local home_dir codex_dir
home_dir="${HOME}"
SKILL_DETECTED_COUNT=0
if [[ -f "${home_dir}/.claude/.claude.json" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
fi
if [[ -d "${AGENTS_HOME:-${home_dir}/.agents}" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
elif [[ -d "${CODEX_HOME:-${home_dir}/.codex}" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
fi
if [[ -d "${home_dir}/.config/opencode" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
fi
if [[ -f "${home_dir}/.gemini/GEMINI.md" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
fi
if [[ -f "${XDG_CONFIG_HOME:-${home_dir}}/.copilot/config.json" || -f "${home_dir}/.copilot/config.json" ]]; then
SKILL_DETECTED_COUNT=$((SKILL_DETECTED_COUNT + 1))
fi
}
default_install_dir() {
if [[ -n "$DAGU_INSTALL_DIR" ]]; then
printf '%s' "$DAGU_INSTALL_DIR"
return 0
fi
if [[ "$SERVICE_MODE" == "yes" && "$OS" == "linux" && "$SERVICE_SCOPE" == "system" ]]; then
printf '/usr/local/bin'
else
printf '%s/.local/bin' "$HOME"
fi
}
default_dagu_home() {
if [[ "$OS" == "linux" && "$SERVICE_MODE" == "yes" && "$SERVICE_SCOPE" == "system" ]]; then
printf '/var/lib/dagu'
else
printf '%s/.dagu' "$HOME"
fi
}
configure_defaults() {
if [[ -z "$SERVICE_MODE" ]]; then
if is_promptable; then
SERVICE_MODE="yes"
else
SERVICE_MODE="no"
fi
fi
if [[ "$SERVICE_MODE" != "yes" && "$SERVICE_MODE" != "no" ]]; then
ui_error "Invalid --service value: $SERVICE_MODE"
exit 2
fi
if [[ "$SERVICE_MODE" == "yes" && -z "$SERVICE_SCOPE" ]]; then
if [[ "$OS" == "linux" ]]; then
if command -v sudo >/dev/null 2>&1; then
SERVICE_SCOPE="system"
else
SERVICE_SCOPE="user"
fi
else
SERVICE_SCOPE="user"
fi
fi
if [[ "$OS" != "linux" ]]; then
SERVICE_SCOPE="user"
fi
if [[ "$SERVICE_MODE" == "no" ]]; then
SERVICE_SCOPE="user"
fi
if [[ "$SERVICE_SCOPE" != "user" && "$SERVICE_SCOPE" != "system" ]]; then
ui_error "Invalid --service-scope value: $SERVICE_SCOPE"
exit 2
fi
if [[ -z "$HOST" ]]; then
HOST="127.0.0.1"
fi
if [[ -z "$PORT" ]]; then
PORT="8080"
fi
DAGU_INSTALL_DIR="$(default_install_dir)"
DAGU_HOME_DIR="$(default_dagu_home)"
INSTALL_PATH="${DAGU_INSTALL_DIR}/dagu"
SERVICE_URL="http://${HOST}:${PORT}"
resolve_service_path
if [[ -z "$OPEN_BROWSER" ]]; then
OPEN_BROWSER="yes"
fi
if [[ ${#EXPLICIT_SKILL_DIRS[@]} -gt 0 ]]; then
SKILL_MODE="explicit"
elif [[ -z "${SKILL_MODE}" ]]; then
if [[ "$SKILL_DETECTED_COUNT" -gt 0 ]]; then
SKILL_MODE="auto"
else
SKILL_MODE="skip"
fi
fi
}
append_service_path_segment() {
local segment="$1"
[[ -z "$segment" ]] && return 0
case ":${SERVICE_PATH}:" in
*":${segment}:"*) return 0 ;;
esac
if [[ -z "${SERVICE_PATH}" ]]; then
SERVICE_PATH="${segment}"
else
SERVICE_PATH="${SERVICE_PATH}:${segment}"
fi
}
append_service_path_list() {
local list="$1"
local segment
local old_ifs="${IFS}"
[[ -z "$list" ]] && return 0
IFS=':'
for segment in $list; do
append_service_path_segment "$segment"
done
IFS="${old_ifs}"
}
resolve_service_path() {
local path_helper_output=""
SERVICE_PATH=""
append_service_path_list "${PATH:-}"
if [[ "$OS" == "macos" && -x /usr/libexec/path_helper ]]; then
path_helper_output="$("/usr/libexec/path_helper" -s 2>/dev/null | sed -n 's/^PATH=\"\(.*\)\"; export PATH$/\1/p' | head -n1 || true)"
append_service_path_list "${path_helper_output}"
fi
append_service_path_segment "${DAGU_INSTALL_DIR}"
append_service_path_segment "${HOME}/.local/bin"
append_service_path_segment "${HOME}/bin"
append_service_path_segment "${HOME}/.npm-global/bin"
append_service_path_segment "${HOME}/.local/share/pnpm"
append_service_path_segment "${HOME}/.bun/bin"
append_service_path_segment "/opt/homebrew/bin"
append_service_path_segment "/usr/local/bin"
append_service_path_segment "/usr/bin"
append_service_path_segment "/bin"
append_service_path_segment "/usr/sbin"
append_service_path_segment "/sbin"
if [[ -z "${SERVICE_PATH}" ]]; then
SERVICE_PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
fi
}
add_uninstall_install_path() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_INSTALL_PATHS[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_INSTALL_PATHS+=("$item")
}
add_uninstall_dagu_home() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_DAGU_HOMES[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_DAGU_HOMES+=("$item")
}
add_uninstall_path_profile() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_PATH_PROFILES[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_PATH_PROFILES+=("$item")
}
add_uninstall_skill_dir() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_SKILL_DIRS[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_SKILL_DIRS+=("$item")
}
add_uninstall_copilot_file() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_COPILOT_FILES[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_COPILOT_FILES+=("$item")
}
add_uninstall_service_scope() {
local item="$1"
local existing
[[ -z "$item" ]] && return 0
for existing in "${UNINSTALL_SERVICE_SCOPES[@]:-}"; do
[[ "$existing" == "$item" ]] && return 0
done
UNINSTALL_SERVICE_SCOPES+=("$item")
}
join_with_comma() {
local first=1
local value
for value in "$@"; do
[[ -z "$value" ]] && continue
if [[ "$first" == "1" ]]; then
printf '%s' "$value"
first=0
else
printf ', %s' "$value"
fi
done
}
strip_matching_install_paths() {
local target="$1"
local filtered=()
local path
for path in "${UNINSTALL_INSTALL_PATHS[@]:-}"; do
if [[ "$path" == "$target" ]]; then
filtered+=("$path")
fi
done
UNINSTALL_INSTALL_PATHS=("${filtered[@]}")
}
strip_matching_service_scopes() {
local target="$1"
local filtered=()
local scope
for scope in "${UNINSTALL_SERVICE_SCOPES[@]:-}"; do
if [[ "$scope" == "$target" ]]; then
filtered+=("$scope")
fi
done
UNINSTALL_SERVICE_SCOPES=("${filtered[@]}")
}
choose_operation_mode() {
if [[ "$UNINSTALL_MODE" == "1" ]]; then
return 0
fi
if ! is_promptable; then
return 0
fi
ui_section "Choose setup"
if prompt_yes_no "Install or repair Dagu now?" "yes"; then
UNINSTALL_MODE=0
else
UNINSTALL_MODE=1
fi
}
validate_uninstall_flags() {
if [[ "$UNINSTALL_MODE" != "1" ]]; then
return 0
fi
if [[ -n "$VERSION" ]]; then
ui_error "--version is only supported during install."
exit 2
fi
if [[ -n "$HOST" || -n "$PORT" ]]; then
ui_error "--host and --port are only supported during install."
exit 2
fi
if [[ -n "$ADMIN_USERNAME" || -n "$ADMIN_PASSWORD" ]]; then
ui_error "Admin bootstrap flags are only supported during install."
exit 2
fi
if [[ -n "$OPEN_BROWSER" ]]; then
ui_error "--open-browser is only supported during install."
exit 2
fi
if [[ -n "$SERVICE_MODE" ]]; then
ui_error "--service is only supported during install. Use --service-scope to target a specific uninstall scope."
exit 2
fi
if [[ -n "$SERVICE_SCOPE" && "$SERVICE_SCOPE" != "user" && "$SERVICE_SCOPE" != "system" ]]; then
ui_error "Invalid --service-scope value: $SERVICE_SCOPE"
exit 2
fi
if [[ "$OS" == "macos" && -n "$SERVICE_SCOPE" && "$SERVICE_SCOPE" != "user" ]]; then
ui_error "macOS uninstall only supports --service-scope user."
exit 2
fi
}
run_install_wizard() {
local answer
if ! is_promptable; then
return 0
fi
ui_section "Recommended setup"
ui_info "This wizard can install Dagu, run it in the background, create your first admin, and install the Dagu AI skill."
if prompt_yes_no "Install Dagu as a background service?" "yes"; then
SERVICE_MODE="yes"
else
SERVICE_MODE="no"
SERVICE_SCOPE="user"
fi
if [[ "$SERVICE_MODE" == "yes" && "$OS" == "linux" ]]; then
if prompt_yes_no "Use a system-wide Linux service?" "yes"; then
SERVICE_SCOPE="system"
else
SERVICE_SCOPE="user"
fi
fi
if prompt_yes_no "Open Dagu only on this computer?" "yes"; then
HOST="127.0.0.1"
else
HOST="0.0.0.0"
fi
PORT="$(prompt_text "Web UI port" "${PORT}")"
DAGU_INSTALL_DIR="$(prompt_text "Install directory" "${DAGU_INSTALL_DIR}")"
INSTALL_PATH="${DAGU_INSTALL_DIR}/dagu"
if [[ "$SERVICE_MODE" == "yes" ]]; then
DAGU_HOME_DIR="$(prompt_text "Dagu data directory" "${DAGU_HOME_DIR}")"
ADMIN_USERNAME="$(prompt_text "Initial admin username" "${ADMIN_USERNAME:-admin}")"
if [[ -z "$ADMIN_PASSWORD" ]]; then
ADMIN_PASSWORD="$(prompt_secret_confirm "Initial admin password")"
fi
if [[ "${#ADMIN_PASSWORD}" -lt 8 ]]; then
ui_error "The admin password must be at least 8 characters."
exit 1
fi
fi
if [[ ${#EXPLICIT_SKILL_DIRS[@]} -eq 0 ]]; then
if [[ "$SKILL_DETECTED_COUNT" -gt 0 ]]; then
if prompt_yes_no "Install the Dagu AI skill into detected AI tools?" "yes"; then
SKILL_MODE="auto"
else
SKILL_MODE="skip"
fi
elif prompt_yes_no "Install the Dagu AI skill into a custom skills directory?" "no"; then
answer="$(prompt_text "Skills directory" "${HOME}/.agents/skills")"
if [[ -n "$answer" ]]; then
EXPLICIT_SKILL_DIRS+=("$answer")
SKILL_MODE="explicit"
fi
else
SKILL_MODE="skip"
fi
fi
}
show_install_plan() {
ui_section "Install plan"
ui_kv "Version" "${VERSION}"
ui_kv "OS" "${OS}"
ui_kv "Architecture" "${ARCH}"