#!/bin/bash
# do-framework configuration
# This file is sourced by all do scripts

# Project identification
export PROJECT_NAME="<%= projectName %>"
export DEPLOYMENT_CONFIG="<%= deploymentConfig %>"

# Derived from deployment config
export FRAMEWORK="<%= framework %>"
export MODEL_SERVER="<%= modelServer %>"

<% if (typeof enableLora !== 'undefined' && enableLora) { %>
# LoRA adapter serving
export ENABLE_LORA=true
export ADAPTER_S3_BUCKET="mlcc-adapters-$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo 'UNKNOWN')-${AWS_REGION}"
<% } %>

# AWS configuration
export AWS_REGION="<%= awsRegion %>"
export ECR_REPOSITORY_NAME="ml-container-creator"

# Build configuration — WHERE the Docker image gets built
export BUILD_TARGET="<%= buildTarget %>"
<% if (buildTarget === 'codebuild') { %>
export CODEBUILD_COMPUTE_TYPE="<%= codebuildComputeType %>"
export CODEBUILD_PROJECT_NAME="${PROJECT_NAME}-build-$(date +%Y%m%d)"
<% } %>

# Deployment configuration — WHERE the model runs
export DEPLOYMENT_TARGET="<%= deploymentTarget %>"

<% if (deploymentTarget === 'realtime-inference') { %>
# SageMaker Real-Time Inference configuration
<% if (typeof existingEndpointName !== 'undefined' && existingEndpointName) { %>
# External endpoint — attaching IC to an existing running endpoint
export ENDPOINT_NAME="<%= existingEndpointName %>"
export ENDPOINT_EXTERNAL=true
<% } else { %>
export INSTANCE_TYPE="<%= instanceType %>"
<% if (typeof instancePools !== 'undefined' && instancePools && instancePools.length > 1) { %>
# Instance pools: heterogeneous instance types with priority-based fallback
# Priority = selection order (1 = preferred, higher = fallback)
export INSTANCE_POOLS='<%= JSON.stringify(instancePools) %>'
<% } %>
<% if (inferenceAmiVersion) { %>
export INFERENCE_AMI_VERSION="<%= inferenceAmiVersion %>"
<% } %>
<% if (typeof capacityReservationArn !== 'undefined' && capacityReservationArn) { %>
# Note: Capacity reservations and instance pools (INSTANCE_POOLS) are mutually exclusive.
# If both are set, the capacity reservation takes precedence and INSTANCE_POOLS is ignored.
export CAPACITY_RESERVATION_ARN="<%= capacityReservationArn %>"
<% } %>
<% } %>
<% } %>

<% if (deploymentTarget === 'async-inference') { %>
# SageMaker Async Inference configuration
export INSTANCE_TYPE="<%= instanceType %>"
<% if (inferenceAmiVersion) { %>
export INFERENCE_AMI_VERSION="<%= inferenceAmiVersion %>"
<% } %>

# Async-specific configuration
# Resolve AWS account ID at runtime for default resource names
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo "UNKNOWN")

<% if (asyncS3OutputPath) { %>
export ASYNC_S3_OUTPUT_PATH="<%= asyncS3OutputPath %>"
<% } else { %>
export ASYNC_S3_OUTPUT_PATH="s3://mlcc-async-${ACCOUNT_ID}-${AWS_REGION}/${PROJECT_NAME}/output/"
<% } %>

<% if (asyncSnsSuccessTopic) { %>
export ASYNC_SNS_SUCCESS_TOPIC="<%= asyncSnsSuccessTopic %>"
<% } else { %>
export ASYNC_SNS_SUCCESS_TOPIC="arn:aws:sns:${AWS_REGION}:${ACCOUNT_ID}:ml-container-creator-${PROJECT_NAME}-async-success"
<% } %>

<% if (asyncSnsErrorTopic) { %>
export ASYNC_SNS_ERROR_TOPIC="<%= asyncSnsErrorTopic %>"
<% } else { %>
export ASYNC_SNS_ERROR_TOPIC="arn:aws:sns:${AWS_REGION}:${ACCOUNT_ID}:ml-container-creator-${PROJECT_NAME}-async-error"
<% } %>

<% if (asyncMaxConcurrentInvocations) { %>
export ASYNC_MAX_CONCURRENT_INVOCATIONS="<%= asyncMaxConcurrentInvocations %>"
<% } %>
<% } %>

