# -*- 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 108385 2013-07-22 10:08:03Z vince@macports.org $

PortSystem          1.0

name                gdal
version             1.10.0
revision            1
categories          gis
license             MIT BSD
platforms           darwin

maintainers         gmail.com:seanasy openmaintainer

description         GDAL - Geospatial Data Abstraction Library

long_description    GDAL is a translator library for raster geospatial \
                    data formats that is released under an X/MIT style \
                    Open Source license. As a library, it presents a \
                    single abstract data model to the calling application \
                    for all supported formats. The related OGR library \
                    (which lives within the GDAL source tree) provides a \
                    similar capability for simple features vector data.

homepage            http://www.gdal.org/
master_sites        http://www.gdal.org/dl/ \
                    http://download.osgeo.org/gdal/ \
                    http://download.osgeo.org/gdal/CURRENT/

checksums           rmd160  106034155b6c80c34816720c77b0f53e22853ddb \
                    sha256  1a9ca12bcc3a55f66bcd6654d5ff12eeddb3a26b622dd7c8872febb4f5a97487

depends_lib-append  port:zlib \
                    port:libpng \
                    port:tiff \
                    port:libgeotiff \
                    port:jpeg \
                    path:include/gif_lib.h:giflib \
                    port:proj

set PYINST          ""
set OPENCLBASE      "/System/Library/Frameworks/OpenCL.framework/"

configure.args-append \
                    --with-local=${prefix} \
                    --with-libz=${prefix} \
                    --without-lzma \
                    --with-png=${prefix} \
                    --with-libtiff=${prefix} \
                    --with-geotiff=${prefix} \
                    --with-jpeg=${prefix}\
                    --with-gif=${prefix} \
                    --with-static-proj4=${prefix} \
                    --without-pg \
                    --without-grass \
                    --without-libgrass \
                    --without-cfitsio \
                    --without-pcraster \
                    --without-netcdf \
                    --without-ogdi \
                    --without-fme \
                    --without-hdf4 \
                    --without-hdf5 \
                    --without-jasper \
                    --without-openjpeg \
                    --without-ecw \
                    --without-kakadu \
                    --without-mrsid \
                    --without-jp2mrsid \
                    --without-msg \
                    --without-bsb \
                    --without-oci \
                    --without-grib \
                    --without-mysql \
                    --without-ingres \
                    --without-xerces \
                    --without-odbc \
                    --without-curl \
                    --without-sqlite3 \
                    --without-spatialite \
                    --without-dwgdirect \
                    --without-idb \
                    --without-sde \
                    --without-geos \
                    --without-opencl \
                    --without-pam \
                    --without-perl \
                    --without-php \
                    --without-ruby \
                    --without-python \
                    --without-xerces \
                    --without-expat \
                    --without-poppler \
                    --mandir=${prefix}/share/man

# Patch to ensure that the python installation respects DESTDIR
patchfiles          patch-swig_python_GNUmakefile \
                    patch-gdalwarpkernel_opencl_c.diff \
                    patch-gdalwarpkernel_opencl_h.diff

# Patches for universal build
post-configure {
    reinplace -E "s|(INST_PYMOD).*|\\1\t=\t${PYINST}|" \
        ${worksrcpath}/GDALmake.opt
    if {[variant_isset universal]} {
        system "ed - ${worksrcpath}/port/cpl_config.h < ${filespath}/config.h.ed"
    }
}

# Set target to none
build.target

variant lzma conflicts universal \
    description {Enable LZMA (7Z) compression support} {
    depends_lib-append      port:lzma
    configure.args-delete   --without-lzma
    configure.args-append   --with-lzma=${prefix}
}

variant geos description {Enable GEOS geometry engine functionality} {
    depends_lib-append      port:geos
    configure.args-delete   --without-geos
    configure.args-append   --with-geos=${prefix}/bin/geos-config
}

variant curl description {Enable curl support} {
    depends_lib-append      port:curl
    configure.args-delete   --without-curl
    configure.args-append   --with-curl=${prefix}/bin/curl-config
}

variant mrsid description {Enable MrSID file format} {
    depends_lib-append      port:geoexpress-sdk
    configure.args-delete   --without-mrsid
    configure.args-append       --with-mrsid=${prefix}/share/Geo_DSDK

    # Fixes for Geo_DSDK 7.x - http://trac.osgeo.org/gdal/ticket/2410
    patchfiles-append       patch-frmts_mrsid_mrsiddataset.cpp \
                            patch-mrsid-configure
}

variant hdf4 description {Enable HDF4 file format} {
    depends_lib-append      port:hdf4
    configure.args-delete   --without-hdf4
    configure.args-append   --with-hdf4=${prefix}
}

variant hdf5 description {Enable HDF5 file format} {
    depends_lib-append      port:hdf5-18
    configure.args-delete   --without-hdf5
    configure.args-append   --with-hdf5=${prefix}
}

variant netcdf description {Enable NetCDF file format} {
    depends_lib-append      port:netcdf
    configure.args-delete   --without-netcdf
    configure.args-append   --with-netcdf=${prefix}
}

#JPEG-2K
variant jasper conflicts openjpeg \
    description {Enable JasPer JPEG-2000 format support} {
    depends_lib-append      port:jasper
    configure.args-delete   --without-jasper
    configure.args-append   --with-jasper=${prefix}
}

variant openjpeg conflicts jasper \
    description {Enable OpenJPEG JPEG-2000 format support} {
    depends_lib-append      port:openjpeg
    configure.args-delete   --without-openjpeg
    configure.args-append   --with-openjpeg=${prefix}
}


