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

name                google-cloud-sdk
version             575.0.1
revision            0
categories          devel python
license             Apache-2
maintainers         {breun @breun} openmaintainer
description         Command-line interface for Google Cloud Platform products and services
long_description    Google Cloud SDK is a set of tools for Google Cloud Platform. \
                    It contains gcloud, gsutil, and bq command-line tools, which you can \
                    use to access Compute Engine, Cloud Storage, BigQuery, and other \
                    products and services from the command-line. You can run these tools \
                    interactively or in your automated scripts.

platforms           {darwin any}
supported_archs     i386 x86_64 arm64

if { ${configure.build_arch} eq "i386" } {
    set arch_classifier x86
    checksums       rmd160  d0d5075ff0e7d795bb45786721804b3f13a40068 \
                    sha256  bd24ed83e7c5933c551cddbf47f333b8ace86809625fc64cc52ab4bc8f05aca3 \
                    size    59528913
} elseif { ${configure.build_arch} eq "x86_64" } {
    set arch_classifier x86_64
    checksums       rmd160  6138a270ea8b8c56b13d38a9e1cbacaaac08c9ec \
                    sha256  f59f942bf79de62a2425c6cee90394fb13e4bdb50ea11aec68e3692b8d46ada5 \
                    size    61185597
} elseif { ${configure.build_arch} eq "arm64" } {
    set arch_classifier arm
    checksums       rmd160  7d1dfc9467a14b79f68204dca5ec78badb107f6e \
                    sha256  616b4c88b8f0e068e8db56ab86329de48a7692aa8bb26cfb22a6199b6a027255 \
                    size    61093561
} else {
    set arch_classifier invalid
}

distname            ${name}-${version}-darwin-${arch_classifier}

homepage            https://cloud.google.com/sdk/
master_sites        https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/

worksrcdir          ${name}

# Most recent Python version that is compatible with both
# glcoud (https://docs.cloud.google.com/sdk/docs/install-sdk#mac) and
# gsutil (https://docs.cloud.google.com/storage/docs/gsutil_install#specifications)
python.default_version 314

