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

NEO4J_EXEC="/opt/bin/neo4j"
NEO4J_CONF="${NEO4J_CONF:-/etc/neo4j-community}"
NEO4J_DATA="${NEO4J_DATA:-"/var/lib/neo4j-community/data"}"
NEO4J_LOGS="${NEO4J_LOGS:-"/var/log/neo4j-community"}"
NEO4J_PIDDIR="/run/${RC_SVCNAME}"
NEO4J_PIDFILE="${NEO4J_PIDDIR}/neo4j.pid"

USER="nobody"
GROUP="neo4j"

export JAVA_CMD NEO4J_CONF NEO4J_DATA NEO4J_LOGS NEO4J_PLUGINS NEO4J_PIDFILE

checkconfig() {
	if [ -z ${JAVA_CMD} ]; then
		eerror "Please use Oracle(R) Java(TM) 11, OpenJDK(TM) 11 to run Neo4j"
		eerror "and modify your /etc/conf.d/neo4j-daemon:"
		eerror "    JAVA_CMD=\"/opt/openjdk-bin-<openjdk_version>/bin/java\""
		return 1
	fi

	if [ ! -d ${NEO4J_PIDDIR} ]; then
		checkpath -q -d -o ${USER}:${GROUP} -m 0755 ${NEO4J_PIDDIR} || return 1
	fi
}

start() {
	checkconfig || return 1

	ebegin "Starting ${RC_SVCNAME}"
	start-stop-daemon --start --pidfile "${NEO4J_PIDFILE}" --user "${USER}" \
		--quiet --exec "${NEO4J_EXEC}" -- start
	eend ${?}
}

stop() {
	ebegin "Stopping ${RC_SVCNAME}"
	mark_service_stopping
	${NEO4J_EXEC} stop
	eend ${?} && mark_service_stopped
}

# vim: set ft=gentoo-init-d ts=4 :