variant xerces description {Enable xerces XML support for GML file format} {
    depends_lib-append      port:xercesc
    configure.args-delete   --without-xerces
    configure.args-append   --with-xerces=${prefix}
}

variant expat description {Enable expat XML support} {
    depends_lib-append      port:expat
    configure.args-delete   --without-expat
    configure.args-append   --with-expat=${prefix}
}

# Python variants
variant python24 conflicts python25 python26 python27 \
    description {Enable Python 2.4 support} {
    depends_lib-append      port:python24 \
                            port:py-numpy
    configure.args-delete   --without-python
    configure.args-append   --with-python
    set PYINST              ${frameworks_dir}/Python.framework/Versions/2.4
    build.args-append       PYTHON=python2.4
}

variant python25 conflicts python24 python26 python27 \
    description {Enable Python 2.5 support} {
    depends_lib-append      port:python25 \
                            port:py25-numpy
    configure.args-delete   --without-python
    configure.args-append   --with-python
    set PYINST              ${frameworks_dir}/Python.framework/Versions/2.5
    build.args-append       PYTHON=python2.5
    use_parallel_build      no
}

variant python26 conflicts python24 python25 python27 \
    description {Enable Python 2.6 support} {
    depends_lib-append      port:python26 \
                            port:py26-numpy
    configure.args-delete   --without-python
    configure.args-append   --with-python
    set PYINST              ${frameworks_dir}/Python.framework/Versions/2.6
    build.args-append       PYTHON=python2.6
    use_parallel_build      no
}

variant python27 conflicts python24 python25 python26 \
    description {Enable Python 2.7 support} {
    depends_lib-append      port:python27 \
                            port:py27-numpy
    configure.args-delete   --without-python
    configure.args-append   --with-python
    build.args-append       PYTHON=python2.7
    set PYINST               ${frameworks_dir}/Python.framework/Versions/2.7
    use_parallel_build      no
}

# Database variants
variant postgresql81 description {Enable PostgreSQL 8.1 support} {
    depends_lib-append      port:postgresql81
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql81/bin/pg_config
}

variant postgresql82 description {Enable PostgreSQL 8.2 support} {
    depends_lib-append      port:postgresql82
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql82/bin/pg_config
}

variant postgresql83 description {Enable PostgreSQL 8.3 support} {
    depends_lib-append      port:postgresql83
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql83/bin/pg_config
}

variant postgresql84 description {Enable PostgreSQL 8.4 support} {
    depends_lib-append      port:postgresql84
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql84/bin/pg_config
}

variant postgresql90 description {Enable PostgreSQL 9.0 support} {
    depends_lib-append      port:postgresql90
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql90/bin/pg_config
}

variant postgresql91 description {Enable PostgreSQL 9.1 support} {
    depends_lib-append      port:postgresql91
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql91/bin/pg_config
}

variant postgresql92 description {Enable PostgreSQL 9.2 support} {
    depends_lib-append      port:postgresql92
    configure.args-delete   --without-pg
    configure.args-append   --with-pg=${prefix}/lib/postgresql92/bin/pg_config
}

variant mysql5 description {Enable MySQL 5 support} {
    depends_lib-append      path:bin/mysql_config5:mysql5
    configure.args-delete   --without-mysql
    configure.args-append   --with-mysql=${prefix}/lib/mysql5/bin/mysql_config
}

variant sqlite3 description {Enable SQLite3 support} {
    depends_lib-append      port:sqlite3
    configure.args-delete   --without-sqlite3
    configure.args-append   --with-sqlite3=${prefix}

    post-configure {
        reinplace -E "s|^LIBS(.*)\\\\$|LIBS\\1 -lsqlite3 \\\\|" ${worksrcpath}/GDALmake.opt
    }
}

variant spatialite description {Enable SpartiaLite support} {
    depends_lib-append      port:spatialite
    configure.args-delete   --without-spatialite
    configure.args-append   --with-spatialite=${prefix}
}

variant odbc description {Enable ODBC support} {
    depends_lib-append      port:unixODBC
    configure.args-delete   --without-odbc
    configure.args-append   --with-odbc=${prefix}
}

variant poppler description {Enable poppler support} {
    depends_lib-append      port:poppler
    configure.args-delete   --without-popper
    configure.args-append   --with-poppler=${prefix}
}

# OpenCL
variant opencl description {Use OpenCL for parallel computations} {
    configure.args-delete   --without-opencl
    configure.args-append   --with-opencl
    configure.args-append   --with-opencl-include=${OPENCLBASE}/Headers
    configure.args-append   --with-opencl-lib='-framework OpenCL'
}

# OS X Framework
variant framework description {Build as a Mac OS X framework} {
    configure.args-append   --with-macosx-framework
}

# Perf variant to optimize code
variant perf description {Optimize for speed} {
    configure.compiler      macports-clang-3.3
    configure.cflags        -O3 -pipe -march=native
    configure.cxxflags      -O3 -pipe -march=native
}

default_variants    +expat

# Postconfigure hack to avoid read ${include} .h files before
# GDAL own include directories
post-configure {
    set FRMT_PATH ${worksrcpath}/ogr/ogrsf_frmts
    foreach makefile [glob -dir $FRMT_PATH */GNUmakefile] {
        reinplace "s|\$\(EXPAT_INCLUDE\)||" $makefile
    }
}

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 -W ${worksrcpath} HOWTO-RELEASE NEWS VERSION \
        ${destroot}${prefix}/share/doc/${name}
}

livecheck.type  regex
livecheck.url   http://download.osgeo.org/gdal/CURRENT
livecheck.regex ${name}-(\\d+(?:\\.\\d+)*)\\.tar
