# -*- 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           compiler_blacklist_versions 1.0
PortGroup           github 1.0
PortGroup           muniversal 1.0

# Please keep the mongodb and mongodb-devel ports as similar as possible.

epoch               0
github.setup        mongodb mongo 6.0.7 r
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            1
checksums           rmd160  b50b1900daf9ee99b4337c410e4db47d1a57f26f \
                    sha256  32f6b4ad561a519da0b6a030064f011ebae1aa36f8fcaecd410d4b606cd8efc5 \
                    size    89667776 \
                    certifi-2022.12.7.tar.gz \
                    rmd160  c822033f4f62b6378eeba5381a494f7ad866f116 \
                    sha256  35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \
                    size    156897 \
                    charset-normalizer-2.0.12.tar.gz \
                    rmd160  f9bbde2f4ca286e47416d01eebd5b8c22cd8bff7 \
                    sha256  2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597 \
                    size    79105 \
                    Cheetah3-3.2.6.post1.tar.gz \
                    rmd160  280c221d47dd24d46f9b7e135effd4048fd1a8ef \
                    sha256  58b5d84e5fbff6cf8e117414b3ea49ef51654c02ee887d155113c5b91d761967 \
                    size    295992 \
                    Cython-0.29.33.tar.gz \
                    rmd160  63793b0ebfdc73b852a91bdd7c78e834e2ce643f \
                    sha256  5040764c4a4d2ce964a395da24f0d1ae58144995dab92c6b96f44c3f4d72286a \
                    size    2093693 \
                    flit_core-3.8.0.tar.gz \
                    rmd160  33b542a424bdccc186af59b39ce4c2f1e6608783 \
                    sha256  b305b30c99526df5e63d6022dd2310a0a941a187bd3884f4c8ef0418df6c39f3 \
                    size    41224 \
                    idna-3.4.tar.gz \
                    rmd160  a6a010b1d9e750b1ee52a2eef54d94ce38ced0ba \
                    sha256  814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4 \
                    size    183077 \
                    oauthlib-3.1.1.tar.gz \
                    rmd160  1ee81d217311e971298be031730cc72a5dc4bafd \
                    sha256  8f0215fcc533dd8dd1bee6f4c412d4f0cd7297307d43ac61666389e3bc3198a3 \
                    size    161395 \
                    packaging-21.3.tar.gz \
                    rmd160  45ed4b85bf4ef2069e07dd2922849f7edf98363a \
                    sha256  dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
                    size    84848 \
                    pkce-1.0.3.tar.gz \
                    rmd160  5001b60d76c136c98e47f6bb9ee2d5bac9e4e924 \
                    sha256  9775fd76d8a743d39b87df38af1cd04a58c9b5a5242d5a6350ef343d06814ab6 \
                    size    2757 \
                    poetry_core-1.5.0.tar.gz \
                    rmd160  3c5a0186f2c9a9995cb0d6dbca3095511f08bca3 \
                    sha256  253521bb7104e1df81f64d7b49ea1825057c91fa156d7d0bd752fefdad6f8c7a \
                    size    448812 \
                    psutil-5.8.0.tar.gz \
                    rmd160  ac0a0c786848276d50cfac4c5dfc67254c1d4763 \
                    sha256  0c9ccb99ab76025f2f0bbecf341d4656e9c1351db8cc8a03ccd62e318ab4b5c6 \
                    size    470886 \
                    pymongo-3.13.0.tar.gz \
                    rmd160  615234a930e514cb9889b72a2090c323ffe8b84c \
                    sha256  e22d6cf5802cd09b674c307cc9e03870b8c37c503ebec3d25b86f2ce8c535dc7 \
                    size    804388 \
                    pyparsing-3.0.9.tar.gz \
                    rmd160  c450c72db367849469516a28bcbf208cdb2e3d27 \
                    sha256  2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
                    size    1999906 \
                    PyYAML-6.0.tar.gz \
                    rmd160  da0f60184d72f5a360b297d0131f5a5b08086abf \
                    sha256  68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2 \
                    size    124996 \
                    regex-2021.11.10.tar.gz \
                    rmd160  b4e567eb32eb5e19d39f8859dcaa57fe15adeb54 \
                    sha256  f341ee2df0999bfdf7a95e448075effe0db212a59387de1a70690e4acb03d4c6 \
                    size    702813 \
                    requests-2.26.0.tar.gz \
                    rmd160  0b532167e01570e015b9abd52bb7d442d93a50bd \
                    sha256  b8aa58f8cf793ffd8782d3d8cb19e66ef36f7aba4353eec859e74678b01b07a7 \
                    size    104433 \
                    requirements-parser-0.3.1.tar.gz \
                    rmd160  29cbe92d031f6ff3d8029611a3ae333fa62af257 \
                    sha256  3852277618e653dd1d8fa4129e59b4338358dffafeb3d5106e9f88504db9c460 \
                    size    10080 \
                    setuptools-67.0.0.tar.gz \
                    rmd160  f66e6d08f450a3102bc50f6c72cceb7a05b5ff9f \
                    sha256  883131c5b6efa70b9101c7ef30b2b7b780a4283d5fc1616383cdf22c83cbefe6 \
                    size    2474556 \
                    types-PyYAML-6.0.12.3.tar.gz \
                    rmd160  f55298cbfad692812f1f16f3e4ecb9a2e97b4fbe \
                    sha256  17ce17b3ead8f06e416a3b1d5b8ddc6cb82a422bb200254dd8b469434b045ffc \
                    size    10910 \
                    types-setuptools-57.4.12.tar.gz \
                    rmd160  cc8e250b129a0ad95ed89f69dcf139c393dd9291 \
                    sha256  415a1c23101a05da17eb66bed5d5a865702e5a69f74c66dbf1af643dce9492ab \
                    size    16205 \
                    urllib3-1.26.14.tar.gz \
                    rmd160  272bb6346e1c66ef3d2dffb2b7515f56125eec20 \
                    sha256  076907bf8fd355cde77728471316625a4d2f7e713c125f51953bb5b3eecf4f72 \
                    size    300665 \
                    wheel-0.42.0.tar.gz \
                    rmd160  25c120a08158cb9c9d642a6a900e49dab44b0c12 \
                    sha256  c45be39f7882c9d34243236f2d63cbd58039e360f85d0913425fbd7ceea617a8 \
                    size    98667

