#!/bin/sh
#
# Created by: Mark Felder <feld@FreeBSD.org>

# PROVIDE: synapse
# REQUIRE: LOGIN postgresql
# KEYWORD: shutdown

#
# Add the following line to /etc/rc.conf to enable `synapse':
#
# synapse_enable="YES"

. /etc/rc.subr
name=synapse

rcvar=synapse_enable
load_rc_config ${name}

: ${synapse_enable:=NO}
: ${synapse_user:=%%USERS%%}
: ${synapse_conf:=%%ETCDIR%%/homeserver.yaml}
: ${synapse_dbdir:=%%DBDIR%%}
: ${synapse_logdir:=%%LOGDIR%%}
: ${synapse_pidfile:=%%PIDDIR%%/homeserver.pid}

pidfile="${synapse_pidfile}"
procname=%%PYTHON_CMD%%
command=%%PYTHON_CMD%%
command_args="-m synapse.app.homeserver --daemonize -c ${synapse_conf}"
start_precmd=start_precmd

start_precmd()
{
	if [ ! -d ${synapse_pidfile%/*} ] ; then
		install -d -o ${synapse_user} -g wheel ${synapse_pidfile%/*};
	fi

	if [ ! -d ${synapse_dbdir} ] ; then
		install -d -o ${synapse_user} -g wheel ${synapse_dbdir};
	fi

	if [ ! -d ${synapse_logdir} ] ; then
		install -d -o ${synapse_user} -g wheel ${synapse_logdir};
	fi
}

run_rc_command "$1"
