#!/bin/bash
################################################################################
# Render Helm Templates for AWS
# Prerequisites: kubectl configured for EKS cluster
################################################################################

set -e

# Get script directory and base directory (absolute paths for gomplate compatibility)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BASE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"

# Detect platform and set template tool
detect_template_tool() {
    case "$(uname -s)" in
        Linux*)
            TEMPLATE_CMD="gotemplate"
            ;;
        Darwin*)
            TEMPLATE_CMD="gomplate"
            ;;
        *)
            echo "Unsupported OS: $(uname -s)"
            exit 1
            ;;
    esac
}

detect_template_tool

# Check if template tool is available
if ! command -v $TEMPLATE_CMD &> /dev/null; then
    echo "Error: $TEMPLATE_CMD not found"
    if [ "$TEMPLATE_CMD" = "gomplate" ]; then
        echo "Install with: brew install gomplate"
    else
        echo "Install gotemplate for your platform"
    fi
    exit 1
fi

# Check required config files
if [ ! -f "$BASE_DIR/config/iaas.json" ]; then
    echo "Error: $BASE_DIR/config/iaas.json not found"
    exit 1
fi

if [ ! -f "$BASE_DIR/config/appsecrets.json" ]; then
    echo "Error: $BASE_DIR/config/appsecrets.json not found"
    exit 1
fi

# Check required environment variables
if [ -z "$EKS_CLUSTER_NAME" ]; then
    echo "EKS_CLUSTER_NAME not found, try 'source ../config/config.env' to import this variable."
    exit 1
fi

if [ -z "$AWS_REGION" ]; then
    echo "AWS_REGION not found, try 'source ../config/config.env' to import this variable."
    exit 1
fi

# Get EKS credentials
echo "Getting EKS credentials for cluster: $EKS_CLUSTER_NAME in region: $AWS_REGION"
if [ -n "$AWS_PROFILE" ]; then
    echo "Using AWS profile: $AWS_PROFILE"
    aws eks update-kubeconfig --name "${EKS_CLUSTER_NAME}" --region "${AWS_REGION}" --profile "${AWS_PROFILE}"
else
    aws eks update-kubeconfig --name "${EKS_CLUSTER_NAME}" --region "${AWS_REGION}"
fi

# Verify kubectl connection
if ! kubectl cluster-info &> /dev/null; then
    echo "Error: kubectl not connected to cluster"
    exit 1
fi

echo "Using template tool: $TEMPLATE_CMD"
echo "Rendering templates..."

# Create tmp directory and copy files
mkdir -p "$SCRIPT_DIR/tmp"
cp -r "$BASE_DIR/app/"* "$SCRIPT_DIR/tmp/."

# Render templates (use absolute paths for gomplate 4.x compatibility)
$TEMPLATE_CMD -d iaas="$BASE_DIR/config/iaas.json" -d appsecrets="$BASE_DIR/config/appsecrets.json" -d version="$BASE_DIR/config/version.json" --exclude=templates --exclude=charts --exclude=*.tgz --input-dir="$BASE_DIR/app" --output-dir="$SCRIPT_DIR/tmp"
$TEMPLATE_CMD -d iaas="$BASE_DIR/config/iaas.json" -d appsecrets="$BASE_DIR/config/appsecrets.json" -d version="$BASE_DIR/config/version.json" --exclude=templates --exclude=charts --exclude=*.tgz --input-dir="$BASE_DIR/tr-app" --output-dir="$SCRIPT_DIR/tmp"

echo "Templates rendered to $SCRIPT_DIR/tmp/"
