# -*- 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           java 1.0
PortGroup           select 1.0

name                apache-spark
categories          java parallel devel lang databases
maintainers         nomaintainer
license             Apache-2
homepage            https://spark.apache.org/
supported_archs     noarch
use_xcode           no

description         Engine for large-scale data processing
long_description \
    Apache Spark is a lightning-fast unified analytics engine for big data and machine \
    learning. It was originally developed at UC Berkeley in 2009. Apache Spark is a fast \
    and general-purpose cluster computing system. It provides high-level APIs in Java, \
    Scala and Python, and an optimized engine that supports general execution graphs. \
    It also supports a rich set of higher-level tools including Spark SQL for SQL and \
    structured data processing, MLlib for machine learning, GraphX for graph processing, and Spark Streaming.

# *NOTE* Remember to update latestVersion on a version upgrade.
set latestVersion   apache-spark-4.3

subport apache-spark-2.4 {
    version         2.4.8
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop2.7
    checksums       rmd160  aeea8553404c726951cc9b28fc449a63f741089b \
                    sha256  feedb945ab4906d098d9d71050a4dab1972de489423008489708ecf496ce91cd \
                    size    235899716
    java.version    8+
    java.fallback   openjdk8
    depends_run     port:sbt port:scala2.12 port:python37
}

subport apache-spark-3.0 {
    version         3.0.3
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop3.2
    checksums       rmd160  52d6aaaae488bd6111929a5af49c94b7c1481179 \
                    sha256  65dbff958be6ac5f22e850d0652544d75f8d8a81ce77b5517060a3606eb848a2 \
                    size    224762914
    java.version    11+
    java.fallback   openjdk11
    depends_run     port:sbt port:scala2.13 port:python38
}

subport apache-spark-3.1 {
    version         3.1.3
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop3.2
    checksums       rmd160  5cd49b89fa30f7975f89e9dd815bc38606d8e94d \
                    sha256  2411de04bec186b8651b27a5f16e4b511103c3c1e3f20fbb98b1f8804e61b77f \
                    size    231842529
    java.version    11+
    java.fallback   openjdk11
    depends_run     port:sbt port:scala2.12 port:python38
}

subport apache-spark-3.2 {
    version         3.2.4
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop3.2-scala2.13
    checksums       rmd160  88dcf4a869826b7a70223dcc7ac7cea7e79feab9 \
                    sha256  f863a7095339c5fdc62a4a0dec3c733b882629866c21a190bfe71429487cca01 \
                    size    307404268
    java.version    11+
    java.fallback   openjdk11
    depends_run     port:sbt port:scala2.13 port:python310
}

