# $Id: Portfile,v 1.59 2006/02/14 07:32:40 mww Exp $

PortSystem 1.0

name			postgresql
version			7.4.12
categories		databases
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/
platforms		darwin
set subdir		source/v${portversion}
master_sites	postgresql:source/v${version}

checksums		sha1 fd4e1db4fa3bf1a22d0596337cb1456389ca03e0
patchfiles		patch-PostgreSQL.darwin
use_bzip2		yes

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

configure.env	CPPFLAGS="-I${prefix}/include" \
				CFLAGS="-I${prefix}/include" \
				LDFLAGS="-L${prefix}/lib"

configure.args	--docdir=${prefix}/share/doc/${name}-${version} \
				--datadir=${prefix}/share/${name}-${version} \
				--sysconfdir=${prefix}/etc/pgsql \
				--mandir=${prefix}/share/man \
				--with-openssl=${prefix} \
				--with-zlib \
				--with-rendezvous \
				--enable-thread-safety

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}${prefix}/bin
	# install tsearch2
	system "cd ${worksrcpath}/contrib/tsearch2 && ${build.cmd} install ${destroot.destdir}"
}

# Choose a reasonable default for most platforms
set tclconfig	${prefix}/lib
set tclsh		${prefix}/bin/tclsh8.4

# Default dir for database itself
set dbdir	${prefix}/var/db/pgsql/defaultdb

platform darwin 7 {
	global tclconfig tclsh

	set tclconfig /System/Library/Frameworks/Tcl.framework
	set tclsh /usr/bin/tclsh8.4
}

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

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	{
	global tclconfig tclsh

	configure.args-append	--with-tcl \
				--with-tclconfig=${tclconfig} \
				--with-tkconfig=$prefix/lib
	configure.env-append	TCLSH=${tclsh}
	depends_lib-append	lib:tcl8.4:tcl \
				lib:tk8.4:tk
}

variant server {
	pre-destroot {
		# install startup-item
		set startupdir ${destroot}/Library/StartupItems/PostgreSQL7
		set contrib ${worksrcpath}/contrib/start-scripts
		xinstall -m 755 -d ${startupdir} ${destroot}${dbdir} \
			${destroot}${prefix}/var/log/pgsql \
			${destroot}${prefix}/var/db/pgsql
		xinstall -m 755 ${contrib}/PostgreSQL.darwin ${startupdir}/PostgreSQL7
		xinstall -m 644 ${contrib}/StartupParameters.plist.darwin \
			${startupdir}/StartupParameters.plist
		reinplace "s|__PREFIX__|${prefix}|g" ${startupdir}/PostgreSQL7
		# Add User postgres:postgres
		addgroup postgres
		set gid [existsgroup postgres]
		adduser postgres shell=/bin/sh gid=${gid} \
			home=${prefix}/var/db/pgsql \
			realname=PostgreSQL\ 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 postgres -owner postgres
		}
	}

	post-install {
		ui_msg "\nTo create a database instance, after install do\n\
			sudo su postgres -c \'${prefix}/bin/initdb -D ${dbdir}\'"
		ui_msg "\nTo activate the PostgreSQL7 server, add\n\
			POSTGRESQL7=-YES-\n\
			to your ${prefix}/etc/rc.local"
	}
}