# MacPorts variants are not allowed to contain '-' and gcloud component names use both '-' and '_',
# so each variant adds an explicit mapping from variant name to gcloud component name.
# See https://cloud.google.com/sdk/docs/components for more info about gcloud components.
# Current list of available components is not documented by Google, but can be found via: gcloud components list
# 'gcloud', 'gcloud-crc32c', 'bq', 'gsutil' and 'core' components are installed by default, so are not offered as variants.
set variant_to_component [dict create]
variant alpha description {Add gcloud CLI Alpha Commands} { dict set variant_to_component alpha alpha }
variant anthos_auth description {Add Anthos auth} { dict set variant_to_component anthos_auth anthos-auth }
variant app_engine_go description {Add App Engine Go Extensions} { dict set variant_to_component app_engine_go app-engine-go }
variant app_engine_java description {Add gcloud app Java Extensions} { dict set variant_to_component app_engine_java app-engine-java }
variant app_engine_python description {Add gcloud app Python Extensions} { dict set variant_to_component app_engine_python app-engine-python }
variant app_engine_python_extras description {Add gcloud app Python Extensions (Extra Libraries)} { dict set variant_to_component app_engine_python_extras app-engine-python-extras }
variant appctl description {Add Appctl} { dict set variant_to_component appctl appctl }
variant beta description {Add gcloud CLI Beta Commands } { dict set variant_to_component beta beta }
variant bigtable description {Add Cloud Bigtable Emulator} { dict set variant_to_component bigtable bigtable }
variant cbt description {Add Cloud Bigtable Command Line Tool} { dict set variant_to_component cbt cbt }
variant cloud_datastore_emulator description {Add Cloud Datastore Emulator} { dict set variant_to_component cloud_datastore_emulator cloud-datastore-emulator }
variant cloud_firestore_emulator description {Add Cloud Firestore Emulator} { dict set variant_to_component cloud_firestore_emulator cloud-firestore-emulator }
variant cloud_run_proxy description {Add Cloud Run Proxy} { dict set variant_to_component cloud_run_proxy cloud-run-proxy }
variant cloud_sql_proxy description {Add Cloud SQL Proxy} { dict set variant_to_component cloud_sql_proxy cloud-sql-proxy }
variant config_connector description {Add config connector} { dict set variant_to_component config_connector config-connector }
variant docker_credential_gcr description {Add Google Container Registry's Docker credential helper} { dict set variant_to_component docker_credential_gcr docker-credential-gcr }
variant enterprise_certificate_proxy description {Add Enterprise certificate proxy} { dict set variant_to_component enterprise_certificate_proxy enterprise-certificate-proxy }
variant gke_gcloud_auth_plugin description {Add GKE Google Cloud auth plugin} { dict set variant_to_component gke_gcloud_auth_plugin gke-gcloud-auth-plugin }
variant istioctl description {Add istioctl} { dict set variant_to_component istioctl istioctl }
variant kpt description {Add kpt} { dict set variant_to_component kpt kpt }
variant kubectl description {Add kubectl} { dict set variant_to_component kubectl kubectl }
variant kubectl_oidc description {Add kubectl-oidc} { dict set variant_to_component kubectl_oidc kubectl-oidc }
variant kustomize description {Add Kustomize} { dict set variant_to_component kustomize kustomize }
variant local_extract description {Add On-Demand Scanning API extraction helper} { dict set variant_to_component local_extract local-extract }
variant log_streaming description {Add Log Streaming} { dict set variant_to_component log_streaming log-streaming }
variant managed_flink_client description {Add managed Flink client} { dict set variant_to_component managed_flink_client managed-flink-client }
variant minikube description {Add Minikube} { dict set variant_to_component minikube minikube }
variant nomos description {Add Nomos CLI} { dict set variant_to_component nomos nomos }
variant package_go_module description {Add Artifact Registry Go Module Package Helper} { dict set variant_to_component package_go_module package-go-module }
variant pkg description {Add pkg} { dict set variant_to_component pkg pkg }
variant preview description {Add gcloud CLI Preview Commands} { dict set variant_to_component preview preview }
variant pubsub_emulator description {Add Cloud Pub/Sub Emulator} { dict set variant_to_component pubsub_emulator pubsub-emulator }
variant spanner_cli description {Add Spanner CLI} { dict set variant_to_component spanner_cli spanner-cli }
variant spanner_migration_tool description {Add Spanner migration tool} { dict set variant_to_component spanner_migration_tool spanner-migration-tool }
variant skaffold description {Add Skaffold} { dict set variant_to_component skaffold skaffold }
variant terraform_tools description {Add Terraform Tools} { dict set variant_to_component terraform_tools terraform-tools }

use_configure       no
build {
    # Define the basic installation command
    set install_command "CLOUDSDK_CONFIG=${worksrcpath}/.config CLOUDSDK_PYTHON=${python.bin} ./install.sh \
        --usage-reporting false \
        --command-completion false \
        --path-update false \
        --quiet \
        --install-python false"

    # Determine flags for installation of additional components
    set additional_components {}
    foreach component_variant [dict keys ${variant_to_component}] {
        if {[variant_isset ${component_variant}]} {
            lappend additional_components [dict get ${variant_to_component} ${component_variant}]
        }
    }
    if { [llength ${additional_components}] > 0 } {
        append install_command " --additional-components"
        foreach additional_component ${additional_components} {
            append install_command " " ${additional_component}
        }
    }

    # Run installer script
    system -W ${worksrcpath} ${install_command}

    # Default to the MacPorts Python binary
    reinplace "s|CLOUDSDK_PYTHON=\$(order_python .*|CLOUDSDK_PYTHON=${python.bin}|" \
        ${worksrcpath}/bin/bq \
        ${worksrcpath}/bin/docker-credential-gcloud \
        ${worksrcpath}/bin/git-credential-gcloud.sh \
        ${worksrcpath}/bin/gcloud \
        ${worksrcpath}/bin/gsutil \
        ${worksrcpath}/bin/java_dev_appserver.sh

    reinplace "s|^#!/usr/bin/env python$|#!${python.bin}|" \
        ${worksrcpath}/bin/dev_appserver.py

    # Disable updater
    reinplace "s|\"disable_updater\": false|\"disable_updater\": true|" ${worksrcpath}/lib/googlecloudsdk/core/config.json
}

test.run    yes
test.cmd    bin/gcloud
test.target
test.args   version

destroot {
    set libexecdir ${destroot}${prefix}/libexec/${name}
    xinstall -d ${libexecdir}
    copy \
        ${worksrcpath}/.install \
        ${worksrcpath}/bin \
        ${worksrcpath}/lib \
        ${worksrcpath}/platform \
        ${libexecdir}
    foreach f { anthoscli bq docker-credential-gcloud gcloud gsutil } {
        ln -s ../libexec/${name}/bin/${f} ${destroot}${prefix}/bin/${f}
    }
    ln -s ../libexec/${name}/bin/git-credential-gcloud.sh ${destroot}${prefix}/bin/git-credential-gcloud

    # bash completion
    set bash_completions_path ${prefix}/etc/bash_completion.d
    xinstall -d ${destroot}${bash_completions_path}
    xinstall -m 644 ${worksrcpath}/completion.bash.inc ${destroot}${bash_completions_path}/google-cloud-sdk

    # zsh completion
    set gcloud_libexec ${destroot}${prefix}/libexec/google-cloud-sdk
    xinstall -m 644 ${worksrcpath}/completion.zsh.inc ${gcloud_libexec}
}

notes "
    google-cloud-sdk zsh completions aren't loaded automatically.
    You must source them manually in your ~/.zshrc:
        source '${prefix}/libexec/google-cloud-sdk/completion.zsh.inc'"

livecheck.url       https://cloud.google.com/sdk/docs/release-notes
livecheck.regex     {([\d\.]+) \(\d{4}-\d{2}-\d{2}\)}
