# $Id: Portfile,v 1.1 2006/03/02 22:01:46 mww Exp $

PortSystem 1.0

name			postgresql7
version			7.4.12
categories		databases
platforms		darwin
maintainers		mww@opendarwin.org
description		The most advanced open-source database available anywhere
long_description PostgreSQL is Object-Relational Database Management System. \
				It supports almost all SQL constructs, including transactions, \
				subselects, and user-defined types and functions. Write-ahead \
				logging means increased performance while retaining data \
				integrity.

homepage		http://www.postgresql.org/
master_sites	postgresql:source/v${version}

set dbase		postgresql-base-${version}.tar.bz2
set dopt		postgresql-opt-${version}.tar.bz2
set dtest		postgresql-test-${version}.tar.bz2
distfiles		${dbase} ${dopt} ${dtest}
checksums		${dbase} sha1 ae4b3f5b344e73855fcad4663897508e962b5cdc \
				${dopt} sha1 f549f88661903b8b09d2f856b83a7c971c728be1 \
				${dtest} sha1 846922094e26a725fe6d28d0231b480d4b89ada6
use_bzip2		yes

depends_lib		port:readline \
				port:openssl \
				port:zlib \
				port:bison

worksrcdir		postgresql-${version}

set libdir		${prefix}/lib/pgsql7
configure.args	--sysconfdir=${prefix}/etc/pgsql7 \
				--bindir=${libdir}/bin \
				--libdir=${libdir} \
				--includedir=${prefix}/include/pgsql7 \
				--datadir=${prefix}/share/${name} \
				--mandir=${prefix}/share/man \
				--docdir=${prefix}/share/doc/${name} \
				--with-docdir=${prefix}/share/doc/${name} \
				--with-includes=${prefix}/include \
				--with-libraries=${prefix}/lib \
				--with-openssl=${prefix} \
				--with-rendezvous \
				--with-readline \
				--with-zlib \
				--enable-thread-safety \
				--enable-integer-datetimes \
				--enable-multibyte \
				--enable-unicode
configure.env	CPPFLAGS="-I${prefix}/include" \
				CFLAGS="-I${prefix}/include" \
				LDFLAGS="-L${prefix}/lib"

build.type		gnu

post-build	{
	# build autovacuum
	system "cd ${worksrcpath}/contrib/pg_autovacuum && ${build.cmd}"
	# build tsearch2
	system "cd ${worksrcpath}/contrib/tsearch2 && ${build.cmd}"
}

test.run		yes
test.target		check

# install headers too
destroot.target-append  install-all-headers
post-destroot	{
	# install autovacuum
	xinstall -m 755 -s ${worksrcpath}/contrib/pg_autovacuum/pg_autovacuum \
		${destroot}${libdir}/bin
	# install tsearch2
	system "cd ${worksrcpath}/contrib/tsearch2 && ${build.cmd} install ${destroot.destdir}"
	# symlinks
	system "cd ${destroot}${prefix}/bin && ln -sf ${libdir}/bin/psql psql7"
	# nuke nonsense
	system "cd ${destroot}${prefix}/share && rm -rf doc"
}

# Defaults for database
set dbdir	${prefix}/var/db/pgsql7/defaultdb
set logdir	${prefix}/var/log/pgsql7
set dbuser	postgres7
set dbgrp	postgres

variant perl	{
	configure.args-append	--with-perl
	depends_lib-append	lib:perl:perl5.8
}

variant java	{
	configure.args-append	--with-java
	depends_build		port:apache-ant
	depends_lib-append	bin:java:kaffe
	configure.env-append	ANT=${prefix}/share/java/apache-ant/bin/ant
}

variant tcltk	{
	configure.args-append	--with-tcl \
				--with-tclconfig=${prefix}/lib \
				--with-tkconfig=${prefix}/lib
	configure.env-append	TCLSH=${prefix}/bin/tclsh
	depends_lib-append	port:tcl port:tk
}

variant server {
	startupitem.create	yes
	startupitem.init	\
"PGCTL=${libdir}/bin/pg_ctl"
	startupitem.start	\
"su ${dbuser} -c \"\${PGCTL} -D \${POSTGRESQL7DATA:=${dbdir}} start -l ${logdir}/postgres.log\""
	startupitem.stop	\
"su ${dbuser} -c \"\${PGCTL} -D \${POSTGRESQL8DATA:=${dbdir}} stop -s -m fast\""

	pre-destroot {
		xinstall -m 755 -d ${destroot}${dbdir} \
			${destroot}${prefix}/var/log/pgsql \
			${destroot}${prefix}/var/db/pgsql
		# Add User postgres:postgres
		addgroup ${dbuser}
		set gid [existsgroup ${dbgrp}]
		adduser ${dbuser} shell=/bin/sh gid=${gid} \
			home=${prefix}/var/db/pgsql7 \
			realname=PostgreSQL-7\ Server
	}

	post-destroot {
		destroot.keepdirs ${destroot}${prefix}/var/db/pgsql
		system "cd ${destroot}${prefix}/var/log/pgsql && \
			touch pgsql.log pg_autovacuum.log"

		foreach d { db/pgsql log/pgsql log/pgsql/pgsql.log log/pgsql/pg_autovacuum.log } {
			file attributes ${destroot}${prefix}/var/${d} \
				-group ${dbgr} -owner ${dbuser}
		}
	}

	post-install {
		ui_msg "\nTo create a database instance, after install do\n\
			sudo su ${dbuser} -c \'${libdir}/bin/initdb -D ${dbdir}\'"
	}
}

platform darwin 8 {
	configure.env-append	CC=/usr/bin/gcc-4.0 CPP=/usr/bin/cpp-4.0 CXX=/usr/bin/g++-4.0
}

