#!/bin/bash

# preset_mongodb_k8s.sh - MongoDB preset data deployment script for Kubernetes
# Usage: ./preset_mongodb_k8s.sh [version] [namespace] [local_file_path(optional)]

set -e

# Show usage information
show_usage() {
    echo "Usage: $0 [version] [namespace] [local_file_path(optional)]"
    echo "Examples: "
    echo "  $0 6.5.0 default                                       # Download and use specified version"
    echo "  $0 6.5.0 default /path/to/preset_mongodb_6.5.0.tar.gz  # Use local file"
    exit 1
}

# Check parameters
if [ $# -lt 2 ]; then
    echo "Error: At least two parameters (version and namespace) are required"
    show_usage
fi

VERSION="$1"
NAMESPACE="$2"
LOCAL_FILE="$3"
FILENAME="preset_mongodb_${VERSION}.tar.gz"

# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
    echo "Error: kubectl is not installed or not in PATH"
    exit 1
fi

# Check if namespace exists
if ! kubectl get namespace "$NAMESPACE" &> /dev/null; then
    echo "Error: Namespace '$NAMESPACE' does not exist"
    exit 1
fi

# Get config pod name
POD_NAME=$(kubectl get pod -n "$NAMESPACE" | grep config | awk '{print $1}')
if [ -z "$POD_NAME" ]; then
    echo "Error: No config pod found in namespace '$NAMESPACE'"
    exit 1
fi

echo "Found config pod: $POD_NAME in namespace: $NAMESPACE"

# Handle file source
if [ -n "$LOCAL_FILE" ]; then
    # Use local file
    if [ ! -f "$LOCAL_FILE" ]; then
        echo "Error: Local file does not exist: $LOCAL_FILE"
        exit 1
    fi
    echo "Using local file: $LOCAL_FILE"
else
    # Download file
    URL="https://pdpublic.mingdao.com/private-deployment/data/preset_mongodb_${VERSION}.tar.gz"
    echo "Downloading file: $URL"
    if ! wget -q "$URL"; then
        echo "Error: Failed to download file"
        exit 1
    fi
fi

# Check if file exists
if [ ! -f "./$FILENAME" ]; then
    echo "Error: File does not exist: ./$FILENAME"
    exit 1
fi

echo "Starting MongoDB preset data deployment version: $VERSION"

# Copy file to pod
echo "Copying file to pod..."
if ! kubectl cp "./$FILENAME" "$NAMESPACE/$POD_NAME":/tmp/; then
    echo "Error: Failed to copy file to pod"
    exit 1
fi

# Extract file inside pod
echo "Extracting file inside pod..."
if ! kubectl exec -n "$NAMESPACE" "$POD_NAME" -- tar -zxf "/tmp/$FILENAME" -C /init/mongodb/; then
    echo "Error: Failed to extract file in pod"
    exit 1
fi

# Execute region reset operation
echo "Executing region reset operation..."
if ! kubectl exec -n "$NAMESPACE" "$POD_NAME" -- bash -c "source /entrypoint.sh && mongodbResetRegion"; then
    echo "Error: Failed to execute region reset operation"
    exit 1
fi

# Execute translation template reset operation
echo "Executing translation template reset operation..."
if ! kubectl exec -n "$NAMESPACE" "$POD_NAME" -- bash -c "source /entrypoint.sh && mongodbResetTemplateMessage"; then
    echo "Error: Failed to execute translation template reset operation"
    exit 1
fi

# Clean up temporary files in pod
echo "Cleaning up temporary files in pod..."
kubectl exec -n "$NAMESPACE" "$POD_NAME" -- rm -f "/tmp/$FILENAME"

# Clean up local temporary files
echo "Cleaning up local temporary files..."
rm -f "./$FILENAME"

echo "MongoDB preset data update completed!"

