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

name                    kubectl
categories              sysutils
license                 Apache-2
homepage                https://kubectl.docs.kubernetes.io/
installs_libs           no

maintainers             {@patarra gmail.com:patarra} \
                        {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

subport kubectl_select {}

set source_build        no
set offline_build       yes

# *NOTE* Remember to update `latestVersion` on a version upgrade.
set latestVersion       kubectl-1.36

# When source_build is yes, the checksum should be the one for the
# kubernetes source tarball, instead of the zipped pre-built binary.

subport kubectl-1.36 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     2
    revision            0
    checksums           rmd160  9863dab4aec4cc172283e552abb313944193a77a \
                        sha256  ffbc46612cf32ad08a3b606de064b63afc350ae1dd1a691e99b5cd127d716c26 \
                        size    40606064
}

subport kubectl-1.35 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     6
    revision            0
    checksums           rmd160  c75f7097065878aa44dc077be52ff9152abe71f1 \
                        sha256  2705457cc1a6f9e09f2849e317eb6117d6f0af6dbabefe6f839fbb9df62c6e5f \
                        size    39185264
}

subport kubectl-1.34 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     9
    revision            0
    checksums           rmd160  6fba79f526d012999014a3bc52389b67f8965ab9 \
                        sha256  a6949a78bd93f97dd2b0b2abf736eb2907a9b1a9739bf71d340f7aa95a799ce2 \
                        size    38367952
}

subport kubectl-1.33 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     13
    revision            0
    checksums           rmd160  9d2fa17e1c3fcc05b13ad9806145e4e80a560cc5 \
                        sha256  6956d572684a8364c9172e102985bcf2aff03289db777900cf5bf18e7883d39e \
                        size    37378128
}

subport kubectl-1.32 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     13
    revision            0
    checksums           rmd160  15f6b01ab45e37c0f27b065284bd9096716eb3e0 \
                        sha256  3b48c21be36b5d7e387dcd7bd1914b223b3aeb55ae7ded4cdc5244dd943accc1 \
                        size    36396395
}

subport kubectl-1.31 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     14
    revision            0
    checksums           rmd160  5e2847bde05b83373cb79712f17bdb9d0d52851f \
                        sha256  ddca4935c8b6a3b4b6cd896c6b24bdd1b17d7a7004d12fca8b8b01ac727ddc5a \
                        size    36611046
}

subport kubectl-1.30 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     14
    revision            0
    checksums           rmd160  d0a81f509c0a71f5addbd0e141b18e532af1b491 \
                        sha256  ad003cc133346d20ae091a540a42bf9adbcf124ec2959004a636fd1e9694f534 \
                        size    39640438
}

subport kubectl-1.29 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     15
    revision            0
    checksums           rmd160  fbd20875ebf4ac2a1d31ad18032001812b7b1aa7 \
                        sha256  306214328ebdc42b274e7e80a5f6850e95f5b8da05123e3f92041caead0880d1 \
                        size    41223042
}

subport kubectl-1.28 {
    set source_build    yes
    set offline_build   no
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     15
    revision            0
    checksums           rmd160  66f7671f9b665e1f8f8aa5bdf4f697abf418b147 \
                        sha256  2d6d0a2a9e1cf207ac3090f9ad37a8f12d061b568973fcee91af9ba7c0292cb3 \
                        size    40046030
}

subport kubectl-1.27 {
    set source_build    yes
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     16
    revision            0
    checksums           rmd160  11d99ae884f611b8cec2098eb523bafd8233f6aa \
                        sha256  e1d9d96de9103e0982727acfb3d59172c7f67fa8e5371a627eaa15cd1236fe5d \
                        size    39231976
}

subport kubectl-1.26 {
    set source_build    yes
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     15
    revision            0
    checksums           rmd160  52a005df44f39f10d57fb10ecf606e867d1f90de \
                        sha256  10ab7b287996c04dd584e781808be52a090987f893bc34008ca5321e8c414aa2 \
                        size    41444383
}

subport kubectl-1.25 {
    set source_build    yes
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     16
    revision            0
    checksums           rmd160  c11e7bc1034290339e06d12d069cd75900b3666e \
                        sha256  c57dd9448066c59eb0eb9c0b595a42caeb461403c0fc9bcbb784d43da2f4a971 \
                        size    38714770
}

