# -*- 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             544.0.0
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  f80b67dddf2d8444b7d1951a0e57b28e6cb04b3b \
                    sha256  92e60c85a5b491e7f87c396699756f1745e17bd1ce4dbf1efb0bb872e980a8c3 \
                    size    55699637
} elseif { ${configure.build_arch} eq "x86_64" } {
    set arch_classifier x86_64
    checksums       rmd160  46827d47b46dd322deed9807f1db5a0a26cf025d \
                    sha256  f3780c2de10a6c6bdf13d770e469450cf767c43b1d6d3013b8e9d1a2bac3437d \
                    size    57233420
} elseif { ${configure.build_arch} eq "arm64" } {
    set arch_classifier arm
    checksums       rmd160  43d4ae716fb5b9c3320f5955989e2650ee21d049 \
                    sha256  a345eda1b78715a12f17b79632fa58f52fd96267c2bbcec7ab26089228e1ac49 \
                    size    57165736
} 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://cloud.google.com/sdk/docs/install#mac) and
# gsutil (https://cloud.google.com/storage/docs/gsutil_install#before_you_begin)
python.default_version 313

post-patch {
    # 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
}

use_configure       no
build               {}

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

patch {
    # Make sure the installer script will not attempt to install its own Python,
    # even though `--install-python false` is passed to it.
    # This is a workaround for https://issuetracker.google.com/issues/422318565
    reinplace "s|python_manager.PromptAndInstallPythonOnMac|#python_manager.PromptAndInstallPythonOnMac|" \
        ${worksrcpath}/lib/googlecloudsdk/core/updater/update_manager.py

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

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}\)}
