# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 92951 2012-05-11 18:33:30Z vince@macports.org $

PortSystem 1.0
PortGroup select 1.0

name			postgresql90
version			9.0.7
revision                2

categories		databases
platforms		darwin
maintainers		mww jwa
license			Permissive
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		http://ftp3.de.postgresql.org/pub/Mirrors/ftp.postgresql.org/source/v${version}/ \
			postgresql:source/v${version}/
distname		postgresql-${version}

checksums           rmd160  80a8c91233462515914aeb5c15cf63b7195da3ef \
                    sha256  dabe26d71d8b595f9514da1363dc20c3f4b90b6908026dd06984cac448a6d833

use_bzip2		yes

depends_lib		port:readline port:openssl port:zlib port:libxml2 port:libxslt port:ossp-uuid
depends_build		port:bison
depends_run		port:postgresql_select

worksrcdir		postgresql-${version}

patchfiles		src_pl_plpython_Makefile.patch

set libdir		${prefix}/lib/${name}
configure.args	--sysconfdir=${prefix}/etc/${name} \
				--bindir=${libdir}/bin \
				--libdir=${libdir} \
				--includedir=${prefix}/include/${name} \
				--datadir=${prefix}/share/${name} \
				--mandir=${prefix}/share/man \
				--with-includes=${prefix}/include \
				--with-libraries=${prefix}/lib \
				--with-openssl \
				--with-bonjour \
				--with-readline \
				--with-zlib \
				--with-libxml \
				--with-libxslt \
				--enable-thread-safety \
				--enable-integer-datetimes \
				--with-ossp-uuid
configure.cppflags-append	-I${prefix}/include/ossp

# building psql with clang causes segfault on query; see #31717
if {${configure.compiler} == "clang" && [vercmp ${xcodeversion} 4.4] < 0} {
    configure.compiler llvm-gcc-4.2
}

if {[variant_isset universal]} {
    configure.cflags-append	-DAC_APPLE_UNIVERSAL_BUILD
    post-configure {
        system "cd ${worksrcpath}/src/include \
                && ed - pg_config.h < ${filespath}/pg_config.h.ed \
                && touch stamp-h"
        system "cd ${worksrcpath}/src/interfaces/ecpg/include \
                && ed - ecpg_config.h < ${filespath}/ecpg_config.h.ed \
                && touch stamp-h"
    }
}

build.type		gnu
build.target

test.run		yes
test.target		check

livecheck.type	regex
livecheck.url	${homepage}
livecheck.regex	(9\\.0\\.\[.0-9\]+)

set contribs	"adminpack dblink fuzzystrmatch lo uuid-ossp pg_buffercache pg_trgm pgcrypto tsearch2 vacuumlo xml2"

post-build {
	foreach contrib ${contribs} {
		system "cd ${worksrcpath}/contrib/${contrib}/ && ${build.cmd}"
	}
}

post-destroot {
	foreach contrib ${contribs} {
		system "echo contrib: \"${contrib}\""
		system "cd ${worksrcpath}/contrib/${contrib}/ && \
			${destroot.cmd} install ${destroot.destdir} && touch DONE"
	}
	system "cd ${destroot}${prefix}/bin && ln -sf ${libdir}/bin/psql psql90"
	file delete -force ${destroot}${prefix}/share/doc/postgresql \
		${destroot}${prefix}/share/man
	
	reinplace -E "s#-arch\[\[:space:\]\]+\[\[:alnum:\]_\]+##g" \
	    ${destroot}${prefix}/lib/postgresql90/pgxs/src/Makefile.global
	
	reinplace -E "s#^CFLAGS =#CFLAGS +=#g" \
	    ${destroot}${prefix}/lib/postgresql90/pgxs/src/Makefile.global
	    
	reinplace -E "s#^(\[\[:space:\]\]+)LDFLAGS =#\\1LDFLAGS +=#g" \
	    ${destroot}${prefix}/lib/postgresql90/pgxs/src/Makefile.global
}

select.group    postgresql
select.file     ${filespath}/${name}

variant python description {add support for python} {
	depends_lib-append		port:python27
	configure.args-append	--with-python
	configure.python		${prefix}/bin/python2.7
}

variant krb5 description {add support for Kerberos 5 authentication} {
	configure.args-append	--with-krb5
}

variant perl description {add Perl support} {
	depends_lib-append			bin:perl:perl5
	configure.args-append		--with-perl
}

variant tcl description {add Tcl support} {
	depends_lib-append			port:tcl
	configure.args-append		--with-tcl --with-tclconfig=${prefix}/lib
	configure.env-append		TCLSH=${prefix}/bin/tclsh
}

post-install {
    ui_msg "\nTo use the postgresql server, install the ${name}-server port"
}

