ARG KALI_BASE=kalilinux/kali-rolling
FROM ${KALI_BASE}

LABEL org.opencontainers.image.title="decompile-engine" \
      org.opencontainers.image.version="1.10.0" \
      org.opencontainers.image.source="https://github.com/Admin12121/decompile" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.authors="admin12121" \
      io.decompile.engine.version="1.10.0" \
      io.decompile.engine.protocol="2" \
      io.decompile.engine.capabilities="native,android,jvm,dotnet,apple,wasm,native-semantic-v1,native-semantic-v2,native-semantic-v3,native-semantic-v4,native-semantic-v5,native-semantic-v6,native-semantic-v7,native-semantic-v8,native-semantic-v9,native-semantic-v10,graph-layout-elk-v1,graph-layout-elk-v2" \
      io.decompile.engine.worker="directory-v2"

ENV DEBIAN_FRONTEND=noninteractive
ENV DOTNET_ROOT=/opt/dotnet
ENV PATH="/opt/dotnet:/usr/local/bin:${PATH}"
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
ENV DOTNET_NOLOGO=1

ARG DOTNET_CHANNEL=10.0
ARG ILSPYCMD_VERSION=10.0.1.8346
ARG ELKJS_VERSION=0.12.0

RUN printf 'Acquire::Retries "5";\nAcquire::http::Timeout "30";\nAcquire::https::Timeout "30";\n' \
    > /etc/apt/apt.conf.d/80-retries

RUN printf 'deb http://kali.download/kali kali-rolling main contrib non-free non-free-firmware\n' \
    > /etc/apt/sources.list

RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get update --fix-missing && \
    apt-get install -y --fix-missing --no-install-recommends \
      ghidra \
      openjdk-21-jre-headless \
      python3 \
      jadx \
      apktool \
      file \
      binutils \
      coreutils \
      curl \
      unzip \
      zip \
      upx-ucl \
      rizin \
      python3-lief \
      detect-it-easy \
      graphviz \
      nodejs \
      npm \
      wabt \
      ca-certificates && \
    rm -rf /var/lib/apt/lists/*

RUN npm install -g "elkjs@${ELKJS_VERSION}" --omit=dev --no-audit --no-fund
ENV NODE_PATH=/usr/local/lib/node_modules

RUN apt-get update --fix-missing && \
    apt-get install -y --fix-missing --no-install-recommends libicu-dev && \
    rm -rf /var/lib/apt/lists/*

RUN curl -fsSL https://dot.net/v1/dotnet-install.sh -o /tmp/dotnet-install.sh && \
    chmod +x /tmp/dotnet-install.sh && \
    /tmp/dotnet-install.sh --channel "$DOTNET_CHANNEL" --install-dir "$DOTNET_ROOT" && \
    rm -f /tmp/dotnet-install.sh && \
    DOTNET_CLI_HOME=/tmp/dotnet-home dotnet tool install --tool-path /usr/local/bin ilspycmd --version "$ILSPYCMD_VERSION" && \
    rm -rf /tmp/dotnet-home

ENV DECOMPILE_IN_DOCKER=1

RUN mkdir -p /input /out /reverse /tmp/decompile-home/.config && \
    chmod 0777 /input /out /reverse /tmp/decompile-home /tmp/decompile-home/.config

RUN mkdir -p /usr/local/bin/decompile_tool/backends

# Engine layer only: host-side CLI presentation is intentionally kept separate.
# Frontend-only releases therefore do not require rebuilding the heavy engine.
COPY decompile_tool/__init__.py /usr/local/bin/decompile_tool/__init__.py
COPY decompile_tool/cli.py /usr/local/bin/decompile_tool/cli.py
COPY decompile_tool/core.py /usr/local/bin/decompile_tool/core.py
COPY decompile_tool/scaling.py /usr/local/bin/decompile_tool/scaling.py
COPY decompile_tool/textsafe.py /usr/local/bin/decompile_tool/textsafe.py
COPY decompile_tool/engine.py /usr/local/bin/decompile_tool/engine.py
COPY decompile_tool/analysis.py /usr/local/bin/decompile_tool/analysis.py
COPY decompile_tool/intelligence.py /usr/local/bin/decompile_tool/intelligence.py
COPY decompile_tool/semantics.py /usr/local/bin/decompile_tool/semantics.py
COPY decompile_tool/managed.py /usr/local/bin/decompile_tool/managed.py
COPY decompile_tool/mobile.py /usr/local/bin/decompile_tool/mobile.py
COPY decompile_tool/progress.py /usr/local/bin/decompile_tool/progress.py
COPY decompile_tool/artifacts.py /usr/local/bin/decompile_tool/artifacts.py
COPY decompile_tool/detection.py /usr/local/bin/decompile_tool/detection.py
COPY decompile_tool/graphs.py /usr/local/bin/decompile_tool/graphs.py
COPY decompile_tool/elk_layout.py /usr/local/bin/decompile_tool/elk_layout.py
COPY decompile_tool/elk_layout.cjs /usr/local/bin/decompile_tool/elk_layout.cjs
COPY decompile_tool/semantic_objects.py /usr/local/bin/decompile_tool/semantic_objects.py
COPY decompile_tool/semantic_runtime.py /usr/local/bin/decompile_tool/semantic_runtime.py
COPY decompile_tool/semantic_synthesis.py /usr/local/bin/decompile_tool/semantic_synthesis.py
COPY decompile_tool/semantic_graphs.py /usr/local/bin/decompile_tool/semantic_graphs.py
COPY decompile_tool/semantic_types.py /usr/local/bin/decompile_tool/semantic_types.py
COPY decompile_tool/semantic_state.py /usr/local/bin/decompile_tool/semantic_state.py
COPY decompile_tool/semantic_behavior.py /usr/local/bin/decompile_tool/semantic_behavior.py
COPY decompile_tool/semantic_deobfuscation.py /usr/local/bin/decompile_tool/semantic_deobfuscation.py
COPY decompile_tool/semantic_ir.py /usr/local/bin/decompile_tool/semantic_ir.py
COPY decompile_tool/semantic_indirect.py /usr/local/bin/decompile_tool/semantic_indirect.py
COPY decompile_tool/graph_explorer.py /usr/local/bin/decompile_tool/graph_explorer.py
COPY decompile_tool/metadata.py /usr/local/bin/decompile_tool/metadata.py
COPY decompile_tool/output.py /usr/local/bin/decompile_tool/output.py
COPY decompile_tool/planner.py /usr/local/bin/decompile_tool/planner.py
COPY decompile_tool/process.py /usr/local/bin/decompile_tool/process.py
COPY decompile_tool/ui.py /usr/local/bin/decompile_tool/ui.py
COPY decompile_tool/platform_open.py /usr/local/bin/decompile_tool/platform_open.py
COPY decompile_tool/DumpAllDecompile.java /usr/local/bin/decompile_tool/DumpAllDecompile.java
COPY decompile_tool/ConfigureAnalysis.java /usr/local/bin/decompile_tool/ConfigureAnalysis.java
COPY decompile_tool/backends/__init__.py /usr/local/bin/decompile_tool/backends/__init__.py
COPY decompile_tool/backends/die.py /usr/local/bin/decompile_tool/backends/die.py
COPY decompile_tool/backends/lief_backend.py /usr/local/bin/decompile_tool/backends/lief_backend.py
COPY decompile_tool/backends/rizin.py /usr/local/bin/decompile_tool/backends/rizin.py
COPY decompile /usr/local/bin/decompile

RUN chmod +x /usr/local/bin/decompile && \
    chmod +x /usr/local/bin/decompile_tool/cli.py

# Record the exact package/tool versions inside the built engine.  Builders can
# additionally pin KALI_BASE to an image digest for byte-for-byte base-image
# reproducibility without hard-coding a stale rolling digest in this source.
RUN mkdir -p /usr/local/share/decompile && \
    { echo "engine=1.10.0"; echo "elkjs=${ELKJS_VERSION}"; echo "ilspycmd=${ILSPYCMD_VERSION}"; \
      dpkg-query -W -f='${Package}=${Version}\n' | sort; } > /usr/local/share/decompile/toolchain.lock

WORKDIR /reverse

ENTRYPOINT ["decompile"]
