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

github.setup    timescale timescaledb 2.22.0
github.tarball_from archive
revision        0
license         Apache-2
description     A time-series database that integrates with PostgreSQL.
maintainers     {blair @blair} openmaintainer
categories      databases
conflicts       timescaledb1
homepage        https://www.timescaledb.com/

long_description \
                TimescaleDB is an open-source database designed to \
                make SQL scalable for time-series data. It is \
                engineered up from PostgreSQL, providing automatic \
                partitioning across time and space (partitioning key), \
                as well as full SQL support. TimescaleDB is packaged \
                as a PostgreSQL extension.

checksums       rmd160  0d9f8fd7d0bff255729fdd47ecfa9445263c1d10 \
                sha256  21a14f1c9d8faf9178bffe9aed711b8d06162594b2d3f89f3239a84a67616e04 \
                size    8139369

depends_build   path:bin/cmake:cmake \
                port:perl${perl5.major} \
                port:p${perl5.major}-ipc-run

set postgresql_branches {12 13 14 15 16 17}

foreach branch ${postgresql_branches} {
    subport pg${branch}-${name} {
        set pg postgresql${branch}
        depends_lib-append      port:${pg}
        configure.env-append    PATH=${prefix}/lib/${pg}/bin:$::env(PATH)
    }
}

# End of support branches
subport pg12-${name} {
    # 2.11.2 is the last version supported for PostgreSQL 12.x.
    # https://github.com/timescale/timescaledb/releases/tag/2.12.0
    github.setup    timescale timescaledb 2.11.2
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision        0
    checksums       rmd160  032705bda2f125dae286df68e800c664b0233663 \
                    sha256  06cf5047bb6cd03058724d6c91c29a871e419074ca92770302d182cd6ccedf0b \
                    size    7729279
}

subport pg13-${name} {
    # 2.15.3 is the last version supported for PostgreSQL 13.x.
    # https://github.com/timescale/timescaledb/releases/tag/2.16.0
    github.setup    timescale timescaledb 2.15.3
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision        0
    checksums       rmd160  b94f13ddf31c7e2a79e95d3d1b30ef23ffc4de5c \
                    sha256  70b7aa63558323f60e063c870456fa604d8b89fba8e6dab94aa26c9472b99942 \
                    size    7441397
}

subport pg14-${name} {
    # 2.19.3 is the last version supported for PostgreSQL 14.x.
    # https://github.com/timescale/timescaledb/releases/tag/2.20.0
    github.setup    timescale timescaledb 2.19.3
    github.tarball_from archive
    revision        0
    checksums       rmd160  0c5880a084e02a2cf918b29557c1401458fa4bb4 \
                    sha256  ef2c9019f3e292f77e274a95386e550cb8c358ec0e053f6b7097565f61381e01 \
                    size    8010948
}

compiler.c_standard     2011
# Work around MacPorts base C11 compiler selection bug.
# https://github.com/macports/macports-base/pull/196
compiler.blacklist-append {clang < 500}

configure.cmd           ./bootstrap
configure.pre_args

# Do not remove -DAPACHE_ONLY from configure.args otherwise some
# binaries may be built that are licensed under the Timescale License
# (TSL). See https://github.com/timescale/timescaledb#timescaledb and
# https://github.com/timescale/timescaledb/blob/master/tsl/LICENSE-TIMESCALE.
# However, using +timescale_license will enable Timescale License'd code.
#
# -DLINTER=off: see https://github.com/timescale/timescaledb/issues/2889
configure.args          -DAPACHE_ONLY=1 \
                        -DLINTER=off \
                        -DPROJECT_INSTALL_METHOD=macports

build.dir               ${worksrcpath}/build

# @holymonson 2024-10-30: for smooth upgrade from legacy, kill this hack sometime later.
# https://trac.macports.org/wiki/PortfileRecipes#deactivatehack
pre-activate {
    if {![catch {lindex [registry_active timescaledb] 0} installed]} {
        set _version [lindex $installed 1]
        if {[vercmp $_version 2.16.1] < 0} {
            registry_deactivate_composite timescaledb "" [list ports_nodepcheck 1]
        }
    }
}

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

variant timescale_license description {Enable Timescale License code, license will be Timescale License} {
    configure.args-delete   -DAPACHE_ONLY=1
    license                 Timescale-License
}

if {${name} eq ${subport}} {
    # make it a stub port like py-foo
    supported_archs noarch
    use_configure no
    extract     {}
    build       {}
    destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${subport}
        system "echo $name is a stub port > ${destroot}${prefix}/share/doc/${name}/README"
    }

    set latest_branch [lindex ${postgresql_branches} end]
    depends_lib     port:pg${latest_branch}-${name}
}
