#!/bin/sh
#
# $NetBSD: solr.sh,v 1.1 2020/06/09 22:20:01 jym Exp $
#
# Apache Solr daemon
# 
# PROVIDE: solr
# REQUIRE: LOGIN
# 
# You will need to set some variables in /etc/rc.conf to start Solr:
# 
# solr=YES
# solr_port=8983	# Solr binding port number;
# 			#   this variable is optional and defaults to "8983".
#

. /etc/rc.subr

name="solr"
rcvar=$name
command="/usr/pkg/bin/solr"
pidfile="/var/run/solr/solr-${solr_port}.pid"
solr_user="solr"
solr_group="solr"
solr_home="/usr/pkg/solr"
required_files="/usr/pkg/etc/solr/solr.in.sh /usr/pkg/etc/solr/solr.xml"

start_precmd="solr_precmd"
start_cmd="solr_start"
status_cmd="solr_status"
stop_cmd="solr_stop"

solr_precmd()
{
	# Solr defaults are often too low for most OSes (especially NetBSD)
	ulimit -m unlimited
	ulimit -n 65000
	ulimit -p 65000

	# Solr insists on having its xml file accessible from its
	# HOME db directory 
	if [ \! -e ${solr_home}/solr.xml ]; then
		ln -sf /usr/pkg/etc/solr/solr.xml ${solr_home}/solr.xml
	fi
}

solr_start()
{
	/usr/bin/su -m ${solr_user} -c "${command} start ${command_args}"
}

solr_status()
{
	/usr/bin/su -m ${solr_user} -c "${command} status ${command_args}"
}

solr_stop()
{
	rc_pid=$(check_pidfile $pidfile "${JAVA_HOME}/bin/java")
	/usr/bin/su -m ${solr_user} -c "${command} stop ${command_args}"
	wait_for_pids $rc_pid
}

load_rc_config "$name"

if [ -z "${solr_port}" ]; then
	solr_port="8983"
fi

if [ -z "${JAVA_HOME}" ]; then
	JAVA_HOME="/usr/pkg/java/openjdk17"
	export JAVA_HOME
fi

command_args="-p ${solr_port}"

run_rc_command "$1"
