#!/bin/sh set -eu : "${HERDR_CONNECT_BACKEND_URL:?Set HERDR_CONNECT_BACKEND_URL}" : "${HERDR_CONNECT_ENROLLMENT_GRANT:?Generate an enrollment command in the dashboard}" case "$HERDR_CONNECT_BACKEND_URL" in https://*|http://localhost:*|http://127.0.0.1:*) ;; *) echo 'Use HTTPS, or localhost for development.' >&2; exit 1 ;; esac command -v node >/dev/null command -v herdr >/dev/null node -e 'const [major,minor]=process.versions.node.split(".").map(Number); if(major<22||(major===22&&minor<13)) process.exit(1)' || { echo 'Install Node 22.13+ or 24 LTS first.' >&2; exit 1; } hc_directory="${HERDR_CONNECT_PLUGIN_DIRECTORY:-${XDG_CONFIG_HOME:-$HOME/.config}/herdr-connect/plugin}" mkdir -p "$hc_directory" chmod 700 "$hc_directory" for hc_file in remote.mjs remote-lifecycle.mjs remote.sha256; do curl --fail --silent --show-error "${HERDR_CONNECT_BACKEND_URL%/}/downloads/$hc_file" -o "$hc_directory/$hc_file" done node --input-type=module - "$hc_directory" <<'JS' import {readFile} from 'node:fs/promises'; import {createHash} from 'node:crypto'; const dir=process.argv[2]; const expected=(await readFile(`${dir}/remote.sha256`,'utf8')).trim(); const actual=createHash('sha256').update(await readFile(`${dir}/remote.mjs`)).digest('hex'); if(expected!==actual) throw new Error('Plugin download checksum did not match'); JS hc_lock="${XDG_CONFIG_HOME:-$HOME/.config}/herdr-connect/remote-helper.lock" if [ -d "$hc_lock" ]; then node "$hc_directory/remote-lifecycle.mjs" stop hc_attempt=0 while [ -d "$hc_lock" ] && [ "$hc_attempt" -lt 7 ]; do sleep 1; hc_attempt=$((hc_attempt+1)); done [ ! -d "$hc_lock" ] || { echo 'Existing helper has not released its lock yet.' >&2; exit 1; } fi node "$hc_directory/remote.mjs" enroll --server "$HERDR_CONNECT_BACKEND_URL" --name "${HERDR_CONNECT_MACHINE_NAME:-$(hostname)}" unset HERDR_CONNECT_ENROLLMENT_GRANT cat > "$hc_directory/herdr-plugin.toml" <<'TOML' id = "herdr-connect.remote" name = "Herdr Connect Remote" version = "0.1.0" min_herdr_version = "0.9.3" description = "Connect this Herdr session to your hosted account" platforms = ["linux", "macos", "windows"] [[actions]] id = "start" title = "Connect to Herdr Connect" command = ["node", "remote-lifecycle.mjs", "start"] [[actions]] id = "status" title = "Remote helper status" command = ["node", "remote-lifecycle.mjs", "status"] [[actions]] id = "stop" title = "Disconnect remote helper" command = ["node", "remote-lifecycle.mjs", "stop"] TOML herdr plugin link "$hc_directory/herdr-plugin.toml" node "$hc_directory/remote-lifecycle.mjs" start echo 'Installed and registered. Check your dashboard. Use the plugin start action after a reboot.'