subport kubectl-1.24 {
    set source_build    yes
    supported_archs     x86_64 arm64
    platforms           {darwin >= 17}

    set patchNumber     17
    revision            0
    checksums           rmd160  b20e891c34db6aa6405f5006467b2614861a7d0b \
                        sha256  e93cd124db66533f9f7b340af4602a2a69bcd424a7ba4db4011557085d5efe2c \
                        size    38157141
}

subport kubectl-1.23 {
    set source_build    yes
    supported_archs     x86_64 arm64

    set patchNumber     17
    revision            0
    checksums           rmd160  b7f36ce95472e256c5dd5d2f4fee1fa08502d184 \
                        sha256  def840469468254f2b437cb21404b0a76a81b8ee2112583d7f0bdb52e0de1c18 \
                        size    38135738
}

subport kubectl-1.22 {
    set source_build    yes
    supported_archs     x86_64 arm64

    set patchNumber     17
    revision            0

    checksums           rmd160  aed0b0cff951dcdebb692f69e932f133b82aa364 \
                        sha256  d6082dfe3048339921736085167f8878abcec09c839d05ba2e3a8248adfccb11 \
                        size    36116829
}

subport kubectl-1.21 {
    set patchNumber     14
    revision            0

    # Use pre-built version to resolve https://trac.macports.org/ticket/63503
    checksums           rmd160  15e92202b68b15776bcc045f753985541ff60339 \
                        sha256  30c529fe2891eb93dda99597b5c84cb10d2318bb92ae89e1e6189b3ae5fb6296 \
                        size    52867344
}

subport kubectl-1.20 {
    set source_build    yes
    supported_archs     x86_64
    platforms           {darwin < 22}

    set patchNumber     15
    revision            0

    # because source_build is yes, this is the checksum of the kubernetes
    # source tarball
    checksums           rmd160  5fbd8e7c81a3d9c2012143e350a78c4440688fe7 \
                        sha256  527289e39bd202f49dbe10d10b3f9856a01de4da68d4ab973deb422f6cf4d034 \
                        size    34555426
}

subport kubectl-1.19 {
    set source_build    yes
    supported_archs     x86_64
    platforms           {darwin < 22}

    set patchNumber     16
    revision            0

    # because source_build is yes, this is the checksum of the kubernetes
    # source tarball
    checksums           rmd160  9a32c6ea4aae6871fe36e435f6e952d0ae837d79 \
                        sha256  0879f67d8a1b1a1a59952c6104761786e41d4db5065de37680f2816734854070 \
                        size    33574817
}

subport kubectl-1.18 {
    set patchNumber     20
    revision            0
    checksums           rmd160  8f4313fecacbb6169305754538a06c61423227f7 \
                        sha256  bc709378c27da458b31395787a74cd9ac58dce7dbe2a7ba92f8bc2221eeed0be \
                        size    50095696
}

subport kubectl-1.17 {
    set patchNumber     17
    revision            0
    checksums           rmd160  4710ba1c832cd9ac6917600bcd54c70eb2b46666 \
                        sha256  e76b57bbed823a8572f7ccbf9caae56855048434d818dc13559e89ead5f91578 \
                        size    49542736
}

subport kubectl-1.16 {
    set patchNumber     15
    checksums           rmd160  7450d9abe9aa07c1929b39e96b9e1995257b42ae \
                        sha256  aff54bfaaed905813f61a2d0ca039176d6d309e59f92ebdb297c7da1df105485 \
                        size    48907792
}

subport kubectl-1.15 {
    set patchNumber     12
    checksums           rmd160  879ef587b357fa4108ca239527e8dd51186ad65d \
                        sha256  1b06cab9ee7988f8e71c48dd1d9379aa7c14123cbbc63e12ab5342c3e9130972 \
                        size    48668112
}

subport kubectl-1.14 {
    set patchNumber     10
    checksums           rmd160  10bdebc38c14482972e0c344d349a6d450c8d182 \
                        sha256  43d2c24eafb2ef09a6ac77c2b99070668e83edaa325a16a362e304ba578fdc48 \
                        size    48716328
}

subport kubectl-1.13 {
    set patchNumber     12
    checksums           rmd160  fdbdf38601ed7767793a4e8540e8454d43c8e91c \
                        sha256  ddbdc7591569f321b8b0a01dcbd679f6b0a7756f1427a51a39eadfce8d34bea7 \
                        size    44353656
}

