#!/bin/sh
#
# $FreeBSD: textproc/sphinxsearch/files/sphinxsearch.in 302141 2012-08-05 23:19:36Z dougb $
#
# PROVIDE: sphinxsearch
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable sphinxsearch:
#
# sphinxsearch_enable="YES"
#
# Other rc.conf variables:
#
# sphinxsearch_conffile="/usr/local/etc/sphinx.conf"
#                          -- path to config file
# sphinxsearch_pidfile="/var/run/sphinxsearch/searchd.pid"
#                          -- location of pidfile: must match setting
#                             in ${sphinxsearch_conffile}
# sphinxsearch_user="_sphinx"
#                          -- user to run searchd as
# sphinxsearch_group="_sphinx"
#                          -- group to run searchd as
# sphinxsearch_logdir="/var/log/sphinxsearch"
#                          -- directory searchd writes logs to
# sphinxsearch_dir="/var/db/sphinxsearch"
#                          -- working directory used by searchd et al:
#                             must match setting in ${sphinxsearch_conffile}
#
. /etc/rc.subr

name=sphinxsearch
rcvar=sphinxsearch_enable

load_rc_config ${name}

: ${sphinxsearch_enable="NO"}
: ${sphinxsearch_conffile="/usr/local/etc/sphinx.conf"}
: ${sphinxsearch_pidfile="/var/run/sphinxsearch/searchd.pid"}
: ${sphinxsearch_user="_sphinx"}
: ${sphinxsearch_group="_sphinx"}
: ${sphinxsearch_logdir="/var/log/sphinxsearch"}
: ${sphinxsearch_dir="/var/db/sphinxsearch"}

command=/usr/local/sbin/searchd
pidfile=${sphinxsearch_pidfile}
required_files=${sphinxsearch_conffile}
start_precmd=sphinxsearch_prestart

sphinxsearch_prestart ()
{
    piddir=$(dirname ${sphinxsearch_pidfile})
    if [ ! -d ${piddir} ]; then
	mkdir -m 755 -p ${piddir}
	chown -R ${sphinxsearch_user}:${sphinxsearch_group} ${piddir}
    fi
    if [ ! -d ${sphinxsearch_logdir} ]; then
	mkdir -m 755 -p ${sphinxsearch_logdir}
	chown -R ${sphinxsearch_user}:${sphinxsearch_group} \
	    ${sphinxsearch_logdir}
    fi
    if [ ! -d ${sphinxsearch_dir} ]; then
	mkdir -m 755 -p ${sphinxsearch_dir} ${sphinxsearch_dir}/data
	chown -R ${sphinxsearch_user}:${sphinxsearch_group} \
	    ${sphinxsearch_dir}
    fi
}

case "${sphinxsearch_flags}" in
    *--config\ *)
    echo "Warning \$sphinxsearch_flags includes --config option." \
	"Please use \$sphinxsearch_conffile instead."
    ;;
    *)
    sphinxsearch_flags="--config ${sphinxsearch_conffile} ${sphinxsearch_flags}"
    ;;
esac

run_rc_command "$1"