name                mongodb-devel
conflicts           mongodb
license             SSPL
categories          databases
platforms           {darwin >= 18}
maintainers         {ryandesign @ryandesign}

description         high-performance, schema-free, document-oriented (\"NoSQL\") database

long_description    MongoDB is a {*}${description}. The ${name} port \
                    tracks the rapid release cycle which makes new \
                    features available faster but is not supported.

homepage            https://www.mongodb.com/community
master_sites        https://fastdl.mongodb.org/src:mongodb
distname            mongodb-src-r${version}
dist_subdir         mongodb
extract.only        ${distfiles}
checksums-prepend   ${distfiles}
distfiles           ${distfiles}:mongodb

for {set index 1} {${index} < [llength ${checksums}]} {incr index} {
    set item [lindex ${checksums} ${index}]
    switch ${item} {
        md5 -
        sha1 -
        sha256 -
        size -
        rmd160 {
            incr index
        }
        default {
            regsub -- {-\d.*$} ${item} {} tag
            distfiles-append ${item}:${tag}
            master_sites-append https://files.pythonhosted.org/packages/source/[string index ${tag} 0]/${tag}/:${tag}
        }
    }
}

set python_branch   3.11
set python_version  [string map {. {}} ${python_branch}]

depends_build       port:python${python_version} \
                    port:libyaml

depends_lib         port:curl \
                    port:cyrus-sasl2 \
                    port:libpcap \
                    port:libstemmer \
                    port:pcre \
                    port:snappy \
                    port:yaml-cpp \
                    port:zlib \
                    port:zstd

post-extract {
    file mkdir ${workpath}/distfiles
    foreach distfile_and_tag [lrange ${distfiles} 1 end] {
        set distfile [lindex [split ${distfile_and_tag} {:}] 0]
        ln -s ${distpath}/${distfile} ${workpath}/distfiles/
    }
}

patchfiles          compile.req.patch \
                    platform.req.patch

pre-configure {
    # Upstream recommends installing specific versions of the required
    # Python modules modules in a virtualenv. They do not support using
    # newer versions of those modules such as the versions that we have
    # in MacPorts. https://jira.mongodb.org/browse/SERVER-62686
    system -W ${workpath} "${prefix}/bin/python${python_branch} -m venv venv"
}

configure.env       "MAKEFLAGS=-j${build.jobs}"
configure.cmd       ${workpath}/venv/bin/pip
configure.pre_args  install
configure.args      --find-links ${workpath}/distfiles \
                    --no-index \
                    --requirement etc/pip/compile-requirements.txt \
                    --verbose \
                    --verbose

compiler.c_standard 2011
compiler.cxx_standard 2017

# This matches a version check of __apple_build_version__ in SConstruct.
# https://jira.mongodb.org/browse/SERVER-62503
compiler.blacklist-append {clang < 1300.0.29}

# It sets its own optimization flags via the --opt flag.
configure.optflags

# error: integer value -1 is outside the valid range of values [0, 3] for this
# enumeration type [-Wenum-constexpr-conversion]
# https://jira.mongodb.org/browse/SERVER-85825
configure.cxxflags-append \
                    -Wno-error=enum-constexpr-conversion

# -L flags are handled via LIBPATH.
configure.ldflags-delete -L${prefix}/lib

build.cmd           ${workpath}/venv/bin/python buildscripts/scons.py
build.target        install-core
build.args          CC="${configure.cc}" \
                    CFLAGS="${configure.cflags}" \
                    CPPPATH="${prefix}/include" \
                    CXX="${configure.cxx}" \
                    CXXFLAGS="${configure.cxxflags}" \
                    DESTDIR="${destroot}" \
                    LIBPATH="${prefix}/lib" \
                    PREFIX="${prefix}" \
                    --disable-warnings-as-errors=configure \
                    --disable-warnings-as-errors=source \
                    --enable-http-client=on \
                    --install-action=hardlink \
                    --libc++ \
                    --opt=size \
                    --release \
                    --ssl=on \
                    --use-sasl-client \
                    --use-system-pcre \
                    --use-system-snappy \
                    --use-system-stemmer \
                    --use-system-yaml \
                    --use-system-zlib \
                    --use-system-zstd \
                    --wiredtiger=on

# error: no template named 'unary_function' in namespace 'std'; did you mean '__unary_function'?
# https://jira.mongodb.org/browse/SERVER-85779
build.args-append   CPPDEFINES=BOOST_NO_CXX98_FUNCTION_BASE

# It should build with --use-system-asio, but doesn't:
# TODO: Report bug and put link here

# It should build with --use-system-icu, but doesn't:
# TODO: Report bug and put link here

# Do not build mongoc_embedded.
# See https://jira.mongodb.org/browse/SERVER-44592
build.args-append   --use-system-mongo-c=off

# Specify a short variant dir because the default variant dir is deeply nested
# and can be very long, so long that on some systems the build will fail with
# "Argument list too long"; see https://jira.mongodb.org/browse/SERVER-13829
build.args-append   VARIANT_DIR=MP

# i386 was never supported on macOS.
# See https://jira.mongodb.org/browse/SERVER-22810
supported_archs     arm64 x86_64

array set merger_build_args {arm64 "TARGET_ARCH=aarch64" x86_64 "TARGET_ARCH=x86_64"}
foreach a ${supported_archs} {
    lappend merger_build_args($a) \
                    CCFLAGS="-arch ${a}" \
                    LINKFLAGS="${configure.ldflags} -arch ${a}" \
}

if {![info exists universal_possible]} {
    set universal_possible [expr {${os.universal_supported} && [llength ${configure.universal_archs}] >= 2}]
}
if {(!${universal_possible} || ![variant_isset universal]) && [info exists merger_build_args(${build_arch})]} {
    build.args-append {*}$merger_build_args(${build_arch})
}

set dbdir           ${prefix}/var/db/mongodb
set logdir          ${prefix}/var/log/mongodb
set docdir          ${prefix}/share/doc/mongodb
set mongouser       _mongo
add_users           ${mongouser} group=${mongouser} realname=MongoDB\ Server

destroot {
    # The programs have already been installed into the destroot during
    # the build phase because I don't know how to prevent it from doing
    # that. Documentation files have also already been installed to an
    # unreasonable location so move them.
    xinstall -d ${destroot}${docdir}
    foreach f [glob ${destroot}${prefix}/*] {
        if {[file isfile ${f}]} {
            move ${f} ${destroot}${docdir}
        }
    }

    xinstall -m 0755 -o ${mongouser} -g ${mongouser} -d \
        ${destroot}${dbdir} \
        ${destroot}${logdir}
    destroot.keepdirs-append  \
        ${destroot}${dbdir} \
        ${destroot}${logdir}
}

startupitem.create      yes
startupitem.executable  sudo -u ${mongouser} ${prefix}/bin/mongod --dbpath ${dbdir} --logpath ${logdir}/mongodb.log --logappend

if {${os.platform} eq "darwin" && ${os.major} >= 16 && [vercmp ${os.version} 18.2.0] < 0} {
    notes-append {
        Warning: MONGODB MAY LOSE DATA if it crashes on macOS 10.14.0 or\
        earlier and the database is on an APFS volume. This problem is fixed\
        in macOS 10.14.1 and later so please update your operating system or\
        make sure your database is on a Mac OS Extended (Journaled) volume.\
        See https://jira.mongodb.org/browse/WT-4112 for more information.
    }
}

github.livecheck.regex  {([0-9.]+)}