#!/bin/sh
#
# Runs the collectord build for this machine with this package's configuration, for any
# subcommand (sudo /opt/collectorforlinux/bin/collectord verify); a flag on the command line wins.

COLLECTORD_HOME="${COLLECTORD_HOME:-/opt/collectorforlinux}"

machine=$(uname -m)
case "$machine" in
x86_64) build=amd64 ;;
aarch64) build=aarch64 ;;
*)
	echo "collectord: no build for this architecture ($machine); the package has amd64 and aarch64 builds" >&2
	exit 1
	;;
esac

# 001-general.conf names the host ${HOSTNAME}, which sudo and systemd do not set (and
# hostname(1) is missing on minimal images): default it to the kernel's node name.
HOSTNAME="${HOSTNAME:-$(uname -n)}"
export HOSTNAME

COLLECTORD_ENVIRONMENT="${COLLECTORD_ENVIRONMENT:-linux}"
COLLECTORD_CONF="${COLLECTORD_CONF:-${COLLECTORD_HOME}/etc}"
COLLECTORD_ENV_OVERRIDE="${COLLECTORD_ENV_OVERRIDE:-COLLECTOR__}"
export COLLECTORD_HOME COLLECTORD_ENVIRONMENT COLLECTORD_CONF COLLECTORD_ENV_OVERRIDE

exec "${COLLECTORD_HOME}/bin/collectord-${build}" "$@"
