# -*- 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
PortGroup           legacysupport 1.1

epoch               1

name                grass
version             8.5.0
revision            2

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

description         GRASS Geographic Information System
long_description    GRASS 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 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  f1139b2d993ca1106b6a74303ec58cae64268bd6 \
                    sha256  5d06c81504ea961fab709943cf41d8b3e27cdbaae7ccca5baf3f7bb0c5d9f33d \
                    size    67893009

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:proj \
                    port:readline \
                    port:sqlite3 \
                    port:tiff \
                    port:zlib

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

# Enable use of 'macports-libcxx' for macOS 10.14 and earlier,
# needed to build with PDAL.
legacysupport.use_mp_libcxx yes
legacysupport.newest_darwin_requires_legacy 18

# 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 \
                    --with-postgres=no \
                    --with-proj-includes=${prefix}/lib/proj9/include \
                    --with-proj-libs=${prefix}/lib/proj9/lib \
                    --with-proj-share=${prefix}/lib/proj9/share/proj \
                    --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

configure.env       PATH=$::env(PATH):${prefix}/lib/proj9/bin
build.env           PATH=$::env(PATH):${prefix}/lib/proj9/bin

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

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

    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-blas=openblas \
                                --with-lapack=openblas
    }

    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 18}
    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}

    # 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|^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 {310 311 312 313 314}
    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}/macos/files/AppIcon.icns \
            ${worksrcpath}/${app_name}/Contents/Resources/AppIcon.icns
        copy ${worksrcpath}/macos/files/GRASSDocument_gxw.icns \
            ${worksrcpath}/${app_name}/Contents/Resources/GRASSDocument_gxw.icns

        set info_plist ${worksrcpath}/${app_name}/Contents/Info.plist
        copy ${worksrcpath}/macos/files/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
}
