#!/bin/bash

echo "=== Cài đặt GCP Tools ==="

# Google Cloud CLI
echo "1. Cài đặt Google Cloud CLI..."
./install-gcloud-cli.sh
if [ $? -ne 0 ]; then
    echo "Lỗi cài đặt Google Cloud CLI"
    exit 1
fi

# GKE gcloud auth plugin
echo "2. Cài đặt GKE auth plugin..."
if command -v gcloud &> /dev/null; then
    sudo apt install -y google-cloud-cli-gke-gcloud-auth-plugin
    echo "✓ GKE auth plugin đã được cài đặt"
fi

# Cloud SQL Proxy
echo "3. Cài đặt Cloud SQL Proxy..."
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
chmod +x cloud_sql_proxy
sudo mv cloud_sql_proxy /usr/local/bin/
echo "✓ Cloud SQL Proxy đã được cài đặt"

# Terraform (for GCP)
echo "4. Cài đặt Terraform..."
if ! command -v terraform &> /dev/null; then
    wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
    sudo apt update && sudo apt install -y terraform
    echo "✓ Terraform đã được cài đặt"
fi

echo ""
echo "=== GCP Tools đã sẵn sàng ==="
echo "Google Cloud CLI: $(gcloud --version 2>&1 | head -n1)"
echo "Cloud SQL Proxy: $(cloud_sql_proxy --version 2>/dev/null || echo 'Installed')"
echo "Terraform: $(terraform --version 2>&1 | head -n1)"