# ═══════════════════════════════════════════════════════════
# 🔧 OPHION Universal Instrumentation Container
# Downloads and provides OpenTelemetry agents for all languages
# ═══════════════════════════════════════════════════════════

FROM alpine:3.19

LABEL org.opencontainers.image.title="OPHION Instrumentation"
LABEL org.opencontainers.image.description="OpenTelemetry agents for .NET, Node.js, Python, Java, Go, PHP"

# Install tools
RUN apk add --no-cache curl unzip bash jq

# Create directory structure
RUN mkdir -p /otel/{dotnet,dotnet-musl,nodejs,python,java,go,php}

WORKDIR /otel

# ═══════════════════════════════════════════════════════════
# .NET Auto-Instrumentation (glibc)
# ═══════════════════════════════════════════════════════════
ENV OTEL_DOTNET_VERSION=1.6.0
RUN curl -L --retry 3 --retry-delay 5 --max-time 120 \
    "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v${OTEL_DOTNET_VERSION}/opentelemetry-dotnet-instrumentation-linux-glibc-x64.zip" \
    -o /tmp/dotnet.zip && \
    unzip -q /tmp/dotnet.zip -d /otel/dotnet && \
    rm /tmp/dotnet.zip && \
    chmod +x /otel/dotnet/*.sh 2>/dev/null || true

# .NET Auto-Instrumentation (musl/Alpine)
RUN curl -L --retry 3 --retry-delay 5 --max-time 120 \
    "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v${OTEL_DOTNET_VERSION}/opentelemetry-dotnet-instrumentation-linux-musl-x64.zip" \
    -o /tmp/dotnet-musl.zip && \
    unzip -q /tmp/dotnet-musl.zip -d /otel/dotnet-musl && \
    rm /tmp/dotnet-musl.zip && \
    chmod +x /otel/dotnet-musl/*.sh 2>/dev/null || true

# ═══════════════════════════════════════════════════════════
# Java Auto-Instrumentation
# Uses GitHub API to get proper redirect
# ═══════════════════════════════════════════════════════════
ENV OTEL_JAVA_VERSION=2.1.0
RUN JAVA_URL=$(curl -sL "https://api.github.com/repos/open-telemetry/opentelemetry-java-instrumentation/releases/tags/v${OTEL_JAVA_VERSION}" | \
    jq -r '.assets[] | select(.name=="opentelemetry-javaagent.jar") | .browser_download_url') && \
    if [ -n "$JAVA_URL" ] && [ "$JAVA_URL" != "null" ]; then \
        curl -L --retry 3 --max-time 180 "$JAVA_URL" -o /otel/java/opentelemetry-javaagent.jar; \
    else \
        echo "Downloading Java agent directly..." && \
        curl -L --retry 3 --max-time 180 \
            "https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v${OTEL_JAVA_VERSION}/opentelemetry-javaagent.jar" \
            -o /otel/java/opentelemetry-javaagent.jar; \
    fi && \
    ls -la /otel/java/

# ═══════════════════════════════════════════════════════════
# Copy pre-created files for other languages
# ═══════════════════════════════════════════════════════════
COPY files/ /otel/

# Set permissions
RUN chmod -R 755 /otel && \
    chmod +x /otel/python/bootstrap.sh 2>/dev/null || true

# Verify downloads
RUN echo "=== OPHION Instrumentation Contents ===" && \
    ls -la /otel/ && \
    echo "=== Java agent size ===" && \
    ls -lh /otel/java/opentelemetry-javaagent.jar 2>/dev/null || echo "Java agent not present" && \
    echo "=== .NET ===" && ls /otel/dotnet/ | head -5

# Volume for sharing with other containers
VOLUME ["/otel"]

CMD ["echo", "OpenTelemetry agents ready in /otel"]
