#!/usr/bin/env bash
# Builds an AWS Lambda deployment package for Python 3.14 (x86_64 Linux).
# Requires Docker to be installed and running.
#
# Usage: ./build.sh
#
# Output: lambda_function.zip in the current directory

set -euo pipefail

PYTHON_VERSION="3.14"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_ZIP="$SCRIPT_DIR/lambda_function.zip"

echo "==> Building Lambda deployment package for Python ${PYTHON_VERSION}..."

# Clean previous build artifacts
rm -rf "$SCRIPT_DIR/package" "$OUTPUT_ZIP"

# Install dependencies into package/ using a Linux container so compiled
# extensions (.so files) are built for the Lambda execution environment.
docker run --rm \
  -v "$SCRIPT_DIR":/var/task \
  -w /var/task \
  public.ecr.aws/sam/build-python${PYTHON_VERSION}:latest \
  bash -c "pip install -r requirements.txt -t package/"

# Create the zip: dependencies first, then the function code on top
cd "$SCRIPT_DIR/package"
zip -r9 "$OUTPUT_ZIP" .
cd "$SCRIPT_DIR"
zip -g "$OUTPUT_ZIP" lambda_function.py

echo "==> Done! Deployment package: $OUTPUT_ZIP"
