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

name                    kubectl
categories              sysutils
platforms               darwin
license                 Apache-2

maintainers             {@patarra gmail.com:patarra} \
                        {lbschenkel @lbschenkel} \
                        @pedrohdz \
                        openmaintainer

subport kubectl_select {}

# *NOTE* Remember to update `latestVersion` on a version upgrade.
set latestVersion       kubectl-1.15
subport kubectl-1.15 {
    set patchNumber     0
    checksums           rmd160  86573df886f85aa8be05247a1ec1158007ba1ca0 \
                        sha256  63f1ace419edffa1f5ebb64a6c63597afd48f8d94a61d4fb44e820139adbbe54 \
                        size    48587552
}

subport kubectl-1.14 {
    set patchNumber     4
    checksums           rmd160  92a520f847e959e04cf5d7602d8c419fa6c26d1d \
                        sha256  de3a28b0245d67b81b75f731b74a17def673ae4a87c98071421394888b5495d8 \
                        size    48702920
}

subport kubectl-1.13 {
    set patchNumber     8
    checksums           rmd160  efe299962efa7734da83e92a557047d8a69de7e9 \
                        sha256  7b788747a5536f5156b37c71489b8a5c18bd6ab8315db84d579e47db1eb9d56a \
                        size    44329992
}

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

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

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

if {${subport} == ${name}} {
    PortGroup           obsolete 1.0
    replaced_by         ${latestVersion}
    version             1.15.0
    revision            1

} elseif {${subport} == "kubectl_select"} {
    version             0.0.0
    revision            0
    supported_archs     noarch

    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
    }

    livecheck.type     none

} else {
    PortGroup           github 1.0
    supported_archs     x86_64
    depends_run         port:kubectl_select

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

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

    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 {
        file copy ${filespath}/kubectlX.YY.template ${workpath}/${baseName}
        reinplace "s|@@BASE_VERSION@@|${baseVersion}|g" ${workpath}/${baseName}
        reinplace "s|@@PORT_NAME@@|${subport}|g" ${workpath}/${baseName}
    }

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

        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     \"name\": \"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}
    "
}
