#! /bin/sh
#
# $NetBSD: pgbouncer.sh,v 1.3 2016/03/15 11:29:35 fhajny Exp $
#
# PROVIDE: pgbouncer
# REQUIRE: DAEMON
#

if [ -r /etc/rc.subr ]
then
	. /etc/rc.subr
fi

name="pgbouncer"
rcvar=${name}
command="/usr/pkg/bin/${name}"
pgbouncer_conf="/usr/pkg/etc/${name}.ini"
required_files="${pgbouncer_conf}"
command_args="-d ${pgbouncer_conf}"
extra_commands="reload"
pgsql_user="pgbounce"
start_cmd="pgbouncer_start"

pidfile="/var/run/${name}/${name}.pid"
if [ -r ${pgbouncer_conf} ] ; then
	pidfile=`grep -i pidfile ${pgbouncer_conf} | cut -d= -f2`
fi

pgbouncer_start()
{
        echo "Starting ${name}."
	piddir=`dirname ${pidfile}`
	if [ ! -d ${piddir} ] ; then
		/bin/mkdir -p -p ${piddir}
		/usr/sbin/chown ${pgsql_user} ${piddir}
	fi

        doit="/usr/bin/su -m ${pgsql_user} -c '${command} ${command_args}'"
        eval $doit
}

load_rc_config $name
run_rc_command "$1"
