# -*- 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

PortSystem          1.0
PortGroup           cmake 1.1

name                mapserver
version             8.0.2
revision            1
maintainers         hbaspecto.com:jea {yahoo.com:n_larsson @nilason} openmaintainer
categories          gis
license             permissive

description         mapserver
long_description    MapServer is an Open Source platform for publishing spatial \
                    data and interactive mapping applications to the web.

homepage            https://mapserver.org
master_sites        https://download.osgeo.org/mapserver

checksums           rmd160  07390a432c74df07783ef98caf6d417443d4b0ea \
                    sha256  0830c43feefeca132171b429403716a2cbaef0626d439f00e8a3a27a877724fe \
                    size    2957172

depends_build-append \
                    port:pkgconfig

depends_lib-append \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:freetype \
                    port:fribidi \
                    port:gdal \
                    port:giflib \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:geos \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    path:lib/pkgconfig/harfbuzz-icu.pc:harfbuzz-icu \
                    port:curl \
                    port:libxml2 \
                    port:libiconv \
                    port:protobuf-c

patchfiles-append   python.diff

# Optional components
configure.args-append \
                    -DWITH_GIF=ON \
                    -DWITH_MYSQL=OFF \
                    -DWITH_FRIBIDI=ON \
                    -DWITH_HARFBUZZ=ON \
                    -DWITH_CAIRO=ON \
                    -DWITH_SVGCAIRO=OFF \
                    -DWITH_RSVG=OFF \
                    -DWITH_CURL=ON \
                    -DWITH_PIXMAN=OFF \
                    -DWITH_LIBXML2=ON \
                    -DWITH_POSTGIS=OFF \
                    -DWITH_GEOS=ON \
                    -DWITH_FCGI=OFF \
                    -DWITH_PROTOBUFC=ON \
                    -DWITH_ORACLESPATIAL=OFF \
                    -DWITH_ORACLE_PLUGIN=OFF \
                    -DWITH_MSSQL2008=OFF \
                    -DWITH_EXEMPI=OFF

# Optional features
configure.args-append \
                    -DWITH_KML=ON \
                    -DWITH_SOS=ON \
                    -DWITH_WMS=ON \
                    -DWITH_WFS=ON \
                    -DWITH_WCS=ON \
                    -DWITH_CLIENT_WMS=ON \
                    -DWITH_CLIENT_WFS=ON \
                    -DWITH_OGCAPI=ON \
                    -DWITH_ICONV=ON \
                    -DWITH_THREAD_SAFETY=OFF \
                    -DWITH_XMLMAPFILE=OFF

# Mapscripts
configure.args-append \
                    -DWITH_PYTHON=OFF \
                    -DWITH_PYMAPSCRIPT_ANNOTATIONS=OFF \
                    -DWITH_PHPNG=OFF \
                    -DWITH_PERL=OFF \
                    -DWITH_RUBY=OFF \
                    -DWITH_JAVA=OFF \
                    -DWITH_CSHARP=OFF \
                    -DWITH_XMLMAPFILE=OFF \
                    -DWITH_V8=OFF

default_variants    +fastcgi +postgis

variant fastcgi description {FastCGI support} {
    depends_lib-append      port:fcgi
    configure.args-replace  -DWITH_FCGI=OFF -DWITH_FCGI=ON
}


# Set Python variants
set python_suffixes {38 39 310 311 312}
set python_variants {}

foreach pyver ${python_suffixes} {
    lappend python_variants python${pyver}
}

foreach pyver ${python_suffixes} {
    set index [lsearch -exact ${python_variants} python${pyver}]
    set py_dot_ver [string index ${pyver} 0].[string range ${pyver} 1 end]
    set conf [lreplace ${python_variants} ${index} ${index}]

    variant python${pyver} description "Add Python MapScript for Python ${py_dot_ver}" conflicts {*}${conf} "
        depends_build-append    port:swig
        configure.args-append   -DPython_ROOT_DIR=${frameworks_dir}/Python.framework/Versions/${py_dot_ver}/
        configure.args-replace  -DWITH_PYTHON=OFF -DWITH_PYTHON=ON \
                                -DWITH_PYMAPSCRIPT_ANNOTATIONS=OFF -DWITH_PYMAPSCRIPT_ANNOTATIONS=ON
        post-patch {
            reinplace \"s|@PYTHONFRW@|${frameworks_dir}/Python.framework/Versions/${py_dot_ver}|\" \
              mapscript/python/CMakeLists.txt
        }
    "
}

