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

epoch               1

name                grass
version             8.4.1
revision            4

maintainers         {yahoo.com:n_larsson @nilason} openmaintainer
categories          gis
license             GPL-2+

description         GRASS Geographic Information System
long_description    GRASS GIS offers powerful raster, vector, and geospatial   \
                    processing engines in a single integrated software suite.  \
                    It includes tools for terrain and ecosystem modeling,      \
                    hydrology, visualization of raster and vector data,        \
                    management and analysis of geospatial data, and the        \
                    processing of satellite and aerial imagery. It comes with  \
                    a temporal framework for advanced time series processing   \
                    and a Python API for rapid geospatial programming.         \
                    GRASS GIS has been optimized for performance and large     \
                    geospatial data analysis.\nInstall the port `grass-gui`    \
                    to enable graphical user interface.

homepage            https://grass.osgeo.org
master_sites        ${homepage}/grass[join [lrange [split ${version} .] 0 1] {}]/source/

checksums           rmd160  8892e0067af6143986c2ad87ab55fa45e1247ed5 \
                    sha256  d17dcf67e7c9bcd7103d53f4ba46a9d88430d84c6ce1350650b7021d62db0864 \
                    size    69956380

depends_build-append \
                    port:bison \
                    port:flex \
                    port:pkgconfig

depends_lib-append  port:bzip2 \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:fftw-3 \
                    port:freetype \
                    port:gdal \
                    port:geos \
                    port:liblas \
                    port:libpng \
                    port:libsvm \
                    port:pdal \
                    port:readline \
                    port:sqlite3 \
                    port:tiff \
                    port:zlib

patchfiles          01_patch-dyld-link.diff \
                    02_grass-python.diff \
                    03_patch-agl.diff

# c++17 needed for PDAL 2.4+
compiler.cxx_standard     2017
configure.cxxflags-append -std=c++17
configure.cflags-append   -DGL_SILENCE_DEPRECATION

configure.pre_args-append \
                    --prefix=${prefix}/lib
configure.args-append \
                    --exec-prefix=${prefix} \
                    --with-blas=no \
                    --with-bzlib \
                    --with-bzlib-includes=${prefix}/include \
                    --with-bzlib-libs=${prefix}/lib \
                    --with-cairo \
                    --with-cairo-includes=${prefix}/include/cairo \
                    --with-cairo-libs=${prefix}/lib \
                    --with-fftw-includes=${prefix}/include \
                    --with-fftw-libs=${prefix}/lib \
                    --with-freetype \
                    --with-freetype-includes=${prefix}/include/freetype2 \
                    --with-freetype-libs=${prefix}/lib \
                    --with-gdal=${prefix}/bin/gdal-config \
                    --with-geos=${prefix}/bin/geos-config \
                    --with-lapack=no \
                    --with-liblas=${prefix}/bin/liblas-config \
                    --with-libsvm=yes \
                    --with-mysql=no \
                    --with-opencl=no \
                    --with-opengl=osx \
                    --with-openmp=no \
                    --with-pdal=${prefix}/bin/pdal-config \
                    --with-postgres=no \
                    --with-pthread \
                    --with-readline \
                    --with-readline-includes=${prefix}/include/readline \
                    --with-readline-libs=${prefix}/lib \
                    --with-sqlite \
                    --with-sqlite-includes=${prefix}/include \
                    --with-sqlite-libs=${prefix}/lib \
                    --with-tiff-includes=${prefix}/include \
                    --with-tiff-libs=${prefix}/lib \
                    --with-zstd \
                    --with-zstd-includes=${prefix}/include \
                    --with-zstd-libs=${prefix}/lib \
                    --with-x=no

set python_suffixes {39 310 311 312 313}
# keep python_default in sync with qgis3
set python_default 313

