# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 119378 2014-04-24 13:01:32Z vince@macports.org $

PortSystem          1.0
PortGroup           archcheck 1.0

name                postgis2
categories          databases gis
license             GPL
version             2.1.2
epoch               1
platforms           darwin
maintainers         vince openmaintainer

description         PostGIS is the spatial extension to the\
                    PostGreSQL database.

long_description    PostGIS 2 adds geometrical, geographical and topological\
                    types, and functions operating thereon, to the PostGreSQL\
                    database. The 2.0 release also provides raster handling\
                    and basic 3D geometry capacities (TIN and polyhedra).

homepage            http://postgis.refractions.net/

master_sites        http://download.osgeo.org/postgis/source
distname            postgis-${version}

checksums           rmd160  40d2ff3c77c249174227cab82b5dd9bc11484f22 \
                    sha256  0bf842824815d9155129dcdda0a940ae3b7698c114e6e4a1cc2497527d0704d1

depends_build       port:autoconf \
                    port:automake \
                    port:libtool \
                    port:libxslt

depends_lib         port:geos\
                    port:proj\
                    port:libiconv\
                    port:libxml2

archcheck.files     lib/libgeos_c.dylib\
                    lib/libiconv.dylib\
                    lib/libproj.dylib\
                    lib/libxml2.dylib

# PostGIS 2.0 is not compatible with PostGIS 1.5

conflicts           postgis

# Neither is PostGIS 2.0 compatible with PostGreSQL 8

variant postgresql91        description {Builds with postgresql 9.1} \
                            conflicts postgresql90 postgresql92 postgresql93 {
    depends_lib-append      port:postgresql91 
    archcheck.files-append  lib/postgresql91/libpq.dylib
    configure.args-append   --libdir=${prefix}/lib/postgresql91\
                            --with-pgconfig=${prefix}/lib/postgresql91/bin/pg_config
    build.args-append       PGSQL_DOCDIR=${destroot}${prefix}/share/doc/postgresql91\
                            PGSQL_MANDIR=${destroot}${prefix}/share/man
}

variant postgresql92        description {Builds with postgresql 9.2} \
                            conflicts postgresql90 postgresql91 postgresql93 {
    depends_lib-append      port:postgresql92
    archcheck.files-append  lib/postgresql92/libpq.dylib
    configure.args-append   --libdir=${prefix}/lib/postgresql92\
                            --with-pgconfig=${prefix}/lib/postgresql92/bin/pg_config
    build.args-append       PGSQL_DOCDIR=${destroot}${prefix}/share/doc/postgresql92\
                            PGSQL_MANDIR=${destroot}${prefix}/share/man
}

variant postgresql93        description {Builds with postgresql 9.3} \
                            conflicts postgresql90 postgresql91 postgresql92 {
    depends_lib-append      port:postgresql93
    archcheck.files-append  lib/postgresql93/libpq.dylib
    configure.args-append   --libdir=${prefix}/lib/postgresql93\
                            --with-pgconfig=${prefix}/lib/postgresql93/bin/pg_config
    build.args-append       PGSQL_DOCDIR=${destroot}${prefix}/share/doc/postgresql93\
                            PGSQL_MANDIR=${destroot}${prefix}/share/man
}

variant raster              description {Build raster support} {

    depends_lib-append      port:gdal
    configure.args-append   --with-raster
}

variant topology            description {Build topology support} {

    configure.args-append   --with-topology
}

variant sfcgal              description {Uses SFCGAL for 3D queries} {
    depends_lib-append      port:sfcgal
    configure.args-append   --with-sfcgal=${prefix}/bin/sfcgal-config
}

variant gui                 description {Build the GUI import tool} {

    depends_lib-append      port:gtk2
    configure.args-append   --with-gui
}

variant comments            description {Build comments -- ! broken } {

    ui_warn "The comments variant is currently broken. Use at your own risk."
    
    depends_build-append    port:docbook-xsl \
                            port:libxslt \
                            port:ImageMagick
    
    configure.args-append   --datadir=${prefix}/share/${name} \
                            --with-projdir=${prefix} \
                            --with-xsldir=${prefix}/share/xsl/docbook-xsl

    build.target-append     comments
    destroot.target-append  docs-install comments-install
}

default_variants            +raster +topology

if {![variant_isset postgresql90] && ![variant_isset postgresql91] && \
    ![variant_isset postgresql92] && ![variant_isset postgresql93]} {
    default_variants        +postgresql93
}

# Port phases

pre-configure {
    
    system -W ${worksrcpath} "./autogen.sh"
}

configure.cflags-append \
    -Diconv=libiconv -Diconv_open=libiconv_open -Diconv_close=libiconv_close \
    -I${prefix}/include

post-configure {
    if {[variant_isset universal]} {
        # We must add universal flags to compile liblwgeom properly
        file copy ${filespath}/ed_command ${worksrcpath}/ed_command
        reinplace -E "s#XXX#${configure.universal_cflags}#" \
                    ${worksrcpath}/ed_command
        system -W ${worksrcpath}/liblwgeom \
                "ed Makefile < ${worksrcpath}/ed_command"
    }
}

build.args                  ICONV_LDFLAGS='-L${prefix}/lib -liconv'
use_parallel_build          no


livecheck.type              regex
livecheck.url               ${master_sites}
livecheck.regex             {postgis2?-(\d+(?:\.\d+)*)\.[tz]}