# Set PostGIS (and PostgreSQL)
if {[variant_isset postgis]} {
    # check for most recent version of postgresql installed
    set HAVE_PGCONFIG [llength [glob -nocomplain -d ${prefix}/lib postgresql*/bin/pg_config]]
    # if not found, install the postgresql port
    if {$HAVE_PGCONFIG == 0} {
        depends_lib-append  port:postgresql15
    }
}

variant postgis description {Add support for postgis sources} {
    set PGSQL_LIB_DIR [lindex [lsort -dec [glob -nocomplain -d ${prefix}/lib postgresql*]] 0]
    set PGSQL_INC_DIR [lindex [lsort -dec [glob -nocomplain -d ${prefix}/include postgresql*]] 0]
    configure.args-replace  -DWITH_POSTGIS=OFF  -DWITH_POSTGIS=ON
    configure.args-append   -DPostgreSQL_INCLUDE_DIR=${PGSQL_INC_DIR} \
                            -DPostgreSQL_LIBRARY_DIR=${PGSQL_LIB_DIR} \
                            -DPostgreSQL_ADDITIONAL_VERSIONS=15
}

# Set PROJ
set proj_versions {6 7 8 9}
set proj_variants {}
foreach pjver ${proj_versions} {
    lappend proj_variants proj${pjver}
}
foreach proj_ver ${proj_versions} {

    set index [lsearch -exact ${proj_variants} proj${proj_ver}]
    set cflcts [lreplace ${proj_variants} ${index} ${index}]

    variant proj${proj_ver} description "Use Proj${proj_ver}" conflicts {*}${cflcts} "

        depends_lib-append      port:proj${proj_ver}
        configure.args-append   -DPROJ_INCLUDE_DIR=${prefix}/lib/proj${proj_ver}/include \
                                -DPROJ_LIBRARY=${prefix}/lib/proj${proj_ver}/lib/libproj.dylib
    "
}

set projdf "if {"
foreach pv ${proj_versions} {
    set projdf "${projdf}!\[variant_isset proj${pv}\] && "
}
set projdf [string range ${projdf} 0 end-4]
set projdf "${projdf}} { default_variants +proj${pv} }"
eval ${projdf}

# Set Apache
variant apache2 conflicts apache_apple description {Use MacPorts Apache 2} {
    depends_run-append      port:apache2
    global cgi_path
    set cgi_path "${prefix}/www/apache2/cgi-bin/"
}

variant apache_apple conflicts apache2 description {Use Apple Apache} {
    global cgi_path
    set cgi_path "/Library/WebServer/CGI-Executables/"
    destroot.violate_mtree yes
}

if {![variant_isset apache2] && ![variant_isset apache_apple]} {
    # default to the apple apache version if none specified
    default_variants +apache_apple
}

post-destroot {
    file mkdir ${destroot}${cgi_path}
    # There's no good reason to keep the executable in ${prefix}/bin.
    move ${destroot}${prefix}/bin/mapserv ${destroot}${cgi_path}
}

notes "
Using the default Apple Apache variant (+apache_apple) the CGI binary file
'mapserv' is installed in '/Library/WebServer/CGI-Executables/', otherwise
it is installed in '${prefix}/www/apache2/cgi-bin/'.
A MapServer configure sample file is available at
'${prefix}/etc/mapserver-sample.conf', which you may copy to a location of your
choice and set its new path to the environment variable MAPSERVER_CONFIG_FILE
(see https://mapserver.org/mapfile/config.html#config).
"

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}