if {${subport} eq ${name}} {
    build.target    default

    variant gui description {Obsolete variant} {
        ui_error "The '+gui' variant is removed. It is replaced by 'grass-gui' subport"
        return -code error "unsupported variant"
    }

    variant mysql57 conflicts mysql8 description {Add MySQL 5.7 support} {
        depends_lib-append      port:mysql57
        configure.args-delete   --with-mysql=no   --with-mysql=yes
        configure.args-append   --with-mysql-includes=${prefix}/include/mysql57/mysql \
                                --with-mysql-libs=${prefix}/lib/mysql57/mysql
        configure.env-append    MYSQLD_CONFIG=${prefix}/lib/mysql57/bin/mysql_config
    }

    variant mysql8 conflicts mysql57 description {Add MySQL 8 support} {
        depends_lib-append      port:mysql8
        configure.args-replace  --with-mysql=no   --with-mysql=yes
        configure.args-append   --with-mysql-includes=${prefix}/include/mysql8/mysql \
                                --with-mysql-libs=${prefix}/lib/mysql8/mysql
        configure.env-append    MYSQLD_CONFIG=${prefix}/lib/mysql8/bin/mysql_config
    }

    variant openblas description {Use OpenBLAS for BLAS/LAPACK} {
        depends_lib-append      port:openblas
        configure.args-replace  --with-blas=no    --with-blas=yes \
                                --with-lapack=no  --with-lapack=yes
        configure.args-append   --with-liblas=${prefix}/bin/liblas-config \
                                --with-lapack-includes=${prefix}/include \
                                --with-lapack-libs=${prefix}/lib
    }

    variant openmp description {Add OpenMP support} {
        depends_lib-append      port:libomp
        configure.args-replace  --with-openmp=no  --with-openmp=yes
        configure.args-append   --with-openmp-includes=${prefix}/include/libomp \
                                --with-openmp-libs=${prefix}/lib/libomp
        compiler.openmp_version 4.0
    }

    # PostGreSQL variants
    set postgresql_versions {12 13 14 15 16 17}
    set postgresql_variants {}
    foreach pg_version ${postgresql_versions} {
        lappend postgresql_variants postgresql${pg_version}
    }
    foreach pg_version ${postgresql_versions} {
        set vrt postgresql${pg_version}
        set index [lsearch -exact ${postgresql_variants} ${vrt}]
        set conf [lreplace ${postgresql_variants} ${index} ${index}]
        variant ${vrt} description "Use PostgreSQL ${pg_version}" conflicts {*}${conf} "
            depends_lib-append      port:${vrt}
            configure.args-append   --with-postgres-includes=${prefix}/include/${vrt} \
                                    --with-postgres-libs=${prefix}/lib/${vrt}
            configure.args-replace  --with-postgres=no  --with-postgres=yes
        "
    }
    # set PostGreSQL default variant
    set pgdefault "if {"
    foreach pg_version ${postgresql_versions} {
        set pgdefault "${pgdefault}!\[variant_isset postgresql${pg_version}\] && "
    }
    set pgdefault   [string range ${pgdefault} 0 end-4]
    set pgdefault   "${pgdefault}} { default_variants +postgresql${pg_version} }"
    eval ${pgdefault}

    # PROJ variants
    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.env           PATH=$::env(PATH):${prefix}/lib/proj${proj_ver}/bin
            build.env               PATH=$::env(PATH):${prefix}/lib/proj${proj_ver}/bin
            configure.args-append   --with-proj-includes=${prefix}/lib/proj${proj_ver}/include \
                                    --with-proj-libs=${prefix}/lib/proj${proj_ver}/lib \
                                    --with-proj-share=${prefix}/lib/proj${proj_ver}/share/proj
        "
    }
    # set PROJ default variant
    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}

    # Python variants
    set python_variants {}
    foreach pyver ${python_suffixes} {
        lappend python_variants python${pyver}
    }
    foreach pyver ${python_suffixes} {
        set py_dot_ver [string index ${pyver} 0].[string range ${pyver} 1 end]
        set index   [lsearch -exact ${python_variants} python${pyver}]
        set conf    [lreplace ${python_variants} ${index} ${index}]
        variant python${pyver} description "Use Python ${py_dot_ver}" conflicts {*}${conf} "
            depends_lib-append      port:python${pyver} \
                                    port:py${pyver}-Pillow \
                                    port:py${pyver}-numpy \
                                    port:py${pyver}-six
            depends_run-append      port:py${pyver}-psycopg2
        "
    }
    # Patches scripts to launch the selected Python version (fixes #59954)
    post-patch {
        foreach pyver ${python_suffixes} {
            if {[variant_isset python${pyver}]} {
                set pyversion   [string index ${pyver} 0].[string range ${pyver} 1 end]
                set pybin       ${prefix}/bin/python${pyversion}
                fs-traverse f ${worksrcpath} {
                    if {[file extension ${f}] eq ".py"} {
                        reinplace -q "s|/usr/bin/env python3|${pybin}|" ${f}
                    }
                }
                reinplace "s|PYVER.*|PYVER = ${pyversion}|" ${worksrcpath}/macosx/Makefile
                reinplace "s|^PYTHON.*|PYTHON = ${pybin}|" ${worksrcpath}/include/Make/Platform.make.in
            }
        }
        reinplace "s|@PYBIN@|${pybin}|" ${worksrcpath}/lib/init/grass.py
    }
    # set Python default variant
    set pydefault "if {"
    foreach suffix ${python_suffixes} {
        set pydefault "${pydefault}!\[variant_isset python${suffix}\] && "
    }
    set pydefault [string range ${pydefault} 0 end-4]
    set pydefault "${pydefault}} { default_variants +python${python_default} }"
    eval ${pydefault}

    post-configure {
        reinplace -E "s|\\\$\\\(CPPFLAGS)| \$(CPPFLAGS) \
            \\\\\"-Drestrict=\\\\\" \\\\\"-D__attribute__(x)=\\\\\" \\\\\"-D_Nonnull=\\\\\" \
            \\\\\"-Dint8_t=char\\\\\" \\\\\"-DCF_INLINE=\\\\\" \\\\\"-D_Null_unspecified=\\\\\" \
            \\\\\"-D__DARWIN_OS_INLINE=\\\\\"|" ${worksrcpath}/python/libgrass_interface_generator/Makefile
    }

    post-destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/pkgconfig
        xinstall -m 0644 -W ${worksrcpath} grass.pc ${destroot}${prefix}/share/pkgconfig/grass.pc
    }

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


