#!/sbin/openrc-run

description="System monitor recording daemon"

command="/usr/bin/below"
command_args="${cfgfile:+"--config $cfgfile"} record $command_args"
command_background="yes"
pidfile="/run/$RC_SVCNAME.pid"

depend() {
	need cgroups
	after ntp-client
}

start_pre() {
	if [ "$rc_cgroup_mode" != "unified" ]; then
		eerror "below requires rc_cgroup_mode=unified, but current is '$rc_cgroup_mode' (see /etc/rc.conf)"
		return 1
	fi

	command_args="$command_args
		$(togif --collect-io-stat "$collect_io_stat")
		$(togif --compress "$compress")
		$(optif --dict-compress-chunk-size "$dict_compress_chunk_size")
		$(togif --disable-disk-stat "$disable_disk_stat")
		$(togif --disable-exitstats "$disable_exitstats")
		$(optif --interval-s "$interval")
		$(optif --port "$port")
		$(optif --retain-for-s "$retain_for")
		$(optif --service-identity "$service_identity")
		$(optif --skew-detection-threshold-ms "$skew_detection_threshold")
		$(optif --store-size-limit "$store_size_limit")
		"
	return 0
}

optif() {
	test -n "$2" && printf '%s\n' "$1 $2"
}

togif() {
	yesno "$2" && printf '%s\n' "$1"
}
