#!/usr/bin/openrc-run
# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

PDNS_CONFIGDIR="${PDNS_CONFIGDIR:-/etc/powerdns}"

PDNS_CONFIG="${PDNS_CONFIGDIR}/recursor.conf"
PDNS_CHROOTDIR="$( awk -F = '$1 == "chroot" { print $2 }' "${PDNS_CONFIG}" )"
PDNS_SOCKETDIR="$( awk -F = '$1 == "socket-dir" { print $2 }' "${PDNS_CONFIG}" )"
[ -z "${PDNS_SOCKETDIR}" -a -z "${PDNS_CHROOTDIR}" ] && PDNS_SOCKETDIR="/run/pdns-recursor"

name="PowerDNS Recursor"
description="Recursive name server"

extra_started_commands="ping"
description_ping="Ping the PowerDNS instance"

supervisor=supervise-daemon
command="/usr/bin/pdns_recursor"
command_args="--config-dir=${PDNS_CONFIGDIR} --daemon=no"

control_command="/usr/bin/rec_control"
control_command_args="${command_args}"

depend() {
	need net
}

start_pre() {
	checkpath --directory --mode 750 "${PDNS_CHROOTDIR}/${PDNS_SOCKETDIR}"
}

ping() {
	ebegin "Pinging ${name}"
	${control_command} ${control_command_args} ping >/dev/null 2>&1
	eend $?
}