subport grass-gui {
    PortGroup           active_variants 1.1

    epoch               0
    revision            0

    description         GUI support to GRASS Geographic Information System
    long_description    {*}${description}, includes wxPython and application bundle.

    platforms           {darwin any}
    supported_archs     noarch

    depends_build
    depends_lib         port:grass
    use_configure       no
    build               {}

    # Python variants
    set python_suffixes {39 310 311 312 313}
    set py_grass        [lindex $python_suffixes end]
    set py_variant      ${py_grass}
    foreach pyver ${python_suffixes} {
        if {![catch {set result [active_variants grass python${pyver}]}]} {
            if {$result} {
                set py_grass ${pyver}
            }
        }
        if {[variant_isset python${pyver}]} {
            set py_variant ${pyver}
        }
    }
    if {${py_grass} ne ${py_variant}} {
        ui_warn "'${subport}' Python variant must be the same as for the active\
version of 'grass'. Change +python${py_variant} to +python${py_grass}."
    }
    set python_variants {}
    foreach pyver ${python_suffixes} {
        lappend python_variants python${pyver}
    }
    foreach pyver ${python_suffixes} {
        set py_dot_ver [string index ${pyver} 0].[string range ${pyver} 1 end]
        set index   [lsearch -exact ${python_variants} python${pyver}]
        set conf    [lreplace ${python_variants} ${index} ${index}]

        variant python${pyver} description "Use Python ${py_dot_ver}" conflicts {*}${conf} "
            depends_run-append      port:py${py_grass}-matplotlib \
                                    port:py${py_grass}-termcolor \
                                    port:py${py_grass}-wxpython-4.0
        "
    }
    # set Python default variant
    set pydefault "if {"
    foreach suffix ${python_suffixes} {
        set pydefault "${pydefault}!\[variant_isset python${suffix}\] && "
    }
    set pydefault [string range ${pydefault} 0 end-4]
    set pydefault "${pydefault}} { default_variants +python${python_default} }"
    eval ${pydefault}

    # Create app bundle
    global app_name
    set app_name_version [join [lrange [split ${version} "."] 0 1] "."]
    set app_name GRASS-${app_name_version}.app

    destroot {
        xinstall -d -m 0755 ${worksrcpath}/${app_name}/Contents
        xinstall -d -m 0755 ${worksrcpath}/${app_name}/Contents/Resources
        xinstall -d -m 0755 ${worksrcpath}/${app_name}/Contents/MacOS

        set script [open "${worksrcpath}/${app_name}/Contents/MacOS/GRASS" w 0755]
        puts ${script} "#!/usr/bin/osascript"
        puts ${script} ""
        puts ${script} "tell application \"Terminal\" to do script \"${prefix}/bin/grass --gui; exit\""
        close ${script}

        copy ${worksrcpath}/macosx/app/AppIcon.icns \
            ${worksrcpath}/${app_name}/Contents/Resources/AppIcon.icns
        copy ${worksrcpath}/macosx/app/GRASSDocument_gxw.icns \
            ${worksrcpath}/${app_name}/Contents/Resources/GRASSDocument_gxw.icns

        set info_plist ${worksrcpath}/${app_name}/Contents/Info.plist
        copy ${worksrcpath}/macosx/app/Info.plist.in ${info_plist}

        set version_file        [open ${worksrcpath}/include/VERSION]
        set version_data        [split [read ${version_file}] "\n"]
        close ${version_file}

        set grass_version_major [lindex ${version_data} 0]
        set grass_version_minor [lindex ${version_data} 1]
        set grass_version_date  [lindex ${version_data} 3]
        set bundle_version      [join [lrange ${version_data} 0 2] "."]

        reinplace "s|@GRASS_VERSION_MAJOR@|${grass_version_major}|" ${info_plist}
        reinplace "s|@GRASS_VERSION_MINOR@|${grass_version_minor}|" ${info_plist}
        reinplace "s|@GRASS_VERSION_DATE@|${grass_version_date}|" ${info_plist}
        reinplace "s|@BUNDLE_VERSION@|${bundle_version}|" ${info_plist}
        reinplace "s|@DEPLOYMENT_TARGET@|${macosx_deployment_target}|" ${info_plist}

        copy ${worksrcpath}/${app_name} ${destroot}${applications_dir}
    }

    livecheck.type      none
}
