#!/bin/bash
#
# /etc/rc.d/init.d/groonga-server-gqtp
#
# chkconfig: - 80 20
# description: A full-text search engine and column store.
# processname: groonga
# config: /etc/sysconfig/groonga-server-gqtp
# pidfile: /var/run/groonga/groonga-gqtp.pid
#
### BEGIN INIT INFO
# Provides:          groonga-server-gqtp
# Default-Stop:      0 1 6
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Short-Description: groonga's init script
# Description:       groonga is a full-text search engine and column store.
### END INIT INFO

# Source function library.
. /etc/init.d/functions

name="groonga-server-gqtp"
prog="groonga"
groonga=/usr/bin/$prog
curl=/usr/bin/curl
USER=groonga
GROUP=groonga
DATABASE=/var/lib/groonga/db/db
ADDRESS=127.0.0.1
PORT=10043
PIDFILE=/var/run/groonga/$prog-gqtp.pid
PROTOCOL=gqtp
LOG_PATH=/var/log/groonga/groonga-gqtp.log
QUERY_LOG_PATH=/var/log/groonga/query-gqtp.log
OPTION_ARGS=""
GRN_QUERY_EXPANDER_TSV_SYNONYMS_FILE=/usr/share/groonga/synonyms.tsv

if [ -f /etc/sysconfig/$name ]; then
	. /etc/sysconfig/$name
fi

DAEMON_ARGS=""
GROONGA_ARGS="-d"

if [ -n "${USER}" ]; then
	if ! getent passwd | grep -q "^${USER}:"; then
		echo "$0: user for running $prog doesn't exist: ${USER}" >&2
		exit 1
	fi
	if [ -n "${PIDFILE}" ]; then
		mkdir -p `dirname ${PIDFILE}`
		chown -R ${USER} `dirname ${PIDFILE}`
	fi
	DAEMON_ARGS="${DAEMON_ARGS} --user ${USER}"
fi

if [ -n "${PIDFILE}" ]; then
	mkdir -p $(dirname ${PIDFILE})
	if [ -n "${USER}" ]; then
		chown -R ${USER} $(dirname ${PIDFILE})
	fi
	GROONGA_ARGS="${GROONGA_ARGS} --pid-path ${PIDFILE}"
fi
if [ -n "${ADDRESS}" ]; then
	GROONGA_ARGS="${GROONGA_ARGS} --bind-address ${ADDRESS}"
fi
if [ -n "${PORT}" ]; then
	GROONGA_ARGS="${GROONGA_ARGS} --port ${PORT}"
fi
if [ -n "${LOG_PATH}" ]; then
	mkdir -p $(dirname ${LOG_PATH})
	if [ -n "${USER}" ]; then
		chown -R ${USER} $(dirname ${LOG_PATH})
	fi
	GROONGA_ARGS="${GROONGA_ARGS} --log-path ${LOG_PATH}"
fi
if [ -n "${QUERY_LOG_PATH}" ]; then
	mkdir -p $(dirname ${QUERY_LOG_PATH})
	if [ -n "${USER}" ]; then
		chown -R ${USER} $(dirname ${QUERY_LOG_PATH})
	fi
	GROONGA_ARGS="${GROONGA_ARGS} --query-log-path ${QUERY_LOG_PATH}"
fi
if [ -n "${PROTOCOL}" ]; then
	GROONGA_ARGS="${GROONGA_ARGS} --protocol ${PROTOCOL}"
fi
GROONGA_ARGS="${GROONGA_ARGS} ${OPTION_ARGS}"

if [ -z "${DATABASE}" ]; then
	echo "$0: DATABASE should not be empty" >&2
	exit 1
fi

if [ -f "${DATABASE}" ]; then
	GROONGA_ARGS="${GROONGA_ARGS} ${DATABASE}"
else
	mkdir -p $(dirname ${DATABASE})
	if [ -n "${USER}" ]; then
		chown -R ${USER} $(dirname ${DATABASE})
	fi
	if [ -n "${GROUP}" ]; then
		chgrp -R ${GROUP} $(dirname ${DATABASE})
	fi
	GROONGA_ARGS="${GROONGA_ARGS} -n ${DATABASE}"
fi

RETVAL=0

send_command() {
	command=$1
	$groonga --port $PORT -c $ADDRESS ${command}
}

start() {
	echo -n "Starting $name: "
	daemon $DAEMON_ARGS $groonga "$GROONGA_ARGS"
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$name
	return $RETVAL
}

stop() {
	echo -n "Shutting down $name: "
	send_command shutdown
	killproc -p $PIDFILE $prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$name
	return $RETVAL
}

restart() {
	stop
	start
}

status() {
	send_command status
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	restart
	;;
    reload)
	killproc -p $PIDFILE $groonga -HUP
	;;
    condrestart)
	[ -f /var/lock/subsys/$name ] && restart || :
	;;
    status)
	status
	;;
    *)
	echo "Usage: $prog {start|stop|reload|restart|condrestart|status}"
	exit 1
	;;
esac
exit $?