<% if (deploymentTarget === 'hyperpod-eks') { %>
# HyperPod EKS configuration
export HYPERPOD_CLUSTER_NAME="<%= hyperPodCluster %>"
export HYPERPOD_NAMESPACE="<%= hyperPodNamespace %>"
export HYPERPOD_REPLICAS="<%= hyperPodReplicas %>"
<% if (fsxVolumeHandle) { %>
export FSX_VOLUME_HANDLE="<%= fsxVolumeHandle %>"
<% } %>
<% } %>

<% if (deploymentTarget === 'batch-transform') { %>
# SageMaker Batch Transform configuration
export INSTANCE_TYPE="<%= instanceType %>"

# Resolve AWS account ID at runtime for default resource names
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo "UNKNOWN")

<% if (batchInputPath) { %>
export BATCH_INPUT_PATH="<%= batchInputPath %>"
<% } else { %>
export BATCH_INPUT_PATH="s3://mlcc-batch-${ACCOUNT_ID}-${AWS_REGION}/${PROJECT_NAME}/input/"
<% } %>
<% if (batchOutputPath) { %>
export BATCH_OUTPUT_PATH="<%= batchOutputPath %>"
<% } else { %>
export BATCH_OUTPUT_PATH="s3://mlcc-batch-${ACCOUNT_ID}-${AWS_REGION}/${PROJECT_NAME}/output/"
<% } %>
export BATCH_INSTANCE_COUNT="<%= batchInstanceCount %>"
export BATCH_SPLIT_TYPE="<%= batchSplitType %>"
export BATCH_STRATEGY="<%= batchStrategy %>"
export BATCH_JOIN_SOURCE="<%= batchJoinSource || 'None' %>"
<% if (batchMaxConcurrentTransforms) { %>
export BATCH_MAX_CONCURRENT_TRANSFORMS="<%= batchMaxConcurrentTransforms %>"
<% } %>
<% if (batchMaxPayloadInMB) { %>
export BATCH_MAX_PAYLOAD_IN_MB="<%= batchMaxPayloadInMB %>"
<% } %>
<% } %>

<% if (typeof endpointInitialInstanceCount !== 'undefined' && endpointInitialInstanceCount != null) { %>
export ENDPOINT_INITIAL_INSTANCE_COUNT="<%= endpointInitialInstanceCount %>"
<% } %>
<% if (typeof endpointDataCapturePercent !== 'undefined' && endpointDataCapturePercent != null) { %>
export ENDPOINT_DATA_CAPTURE_PERCENT="<%= endpointDataCapturePercent %>"
<% } %>
<% if (typeof endpointVariantName !== 'undefined' && endpointVariantName != null) { %>
export ENDPOINT_VARIANT_NAME="<%= endpointVariantName %>"
<% } %>
<% if (typeof endpointVolumeSize !== 'undefined' && endpointVolumeSize != null) { %>
export ENDPOINT_VOLUME_SIZE="<%= endpointVolumeSize %>"
<% } %>

<% if (typeof icCpuCount !== 'undefined' && icCpuCount != null) { %>
export IC_CPU_COUNT="<%= icCpuCount %>"
<% } %>
<% if (typeof icMemorySize !== 'undefined' && icMemorySize != null) { %>
export IC_MEMORY_SIZE="<%= icMemorySize %>"
<% } %>
<% if (typeof icGpuCount !== 'undefined' && icGpuCount != null) { %>
export IC_GPU_COUNT="<%= icGpuCount %>"
<% } else { %>
export IC_GPU_COUNT="${IC_GPU_COUNT:-1}"
<% } %>
<% if (typeof icCopyCount !== 'undefined' && icCopyCount != null) { %>
export IC_COPY_COUNT="<%= icCopyCount %>"
<% } %>
<% if (typeof icModelWeight !== 'undefined' && icModelWeight != null) { %>
export IC_MODEL_WEIGHT="<%= icModelWeight %>"
<% } %>

<% if (typeof modelEnvVars !== 'undefined' && modelEnvVars && Object.keys(modelEnvVars).length > 0) { %>
# Model environment variables
<% Object.entries(modelEnvVars).forEach(([key, value]) => { %>
export <%= key %>=${<%= key %>:-<%= value %>}
<% }); %>
<% } %>

<% if (typeof serverEnvVars !== 'undefined' && serverEnvVars && Object.keys(serverEnvVars).length > 0) { %>
# Server environment variables
<% Object.entries(serverEnvVars).forEach(([key, value]) => { %>
export <%= key %>=${<%= key %>:-<%= value %>}
<% }); %>
<% } %>

