# -*- 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 117472 2014-02-28 07:59:48Z vince@macports.org $

PortSystem          1.0

name                gdal
version             1.10.1
revision            4
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  b06cbd0f34a6dad99768d5f75a9856e4e027d3bc \
                    sha256  9984f0314c0bd831eb59fb646c2bc5e13cf1378be4f51499045166cbe62be6ea

depends_lib-append  port:zlib \
                    port:libpng \
                    port:tiff \
                    port:libgeotiff \
                    port:jpeg \
                    port:giflib \
                    port:proj

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

configure.args-append \
                    --with-local=${prefix} \
                    --with-libz=${prefix} \
                    --without-liblzma \
                    --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_c_2.diff \
                    patch-gdalwarpkernel_opencl_h.diff

# Upstream fix for "Undefined symbols: _environ"
patchfiles-append   patch-port-cpl_spawn.cpp.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-liblzma
    configure.args-append   --with-liblzma=yes
}

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/Raster_DSDK
}

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
set postgresql_suffixes {81 82 83 84 90 91 92 93}

set portsgresql_variants {}
foreach s ${postgresql_suffixes} {
    lappend portsgresql_variants postgresql${s}
}

foreach s ${postgresql_suffixes} {
    set p postgresql${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${portsgresql_variants} ${p}]
    set c [lreplace ${portsgresql_variants} ${i} ${i}]
    eval [subst {
        variant ${p} description "Enable PostgreSQL ${v} support" conflicts ${c} {
            depends_lib-append      port:${p}
            configure.args-delete   --without-pg
            configure.args-append   --with-pg=${prefix}/lib/postgresql${s}/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}

    if {[variant_isset mrsid]} {
         foreach f [list \
            bin/gdal_contour \
            bin/gdal_grid \
            bin/gdal_rasterize \
            bin/gdal_translate \
            bin/gdaladdo \
            bin/gdalbuildvrt \
            bin/gdaldem \
            bin/gdalenhance \
            bin/gdalinfo \
            bin/gdallocationinfo \
            bin/gdalmanage \
            bin/gdalserver \
            bin/gdalsrsinfo \
            bin/gdaltindex \
            bin/gdaltransform \
            bin/gdalwarp \
            bin/nearblack \
            bin/ogr2ogr \
            bin/ogrinfo \
            bin/ogrtindex \
            bin/testepsg \
            lib/libgdal.1.dylib] \
            {
                exec install_name_tool -change libltidsdk.9.dylib ${prefix}/share/Geo_DSDK/Raster_DSDK/lib/libltidsdk.9.dylib ${destroot}${prefix}/${f}
            }
    }

}

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