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

name                proj
# keep this stub version in sync with latest version in `proj_versions_info`
version             9.8.0
categories          gis

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

set latest_version  ${version}
set latest_major    [lindex [split ${latest_version} .] 0]

# current PROJ subports
#
# meaning of the fields:
# - major version
# - minor.micro version
# - revision
# - rmd160
# - sha256
# - size
set proj_versions_info {
    4 9.3 0 cffdce968c8929fd7f38a65e3d905a62858589a2 6984542fea333488de5c82eea58d699e4aff4b359200a9971537cd7e047185f7 921448
    5 2.0 0 d99eccff181bb002dd0e398a69fb26cbf57fc4d6 ef919499ffbc62a4aae2659a55e2b25ff09cccbbe230656ba71c6224056c7e60 1398188
    6 3.2 0 921b822868362fd1a81158542d540fd34a0c44c7 cb776a70f40c35579ae4ba04fb4a388c1d1ce025a1df6171350dc19f25b80311 2827685
    7 2.1 1 33640fc210ad5e465c04047ac364a53c2415b441 b384f42e5fb9c6d01fe5fa4d31da2e91329668863a684f97be5d4760dbbf0a14 5714666
    8 2.1 1 7456023fdeb03f7d1c182fe67f617960ea3efede 76ed3d0c3a348a6693dfae535e5658bbfd47f71cb7ff7eb96d9f12f7e068b1cf 5882136
    9 8.0 0 8be8201393d695f61c38cbe39935d8f83acd3a91 a8b493b00cf4d08b712b9e063ed5e53e2be90fcde46770e9dbd773341f378f43 5980467
}