subport apache-spark-3.3 {
    version         3.3.4
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop3-scala2.13
    checksums       rmd160  923366083abedc683ffa2c2724a4094b09c4836f \
                    sha256  daba49050e3f2c89fa53f99d9ad1ee1391f3b329e8aa1b49318f2a22a8ad66fc \
                    size    306488887
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark-3.4 {
    version         3.4.4
    revision        0
    set archiveOnly 1
    distname        spark-${version}-bin-hadoop3-scala2.13
    checksums       rmd160  d086c38031fc3c597a08b1a394fe2f5716bdd062 \
                    sha256  fcee9644d932b55872657d47f8cf812098da243790e9ceb0c16c32333afe8146 \
                    size    397953108
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark-3.5 {
    version         3.5.10
    revision        0
    distname        spark-${version}-bin-hadoop3
    checksums       rmd160  0dad2446584559b7a19bacf63c246c11030815f7 \
                    sha256  e11fe2f474ad5f1bea0535efd6f7d9edb3f893aad866f16fda73fc43725f36d8 \
                    size    401381421
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.12 port:python311
}

subport apache-spark-4.0 {
    version         4.0.5
    revision        0
    distname        spark-${version}-bin-hadoop3
    checksums       rmd160  9ad0969aaa8c940c68e32a3b07c4c1372c04f713 \
                    sha256  24b26f8b11e8478cc72cd8ee20842994e9e6e9bcfe45a50abc8ad6111b67ae8b \
                    size    549108494
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark-4.1 {
    version         4.1.4
    revision        0
    distname        spark-${version}-bin-hadoop3
    checksums       rmd160  d2ecefee51124a9835768e1431e47a51bc469c28 \
                    sha256  e9430aa52bd947cc0d3f1299afe5089dcd262166cd202ba6aa77095b8541642b \
                    size    572969525
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark-4.2 {
    version         4.2.1
    revision        0
    distname        spark-${version}-bin-hadoop3
    checksums       rmd160  4ece55b95f38af0f46069896cc2f0ba17f1bded7 \
                    sha256  8766fd9132079ddf3f0b54a51796d05bf442871c62a51374ca9e18d345bb2bd6 \
                    size    555086267
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark-4.3 {
    version         4.3.0
    revision        0
    distname        spark-${version}-bin-hadoop3
    checksums       rmd160  66918afcabea30044e6967701a6def8876fd57ba \
                    sha256  48f4eb55fe42758b3aff52c027b310479c7afe96205eddff3869cd951fa02505 \
                    size    561204042
    java.version    17+
    java.fallback   openjdk17
    depends_run     port:sbt port:scala2.13 port:python311
}

subport apache-spark_select {}

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

    # The version is latestVersion's x.y line with .0, never a preview, so it
    # only increases when latestVersion moves to a newer line.
    set spark_latest_line [string range ${latestVersion} [string length ${name}-] end]
    replaced_by     ${latestVersion}
    epoch           1
    version         ${spark_latest_line}.0
    revision        0

    # Watch for a new release line, including its previews: compare only the
    # x.y line of what the CDN lists with latestVersion's. A newer line needs
    # a new subport and an update of latestVersion.
    livecheck.type      regex
    livecheck.url       https://dlcdn.apache.org/spark/
    livecheck.regex     spark-(\\d+\\.\\d+)\\.\\d+(?:-preview\\d+)?/
    livecheck.version   ${spark_latest_line}

} elseif {${subport} eq "apache-spark_select"} {
    version         0.1
    revision        0
    supported_archs noarch
    platforms       any

    description     common files for selecting the default apache-spark version
    long_description \
        apache-spark_select installs files that allow 'port select' to switch the \
        default version of apache-spark based on the version of Apache Spark requested. \
        It creates unsuffixed symlinks in the MacPorts prefix that point to the selected version.
    homepage        https://www.macports.org/

    distfiles
    use_configure   no
    build {}

    destroot {
        select::install apache-spark ${filespath}/base
        select::install apache-spark ${filespath}/none
    }

    livecheck.type  none

} else {
    # Current releases are served by the Apache CDN (dlcdn); once a release
    # ages out of the supported set it is removed from the CDN and remains
    # only on archive.apache.org. Archived versions are tagged with
    # 'archiveOnly' in their subport block. The archive fallback is always
    # kept so live versions stay installable after they later age off the CDN.
    if {[info exists archiveOnly]} {
        master_sites    https://archive.apache.org/dist/spark/spark-${version}/
    } else {
        master_sites    https://dlcdn.apache.org/spark/spark-${version}/ \
                        https://archive.apache.org/dist/spark/spark-${version}/
    }

    # vercmp sorts a preview after its own release (4.4.0-preview1 after
    # 4.4.0), so the epoch follows from the version: 0 on a preview and 1 on
    # a release, which makes the move from a line's last preview to its
    # release an upgrade. The lines listed here were added at a release and
    # never had a preview, so they keep epoch 0; a new line needs no change.
    set spark_lines_added_at_release {2.4 3.0 3.1 3.2 3.3 3.4 3.5 4.0 4.1 4.3}
    set spark_line      [join [lrange [split ${version} .] 0 1] .]
    set spark_preview   [regexp -- {-preview\d+$} ${version}]
    if {${spark_line} in ${spark_lines_added_at_release}} {
        if {${spark_preview}} {
            return -code error "${subport}: ${version} is a preview, but ${spark_line} is listed as added at a release"
        }
    } elseif {!${spark_preview}} {
        epoch       1
    }

    # Each subport follows one x.y release line. The CDN lists the newest
    # release of each current line, and a line's previews only until its
    # release is out, so a preview is reported while it is the newest
    # release of the line and is dropped once the release replaces it. For a
    # subport that is on a preview, livecheck reports that release as an
    # error ("older than"), because vercmp sorts a preview after its release:
    # that is the cue to update to the release, and the epoch above makes it
    # an upgrade. A line that has left the CDN no longer matches there: mark
    # it archiveOnly, which checks the archive for releases only.
    set spark_line_re   [quotemeta ${spark_line}]
    livecheck.type  regex
    if {[info exists archiveOnly]} {
        livecheck.url   https://archive.apache.org/dist/spark/
        livecheck.regex spark-(${spark_line_re}\\.\\d+)/
    } else {
        livecheck.url   https://dlcdn.apache.org/spark/
        livecheck.regex spark-(${spark_line_re}\\.\\d+(?:-preview\\d+)?)/
    }

    extract.suffix  .tgz
    extract.mkdir   yes
    use_configure   no

    depends_run-append  port:apache-spark_select

    select.group    apache-spark
    select.file     ${filespath}/${subport}

    build {}

    destroot {
        set sparkdir ${prefix}/share/${subport}

        xinstall -m 755 -d ${destroot}${prefix}/share

        file copy ${worksrcpath}/${distname} ${destroot}${sparkdir}

        foreach sparkcmd {
            beeline
            docker-image-tool.sh
            find-spark-home
            load-spark-env.sh
            pyspark
            run-example
            spark-class
            spark-connect-shell
            spark-pipelines
            spark-shell
            spark-sql
            spark-submit
            sparkR
        } {
            set sparkcmdpath ${worksrcpath}/${distname}/bin/${sparkcmd}
            if {[file exists ${sparkcmdpath}]} {
                xinstall -m 755 ${sparkcmdpath} ${destroot}${sparkdir}/bin
            }
        }
    }

    notes "
${subport} needs SPARK_HOME set, also when it runs through the commands that
'port select apache-spark' links into ${prefix}/bin, and PySpark needs
PYSPARK_PYTHON. To use ${subport}, add these lines to your shell startup
file (for example .profile, .bashrc or .zshrc):

    export SPARK_HOME=${prefix}/share/${subport}
    export PYSPARK_PYTHON=${prefix}/bin/python3

To follow whichever version 'port select apache-spark' selects instead, add
these lines (POSIX sh, so they work in sh, bash and zsh):

    _spark_sel=\$(readlink ${prefix}/etc/select/apache-spark/current 2>/dev/null)
    if \[ -n \"\$_spark_sel\" \] && \[ \"\$_spark_sel\" != none \] &&
       \[ -d \"${prefix}/share/\$_spark_sel\" \]; then
        export SPARK_HOME=\"${prefix}/share/\$_spark_sel\"
        export PYSPARK_PYTHON=${prefix}/bin/python3
    fi
    unset _spark_sel

After 'sudo port select --set apache-spark <version>', start a new shell
or source the file again to pick up the change.
"
}