# Framework-specific configuration
<% if (framework === 'transformers') { %>
export MODEL_NAME="<%= modelName %>"
# Secrets Manager integration: when an ARN is configured, do-scripts resolve the
# secret at the appropriate stage (build-time or runtime). When a plaintext value
# is configured, it is exported directly. The _ARN suffix signals resolution is needed.
<% if (typeof hfTokenArn !== 'undefined' && hfTokenArn) { %>
export HF_TOKEN_ARN="<%= hfTokenArn %>"
<% } else if (hfToken) { %>
export HF_TOKEN="<%= hfToken %>"
<% } %>
<% if (typeof ngcTokenArn !== 'undefined' && ngcTokenArn) { %>
export NGC_API_KEY_ARN="<%= ngcTokenArn %>"
<% } else if (ngcApiKey) { %>
export NGC_API_KEY="<%= ngcApiKey %>"
<% } %>

<% if (deploymentTarget !== 'batch-transform') { %>
# Managed Model Customization (do/tune)
export TUNE_SUPPORTED=<%= (typeof tuneSupported !== 'undefined' && tuneSupported) ? 'true' : 'false' %>
export TUNE_S3_BUCKET="mlcc-tune-$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo 'UNKNOWN')-${AWS_REGION}"
<% } %>
<% } %>

<% if (framework === 'diffusors') { %>
export MODEL_NAME="<%= modelName %>"
# Secrets Manager integration: when an ARN is configured, do-scripts resolve the
# secret at the appropriate stage (build-time or runtime). When a plaintext value
# is configured, it is exported directly. The _ARN suffix signals resolution is needed.
<% if (typeof hfTokenArn !== 'undefined' && hfTokenArn) { %>
export HF_TOKEN_ARN="<%= hfTokenArn %>"
<% } else if (hfToken) { %>
export HF_TOKEN="<%= hfToken %>"
<% } %>
<% } %>

<% if (modelFormat) { %>
export MODEL_FORMAT="<%= modelFormat %>"
<% } %>

<% if (roleArn) { %>
export ROLE_ARN="<%= roleArn %>"
<% } %>

<% if (typeof includeBenchmark !== 'undefined' && includeBenchmark) { %>
# SageMaker AI Benchmarking configuration
export BENCHMARK_CONCURRENCY="<%= benchmarkConcurrency %>"
export BENCHMARK_INPUT_TOKENS_MEAN="<%= benchmarkInputTokensMean %>"
export BENCHMARK_OUTPUT_TOKENS_MEAN="<%= benchmarkOutputTokensMean %>"
export BENCHMARK_STREAMING="<%= benchmarkStreaming %>"
<% if (benchmarkRequestCount) { %>
export BENCHMARK_REQUEST_COUNT="<%= benchmarkRequestCount %>"
<% } else { %>
export BENCHMARK_REQUEST_COUNT=""
<% } %>
<% if (benchmarkS3OutputPath) { %>
export BENCHMARK_S3_OUTPUT_PATH="<%= benchmarkS3OutputPath %>"
<% } else { %>
export BENCHMARK_S3_OUTPUT_PATH="s3://mlcc-benchmark-$(aws sts get-caller-identity --query Account --output text)-${AWS_REGION}/${PROJECT_NAME}/"
<% } %>
export BENCHMARK_JOB_NAME=""
export BENCHMARK_WORKLOAD_CONFIG_NAME=""
<% } %>

<% if (orderedEnvVars && orderedEnvVars.length > 0) { %>
# Runtime environment variables (from catalog)
<% orderedEnvVars.forEach(({ key, value }) => { %>
export <%= key %>=${<%= key %>:-<%= value %>}
<% }); %>
<% } %>

export BASE_IMAGE=${BASE_IMAGE:-<%= baseImage || '' %>}

# Allow environment variable overrides
export AWS_REGION=${AWS_REGION:-<%= awsRegion %>}
<% if ((deploymentTarget === 'realtime-inference' && !(typeof existingEndpointName !== 'undefined' && existingEndpointName)) || deploymentTarget === 'async-inference' || deploymentTarget === 'batch-transform') { %>
export INSTANCE_TYPE=${INSTANCE_TYPE:-<%= instanceType %>}
<% } %>
export ECR_REPOSITORY_NAME=${ECR_REPOSITORY_NAME:-ml-container-creator}