foreach {proj_major proj_minor proj_revision proj_rmd160 proj_sha256 proj_size} ${proj_versions_info} {
    subport proj${proj_major} {
        version             ${proj_major}.${proj_minor}
        revision            ${proj_revision}
        license             MIT
        maintainers         {stromnov @stromnov} openmaintainer

        distname            ${name}-${version}

        checksums           rmd160 ${proj_rmd160} sha256 ${proj_sha256} size ${proj_size}

        description         PROJ ${proj_major} is a generic coordinate transformation software

        long_description    {*}${description} that transforms geospatial coordinates from one \
                            coordinate reference system (CRS) to another. This includes \
                            cartographic projections as well as geodetic transformations.

        if {${proj_major} >= 5 && ${proj_major} <= 8} {
            compiler.cxx_standard 2011
        }

        if {${proj_major} >= 6} {
            depends_lib-append  port:sqlite3
        }

        if {${proj_major} >= 6 && ${proj_major} <= 8} {
            depends_build-append port:pkgconfig
        }

        if {${proj_major} >= 7 && ${proj_major} <= 8} {
            configure.pre_args  --prefix=${prefix}/lib/proj${proj_major}
            configure.args      --mandir=${prefix}/lib/proj${proj_major}/share/man \
                                --disable-tiff

            variant tiff description {Enable TIFF I/O} {
                depends_lib-append      port:tiff
                configure.args-replace  --disable-tiff --enable-tiff
            }

            variant lto description {Enable Link Time Optimization} {
                configure.args-append   --enable-lto
            }

            variant native description {Optimize for speed} {
                if {${configure.build_arch} in [list ppc ppc64]} {
                    configure.optflags  -O3 -pipe -mcpu=native -mtune=native -DGDAL_COMPILATION
                } else {
                    configure.optflags  -O3 -pipe -march=native -DGDAL_COMPILATION
                }
            }
        }

        if {${proj_major} >= 7} {
            depends_lib-append  port:curl
            default_variants    +tiff

            # error: return type 'const nn<[...]>' must match previous return type
            # 'nn<[...]>' when lambda expression has unspecified explicit return type
            compiler.blacklist-append {clang < 602}
        }


        if {${subport} eq "proj4"} {
            maintainers     nomaintainer
            set datumgrid_version   1.5

            distfiles-append    ${name}-datumgrid-${datumgrid_version}.zip

            checksums           ${name}-${version}.tar.gz \
                                rmd160 ${proj_rmd160} sha256 ${proj_sha256} size ${proj_size} \
                                ${name}-datumgrid-${datumgrid_version}.zip \
                                rmd160  f5deacd0242557c92c35d43941cd52a7e4096467 \
                                sha256  723c4017d95d7a8abdf3bda4e18d3c15d79b00f9326d453da5fdf13f96c287db \
                                size    2250131

            extract.only        ${name}-${version}.tar.gz

            post-extract {
                system "unzip ${distpath}/${name}-datumgrid-${datumgrid_version}.zip -d ${worksrcpath}/nad"
            }

            configure.pre_args  --prefix=${prefix}/lib/proj49
            configure.args      --mandir=${prefix}/lib/proj49/share/man
        }

        if {${subport} eq "proj5"} {
            maintainers         nomaintainer
            set datumgrid_version   1.8
            distfiles-append    ${name}-datumgrid-${datumgrid_version}.zip

            checksums           ${name}-${version}.tar.gz \
                                rmd160 ${proj_rmd160} sha256 ${proj_sha256} size ${proj_size} \
                                ${name}-datumgrid-${datumgrid_version}.zip \
                                rmd160  f84c374f85c1314c7e357fd50683b67767cea358 \
                                sha256  b9838ae7e5f27ee732fb0bfed618f85b36e8bb56d7afb287d506338e9f33861e \
                                size    6601640

            extract.only        ${name}-${version}.tar.gz

            post-extract {
                set datumgrid ${distpath}/${name}-datumgrid-${datumgrid_version}.zip
                system "unzip '$datumgrid' -d '${worksrcpath}/nad'"
            }

            pre-destroot {
                file copy ${worksrcpath}/src/projects.h ${destroot}${prefix}/include
            }

            configure.pre_args  --prefix=${prefix}/lib/proj5
            configure.args      --mandir=${prefix}/share/man

            post-destroot {
                xinstall -m 644 ${worksrcpath}/src/projects.h ${destroot}${prefix}/include/
            }
        }

        if {${subport} eq "proj6"} {
            set datumgrid_ver   1.8

            distfiles-append    ${name}-datumgrid-${datumgrid_ver}.zip

            checksums           ${name}-${version}.tar.gz \
                                rmd160 ${proj_rmd160} sha256 ${proj_sha256} size ${proj_size} \
                                ${name}-datumgrid-${datumgrid_ver}.zip \
                                rmd160  f84c374f85c1314c7e357fd50683b67767cea358 \
                                sha256  b9838ae7e5f27ee732fb0bfed618f85b36e8bb56d7afb287d506338e9f33861e \
                                size    6601640

            extract.only        ${name}-${version}.tar.gz

            patchfiles-append   6-7-patch-add-missing-includes.diff

            depends_extract-append port:unzip

            post-extract {
                set datumgrid ${distpath}/${name}-datumgrid-${datumgrid_ver}.zip
                system "${prefix}/bin/unzip -o '$datumgrid' -d '${worksrcpath}/data'"
            }

            configure.pre_args  --prefix=${prefix}/lib/proj6
            configure.args      --mandir=${prefix}/lib/proj6/share/man

            # Without this, the build fails with Xcode clang on 10.8/5.1.1 and 10.9/6.2.
            # This is due to an upstream bug requiring a forgiving compiler.
            compiler.blacklist-append {clang < 700}

        }

        if {${subport} eq "proj7"} {
            patchfiles-append   6-7-patch-add-missing-includes.diff
        }

        if {${subport} eq "proj8"} {
            patchfiles-append   8-patch-add-missing-includes.diff

            test.run            yes
            test.target         check-local
        }

        if {${subport} eq "proj9"} {
            PortGroup           cmake   1.1

            cmake.install_prefix ${prefix}/lib/proj9

            compiler.cxx_standard 2017

            configure.args-append \
                                -DENABLE_CURL=ON \
                                -DENABLE_TIFF=OFF \
                                -DENABLE_IPO=OFF \
                                -DBUILD_TESTING=OFF \
                                -DBUILD_EXAMPLES=OFF \
                                -DPROJ_DATA_ENV_VAR_TRIED_LAST=OFF

            # Fixes: error: 'value' is unavailable: introduced in macOS 10.13
            # Enable use of 'macports-libcxx' for macOS <= 10.12.  Copied from:
            # https://github.com/macports/macports-ports/commit/7a40798dec5597b30008e07601ffefbf60a01fcb
            legacysupport.use_mp_libcxx yes

            variant tiff description {Enable TIFF I/O} {
                depends_lib-append      port:tiff
                configure.args-replace  -DENABLE_TIFF=OFF -DENABLE_TIFF=ON
            }

            variant lto description {Enable Link Time Optimization} {
                configure.args-replace  -DENABLE_IPO=OFF -DENABLE_IPO=ON
            }
        }

        livecheck.type      regex
        livecheck.url       ${master_sites}
        if {${proj_major} eq ${latest_major}} {
            livecheck.regex     "${name}-(\\d+(?:\\.\\d+)*)${extract.suffix}"
        } else {
            livecheck.regex     "${name}-(${proj_major}(?:\\.\\d+)*)${extract.suffix}"
        }
    }
}

if {$subport eq $name} {
    PortGroup           stub 1.0

    description         Wrapper port for PROJ ${latest_major}
    long_description    {*}${description}

    supported_archs     noarch
    platforms           any

    depends_lib-append  port:proj9

    livecheck.type      none
}