subport kubectl-1.12 {
    platforms           {darwin < 21}
    set patchNumber     10
    checksums           rmd160  4212a9f7709ef6a7d36f3ebd477140a7d0ef8e50 \
                        sha256  67acf17bf77b0a9729d17ad5c058a358c0cbd12dbf46478c37111cdd97a0828c \
                        size    56860112
}

subport kubectl-1.11 {
    platforms           {darwin < 21}
    set patchNumber     10
    checksums           rmd160  8c3f3121088f745bb9eb279f5531b8b61ff84bc7 \
                        sha256  cf27dd3e8d5a13439de2214b7a07e2371cdbeaf151ee4c7d63e7e7c34ebbf117 \
                        size    54985744
}

subport kubectl-1.10 {
    platforms           {darwin < 21}
    set patchNumber     13
    checksums           rmd160  fe455ab0eb7c862c8dbce970632a9e66c8987cb1 \
                        sha256  94ec5409ac6715dfec11309795da9b30254b9d818f8b1d38d1ca2e914945868d \
                        size    54670032
}

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

    replaced_by         ${latestVersion}
    version             1.36.2
    revision            0

} elseif {${subport} == "kubectl_select"} {
    version             0.0.0
    revision            1
    supported_archs     noarch
    platforms           any

    description         Common files for selecting the default kubectl version
    long_description    This port installs files that allow 'port select' \
                        to create links to the preferred default version \
                        of kubectl.
    homepage            https://www.macports.org/

    distfiles
    use_configure       no
    build {}

    destroot {
        xinstall -d ${destroot}${prefix}/share/bash-completion/completions
        select::install kubectl ${filespath}/base
        select::install kubectl ${filespath}/none
    }

    destroot.keepdirs-append ${destroot}${prefix}/share/bash-completion/completions

    livecheck.type     none

} else {

    set baseVersion     [lindex [split ${subport} "-"] 1]
    set patchVersion    ${baseVersion}.${patchNumber}
    set baseName        kubectl${baseVersion}

    if ${source_build} {
        # Build kubectl from source

        PortGroup           golang 1.0

        go.setup            github.com/kubernetes/kubernetes ${patchVersion} v
        go.package          k8s.io/kubernetes
        go.offline_build    ${offline_build}

        build.cmd           make
        build.target        kubectl

        use_parallel_build  no

        depends_build-append \
                            port:bash

        destroot {
            xinstall ${worksrcpath}/_output/bin/kubectl \
                ${destroot}${prefix}/bin/kubectl${baseVersion}
        }
    } else {
        # Use Darwin amd64 pre-built binary

        PortGroup           github 1.0

        github.setup        kubernetes kubectl ${patchVersion}
        supported_archs     x86_64

        master_sites        https://storage.googleapis.com/kubernetes-release/release/v${version}/bin/darwin/amd64
        distname            kubectl
        dist_subdir         ${name}/${version}
        extract.suffix
        extract.only

        use_configure       no

        build {}

        destroot {
            xinstall ${distpath}/kubectl \
                ${destroot}${prefix}/bin/kubectl${baseVersion}

            # legacysupport tweaks don't work, since the install here is from
            # a binary tarball ... have to tweak the binary to use the legacy
            # support library, which in turn uses the System.B library.
            legacysupport::relink_libSystem ${destroot}${prefix}/bin/kubectl${baseVersion}
        }
    }

    depends_run         port:kubectl_select

    description         Kubernetes cluster CLI
    long_description    Command line interface for running commands against \
                        Kubernetes clusters

    post-build {
        file copy ${filespath}/kubectlX.YY.template ${workpath}/${baseName}
        reinplace "s|@@BASE_VERSION@@|${baseVersion}|g" ${workpath}/${baseName}
        reinplace "s|@@PORT_NAME@@|${subport}|g" ${workpath}/${baseName}
    }

    post-destroot {
        set completionsPath ${destroot}${prefix}/share/${subport}/completion
        xinstall -d ${completionsPath}
        exec ${destroot}${prefix}/bin/kubectl${baseVersion} completion bash \
            > ${completionsPath}/${name}.bash
    }

    # Skip -rc and other pre-release tags
    set regexVersionPart [string map {. {\.}} $baseVersion]
    livecheck.type      regex
    livecheck.url       https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100
    livecheck.regex     \".+releases/tag/v(${regexVersionPart}\\.\\d+)\"

    # Generate select file
    select.group        kubectl
    select.file         ${workpath}/${baseName}

    notes "
    To make this the default kubectl run:
        sudo port select --set kubectl ${baseName}
    "
}
