#!/bin/sh
# Enable or disable Logthread Agent profiles.  logthread-agent-profile list|status|enable|disable ...
# Profiles are YAML fragments in /usr/share/logthread-agent/profiles. Enabling one links it into
# /etc/logthread-agent/conf.d (loaded in name order, after 00-base.yaml). Your own fragments in
# conf.d (for example 90-local.yaml) are never touched.
set -eu
PROFILES=${LOGTHREAD_PROFILE_DIR:-/usr/share/logthread-agent/profiles}
CONF=${LOGTHREAD_CONF_DIR:-/etc/logthread-agent/conf.d}
ENVFILE=${LOGTHREAD_ENV_FILE:-/etc/logthread-agent/agent.env}
BIN=${LOGTHREAD_AGENT_BIN:-/usr/lib/logthread-agent/logthread-agent}
DROPIN=/etc/systemd/system/logthread-agent.service.d
OBI=${LOGTHREAD_AUTOTRACE_BIN:-/usr/lib/logthread-agent/autotrace/obi}
BTF=${LOGTHREAD_BTF_PATH:-/sys/kernel/btf/vmlinux}

die() { echo "logthread-agent-profile: $*" >&2; exit 1; }
usable() { echo "$1" | grep -Eq '^[a-z0-9][a-z0-9-]*$'; }

resolve() { # linux-host picks journald or syslog
  if [ "$1" = linux-host ]; then
    if command -v journalctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then echo linux-host; else echo linux-host-syslog; fi
  else echo "$1"; fi
}

list() {
  for f in "$PROFILES"/*.yaml; do
    n=$(basename "$f" .yaml); [ "$n" = base ] && continue
    case "$n" in linux-host-syslog) continue;; esac
    if [ -e "$CONF/10-$n.yaml" ]; then echo "* $n"; else echo "  $n"; fi
  done
}

validate() {
  [ -x "$BIN" ] || return 0
  set -- "$BIN" validate
  for f in "$CONF"/*.yaml; do set -- "$@" "--config=file:$f"; done
  ( set -a; [ -r "$ENVFILE" ] && . "$ENVFILE"; set +a
    LOGTHREAD_INGEST_KEY=${LOGTHREAD_INGEST_KEY:-validate-only} "$@" ) >/tmp/logthread-agent-validate.$$ 2>&1 || { cat /tmp/logthread-agent-validate.$$ >&2; rm -f /tmp/logthread-agent-validate.$$; return 1; }
  rm -f /tmp/logthread-agent-validate.$$
}

dropin() { # the docker profile needs root: /var/lib/docker/containers is not readable otherwise
  if [ -e "$CONF/10-docker.yaml" ]; then
    mkdir -p "$DROPIN"; printf '[Service]\nUser=root\nGroup=root\n' > "$DROPIN/10-docker.conf"
  else rm -f "$DROPIN/10-docker.conf"; rmdir "$DROPIN" 2>/dev/null || true; fi
  command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ] && systemctl daemon-reload || true
}

systemd() { command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; }

can_autotrace() { # autotrace runs OBI (eBPF): Linux 5.8+ with BTF and the add-on binary
  [ -x "$OBI" ] || die "autotrace is not installed. Re-run install.sh with --profiles ...,autotrace (it downloads the add-on)"
  [ -r "$BTF" ] || die "autotrace needs a kernel with BTF ($BTF is missing): Linux 5.8+ or RHEL 8+"
}

autotrace() { # the OBI service follows the profile
  systemd || return 0
  if [ -e "$CONF/10-autotrace.yaml" ]; then
    systemctl enable logthread-agent-autotrace >/dev/null 2>&1 || true
    systemctl restart logthread-agent-autotrace && echo "restarted logthread-agent-autotrace"
  else
    systemctl disable --now logthread-agent-autotrace >/dev/null 2>&1 || true
  fi
}

apply() {
  validate || die "the resulting configuration is invalid; nothing changed"
  dropin
  if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ] && systemctl is-active --quiet logthread-agent; then
    systemctl restart logthread-agent && echo "restarted logthread-agent"
  fi
  autotrace
}

cmd=${1:-list}; [ $# -gt 0 ] && shift || true
case "$cmd" in
  list|status) list ;;
  enable)
    [ $# -gt 0 ] || die "usage: enable <profile>..."
    mkdir -p "$CONF"; [ -e "$CONF/00-base.yaml" ] || ln -sf "$PROFILES/base.yaml" "$CONF/00-base.yaml"
    for p in "$@"; do
      usable "$p" || die "invalid profile name: $p"
      real=$(resolve "$p"); [ -f "$PROFILES/$real.yaml" ] || die "unknown profile: $p (try: list)"
      [ "$p" != autotrace ] || can_autotrace
      rm -f "$CONF/10-$p.yaml"
      ln -sf "$PROFILES/$real.yaml" "$CONF/10-$p.yaml"; echo "enabled $p"
    done; apply ;;
  disable)
    [ $# -gt 0 ] || die "usage: disable <profile>..."
    for p in "$@"; do usable "$p" || die "invalid profile name: $p"; rm -f "$CONF/10-$p.yaml"; echo "disabled $p"; done; apply ;;
  set) # set <profile>...: exactly these profiles
    for f in "$CONF"/10-*.yaml; do if [ -L "$f" ]; then rm -f "$f"; fi; done
    if [ $# -gt 0 ]; then exec "$0" enable "$@"; fi
    apply ;;
  *) die "usage: $0 list|enable|disable|set <profile>..." ;;
esac
