#!/sbin/runscript

: ${PGLOG:=/var/log/postgresql.log}
extra_commands="reload"

depend()
{
	use net
	provide postgresql
}

start_pre()
{
	if [ ! -d "$PGDATA" ] ; then
		einfo "Creating PostgreSQL dbdir: ${PGDATA}"
		mkdir -p ${PGDATA} && \
			chown -R ${PGUSER}.${PGGROUP} ${PGDATA}
		einfo "Initializing PostgreSQL dbdir: ${PGDATA}"
		su -l ${PGUSER} -c "/usr/bin/initdb -D ${PGDATA}"
	fi
	if [ ! -e ${PGLOG} ]; then
		touch -f ${PGLOG}
		chown ${PGUSER} ${PGLOG}
	fi
}

start()
{
	ebegin "Starting PostgreSQL"
	su -l ${PGUSER} -c "/usr/bin/pg_ctl -D ${PGDATA} -l ${PGLOG} \
			-W start ${PGOPTS}"
	eend $?
}

stop()
{
	ebegin "Stopping PostgreSQL"
	su -l ${PGUSER} -c "/usr/bin/pg_ctl -D ${PGDATA} -l ${PGLOG} -w stop"
	eend $?
}

reload()
{
	ebegin "Reloading PostgreSQL configuration"
	su -l ${PGUSER} -c "/usr/bin/pg_ctl -D ${PGDATA} -l ${PGLOG} reload"
	eend $?
}
