#!/sbin/openrc-run

name="ohlcdb-web"
description="PostgREST serving the ohlcdb API to the page"

# A copy of its own, under the package's own directory: the package carries
# the binary rather than depending on one, and collides with nothing.
command="/usr/lib/ohlcdb-web/postgrest"
# Not the file in /etc but the copy made from it at start, with the
# variables it names filled in.
_rendered="/run/$RC_SVCNAME/postgrest.conf"
command_args="$_rendered"
command_user="ohlcdb-web:ohlcdb-web"
command_background=true
pidfile="/run/$RC_SVCNAME.pid"
output_log="/var/log/ohlcdb-web/postgrest.log"
error_log="$output_log"

depend() {
	need net
}

start_pre() {
	checkpath -d -o "ohlcdb-web:ohlcdb-web" -m 0750 "/var/log/ohlcdb-web"
	checkpath -d -o "root:ohlcdb-web" -m 0750 "/run/$RC_SVCNAME"
	# Started with a variable of the settings still empty, PostgREST would
	# sit there failing to connect; the renderer names the one to fill in
	# and the service does not start.
	/usr/lib/ohlcdb-web/render /etc/ohlcdb-web/postgrest.conf "$_rendered"
}

stop_post() {
	rm -f "$_rendered"
}
