#!/bin/sh
# diy-agentd, the DIY OS Agent daemon (50.35-T17): the launcher an installed copy runs.
#
# `install.sh` copies this to ~/.local/share/diy-platform/tools/bin/diy-agentd, beside the daemon it starts
# (../agentd/bin/diy-agentd.mjs, with its own production node_modules and the workflow engine), and links it to
# ~/.local/bin/diy-agentd. The daemon is TypeScript that Node runs directly (type stripping, Node 22.19 or newer), so there is
# nothing to build: this only finds the daemon from wherever the link points, checks Node, and starts it.
#
#   diy-agentd [-V|--version] [-h|--help]      everything else is the environment (DIY_AGENTS_ROOT, DIY_AGENTD_PORT) and config.json
here=$(dirname "$(readlink -f "$0")")
entry=$here/../agentd/bin/diy-agentd.mjs
[ -f "$entry" ] || { echo "diy-agentd: no daemon at $entry (this launcher belongs beside tools/agentd: run install.sh --tools-only)" >&2; exit 1; }
command -v node >/dev/null 2>&1 || { echo "diy-agentd: node is required (22.19 or newer)" >&2; exit 1; }
node -e 'const [major, minor] = process.versions.node.split(".").map(Number); process.exit(major > 22 || (major === 22 && minor >= 19) ? 0 : 1)' \
  || { echo "diy-agentd: node $(node -p process.versions.node) is too old to run the daemon's TypeScript (22.19 or newer)" >&2; exit 1; }
# ExperimentalWarning: node:sqlite (still flagged experimental in Node 22 and 24) would open every start in the journal.
exec node --disable-warning=ExperimentalWarning "$entry" "$@"
