# -*- 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 106285 2013-05-21 09:08:48Z vince@macports.org $

PortSystem          1.0
PortGroup           archcheck 1.0

name                postgis2
categories          databases gis
license             GPL
epoch               1
version             2.0.3
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  15bf8952c458fd323288028d5062006817cbcccb \
 sha256  7f865a6fdf19afed7d2f3e7178cd504f7254a9e96f2ce6a07d0ea19edc1668a0


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 postgresql90        description {Builds with postgresql 9.0} \
                            conflicts postgresql91 postgresql92 {
    depends_lib-append      port:postgresql90
    archcheck.files-append  lib/postgresql90/libpq.dylib
    configure.args-append   --libdir=${prefix}/lib/postgresql90\
                            --with-pgconfig=${prefix}/lib/postgresql90/bin/pg_config
    build.args-append       PGSQL_DOCDIR=${destroot}${prefix}/share/doc/postgresql90\
                            PGSQL_MANDIR=${destroot}${prefix}/share/man
}

variant postgresql91        description {Builds with postgresql 9.1} \
                            conflicts postgresql90 postgresql92 {
    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 {
    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/postgresql91\
                            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 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]} {
    default_variants        +postgresql92
}

# Port phases

pre-configure {
    if {[variant_isset postgresql90]} {
        set PGVERSION   postgresql90
    } elseif {[variant_isset postgresql91]} {
        set PGVERSION   postgresql91
    }
    
    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             ${name}-(\\d+(?:\\.\\d+)*)\\.tar
