# Define build argument for deployment mode (default is azure, can also be dev)
ARG DEPLOY_MODE="azure"

# Conditional copy of files based on build argument
FROM windowsarena/winarena-base:latest AS build_dev
ONBUILD COPY src/win-arena-container/vm/setup/. /shared/
ONBUILD COPY src/win-arena-container/vm/unattend-files/dev_win11x64-enterprise-eval.xml /run/assets/win11x64-enterprise-eval.xml
ONBUILD ENV FOLDER_NAME=shared

FROM windowsarena/winarena-base:latest AS build_azure
ONBUILD COPY src/win-arena-container/vm/setup/. /oem/
ONBUILD COPY src/win-arena-container/vm/unattend-files/azure_win11x64-enterprise-eval.xml /run/assets/win11x64-enterprise-eval.xml
# Fix for Azure ML Job
ONBUILD COPY src/win-arena-container/fix_az_network.sh /run/network.sh
ONBUILD ENV FOLDER_NAME=oem

FROM build_${DEPLOY_MODE}

ARG DEPLOY_MODE="azure"
ENV DEPLOY_MODE=${DEPLOY_MODE}

RUN echo "FOLDER_NAME: ${FOLDER_NAME}"
RUN echo "DEPLOY_MODE: ${DEPLOY_MODE}"

# If azure, replace windows data folder with oem folder
RUN if [ "${DEPLOY_MODE}" = "azure" ]; then \
        WINDOWS_DATA_FOLDER='\\\\host.lan\\Data'; \
        WINDOWS_OEM_FOLDER='C:\\oem'; \
        OEM_FOLDER='oem'; \
        sed -i "s|${WINDOWS_DATA_FOLDER}|${WINDOWS_OEM_FOLDER}|g" "/${OEM_FOLDER}/install.bat"; \
        sed -i "s|${WINDOWS_DATA_FOLDER}|${WINDOWS_OEM_FOLDER}|g" "/${OEM_FOLDER}/on-logon.ps1"; \
        sed -i "s|${WINDOWS_DATA_FOLDER}|${WINDOWS_OEM_FOLDER}|g" "/${OEM_FOLDER}/setup.ps1"; \
    fi

# Copy client application
COPY src/win-arena-container/client /client

COPY src/win-arena-container/entry_setup.sh /entry_setup.sh
COPY src/win-arena-container/start_client.sh /start_client.sh
COPY src/win-arena-container/start_vm.sh /start_vm.sh
COPY src/win-arena-container/entry.sh /entry.sh

RUN find / -maxdepth 3 -type f -name "*.sh" -exec dos2unix {} \; && chmod +x /*.sh

# Patch IP addresses for modern dockurr/windows (v5.07+)
# Old IP: 20.20.20.21 (dockurr/windows < v5.07)
# New IP: 172.30.0.2 (dockurr/windows >= v5.07)
RUN sed -i 's|20\.20\.20\.21|172.30.0.2|g' /entry_setup.sh /entry.sh /start_client.sh && \
    find /client -name "*.py" -exec sed -i 's|20\.20\.20\.21|172.30.0.2|g' {} \;

# Install fuse
RUN apt-get update && apt-get install -y fuse

ENV YRES="900"
ENV XRES="1440"
ENV RAM_SIZE="8G"
ENV CPU_CORES="8"
ENV VERSION="win11x64-enterprise-eval"
ENV DISK_SIZE="30G"

# Enable QEMU JSON-based QEMU Machine Protocol (QMP)
ENV ARGUMENTS="-qmp tcp:0.0.0.0:7200,server,nowait"

ENTRYPOINT ["/bin/bash", "-c"]