# Print configuration summary
echo "⚙️  Configuration loaded"
echo "   Project: ${PROJECT_NAME}"
echo "   Config:  ${DEPLOYMENT_CONFIG}"
echo "   Region:  ${AWS_REGION}"
echo "   Build target: ${BUILD_TARGET}"
echo "   Deployment target: ${DEPLOYMENT_TARGET}"
<% if (orderedEnvVars && orderedEnvVars.length > 0) { %>
echo "   Runtime env vars: <%= orderedEnvVars.length %>"
<% } %>
<% if ((typeof endpointInitialInstanceCount !== 'undefined' && endpointInitialInstanceCount != null) || (typeof endpointDataCapturePercent !== 'undefined' && endpointDataCapturePercent != null) || (typeof endpointVariantName !== 'undefined' && endpointVariantName != null) || (typeof endpointVolumeSize !== 'undefined' && endpointVolumeSize != null)) { %>
echo "   Endpoint config:"
<% if (typeof endpointInitialInstanceCount !== 'undefined' && endpointInitialInstanceCount != null) { %>
echo "     Initial instance count: ${ENDPOINT_INITIAL_INSTANCE_COUNT}"
<% } %>
<% if (typeof endpointDataCapturePercent !== 'undefined' && endpointDataCapturePercent != null) { %>
echo "     Data capture percent: ${ENDPOINT_DATA_CAPTURE_PERCENT}"
<% } %>
<% if (typeof endpointVariantName !== 'undefined' && endpointVariantName != null) { %>
echo "     Variant name: ${ENDPOINT_VARIANT_NAME}"
<% } %>
<% if (typeof endpointVolumeSize !== 'undefined' && endpointVolumeSize != null) { %>
echo "     Volume size: ${ENDPOINT_VOLUME_SIZE} GB"
<% } %>
<% } %>
<% if ((typeof icCpuCount !== 'undefined' && icCpuCount != null) || (typeof icMemorySize !== 'undefined' && icMemorySize != null) || (typeof icGpuCount !== 'undefined' && icGpuCount != null) || (typeof icCopyCount !== 'undefined' && icCopyCount != null) || (typeof icModelWeight !== 'undefined' && icModelWeight != null)) { %>
echo "   IC config:"
<% if (typeof icCpuCount !== 'undefined' && icCpuCount != null) { %>
echo "     CPU count: ${IC_CPU_COUNT}"
<% } %>
<% if (typeof icMemorySize !== 'undefined' && icMemorySize != null) { %>
echo "     Memory size: ${IC_MEMORY_SIZE} MB"
<% } %>
<% if (typeof icGpuCount !== 'undefined' && icGpuCount != null) { %>
echo "     GPU count: ${IC_GPU_COUNT}"
<% } %>
<% if (typeof icCopyCount !== 'undefined' && icCopyCount != null) { %>
echo "     Copy count: ${IC_COPY_COUNT}"
<% } %>
<% if (typeof icModelWeight !== 'undefined' && icModelWeight != null) { %>
echo "     Model weight: ${IC_MODEL_WEIGHT}"
<% } %>
<% } %>
<% if (typeof modelEnvVars !== 'undefined' && modelEnvVars && Object.keys(modelEnvVars).length > 0) { %>
echo "   Model env vars: <%= Object.keys(modelEnvVars).length %>"
<% } %>
<% if (typeof serverEnvVars !== 'undefined' && serverEnvVars && Object.keys(serverEnvVars).length > 0) { %>
echo "   Server env vars: <%= Object.keys(serverEnvVars).length %>"
<% } %>
<% if (deploymentTarget === 'realtime-inference') { %>
<% if (typeof existingEndpointName !== 'undefined' && existingEndpointName) { %>
echo "   Endpoint: ${ENDPOINT_NAME} (external)"
<% } else { %>
echo "   Instance: ${INSTANCE_TYPE}"
<% } %>
<% } else if (deploymentTarget === 'async-inference') { %>
echo "   Instance: ${INSTANCE_TYPE}"
echo "   S3 output: ${ASYNC_S3_OUTPUT_PATH}"
echo "   SNS success: ${ASYNC_SNS_SUCCESS_TOPIC}"
echo "   SNS error: ${ASYNC_SNS_ERROR_TOPIC}"
<% } else if (deploymentTarget === 'batch-transform') { %>
echo "   Instance: ${INSTANCE_TYPE} x ${BATCH_INSTANCE_COUNT}"
echo "   S3 input: ${BATCH_INPUT_PATH}"
echo "   S3 output: ${BATCH_OUTPUT_PATH}"
echo "   Split type: ${BATCH_SPLIT_TYPE}"
echo "   Strategy: ${BATCH_STRATEGY}"
<% } else if (deploymentTarget === 'hyperpod-eks') { %>
echo "   HyperPod cluster: ${HYPERPOD_CLUSTER_NAME}"
echo "   Namespace: ${HYPERPOD_NAMESPACE}"
<% } %>
