#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BINARY="${RSMON_WORKER_BINARY:-${ROOT_DIR}/bin/rsmon-worker}" ENV_FILE="${RSMON_WORKER_ENV_FILE:-}" START_SERVICE=1 usage() { cat <<'USAGE' Usage: sudo ./scripts/install-systemd.sh [--binary PATH] [--env PATH] [--no-start] Installs rsmon-worker as /usr/local/bin/rsmon-worker and configures systemd. --env is required and must contain RSMON_URL and RSMON_TOKEN. USAGE } while [ "$#" -gt 0 ]; do case "$1" in --binary) BINARY="${2:?--binary requires a path}" shift 2 ;; --env) ENV_FILE="${2:?--env requires a path}" shift 2 ;; --no-start) START_SERVICE=0 shift ;; -h|--help) usage exit 0 ;; *) printf 'Unknown argument: %s\n' "$1" >&2 usage >&2 exit 2 ;; esac done if [ "$(id -u)" -ne 0 ]; then printf 'Run this installer as root.\n' >&2 exit 1 fi if [ ! -x "$BINARY" ]; then if command -v go >/dev/null 2>&1; then make -C "$ROOT_DIR" build else printf 'Worker binary not found at %s and Go is unavailable.\n' "$BINARY" >&2 exit 1 fi fi if [ -z "$ENV_FILE" ]; then printf '%s\n' '--env is required; use a mode-0600 file containing RSMON_URL and RSMON_TOKEN.' >&2 exit 2 fi ARGS=(install --binary "$BINARY" --env-file "$ENV_FILE") if [ "$START_SERVICE" -eq 0 ]; then ARGS+=(--no-start) fi exec "$BINARY" "${ARGS[@]}"