#!/sbin/openrc-run

extra_started_commands="reload"

description="Prometheus exporter for PostgreSQL"
description_reload="Reload configuration"

: ${command_user:="pgexporter"}
: ${cfgfile:="/etc/pgexporter/pgexporter.conf"}
: ${users_file:="/etc/pgexporter/pgexporter_users.conf"}
: ${rundir:="/run/$RC_SVCNAME"}

command="/usr/bin/pgexporter"
command_args="-c $cfgfile -u $users_file $command_args"
command_background=yes
pidfile="/run/$RC_SVCNAME.pid"

required_files="$cfgfile"

start_pre() {
	if ! [ -f "$users_file" ]; then
		eerror "$users_file does not exist!"
		eerror "You must first create it using pgexporter-admin(1)."
		return 1
	fi
	if [ "${collectors-}" ]; then
		collectors="$(printf '%s\n' $collectors | tr '\n' ',')"
		command_args="$command_args --collectors ${collectors%,}"
	fi
	checkpath -q -d -m750 -o "$command_user" "$rundir"
}

reload() {
	ebegin "Reloading $RC_SVCNAME"

	if [ "$supervisor" ]; then
		$supervisor "$RC_SVCNAME" --signal HUP
	else
		start-stop-daemon --signal HUP --pidfile "$pidfile"
	fi
	eend $?
}
