# Build the image using the --build-arg option, e.g.:
# docker build -t boret/myimage:0.1 --build-arg=mxe_sha=123ABC456 .

# We need to stick with 22.04 for now because the latest MXE version
# (db430dc676e6f5d77604af150b8acc1403af4fd7) does not build a working
# version of QtWebKit, which breaks printing in Subsurface.
FROM ubuntu:22.04 as base

# update and set up the packages we need for the build
RUN apt-get update  &&  \
    apt-get full-upgrade -y && \
    apt-get install -y \
    autoconf \
    automake \
    autopoint \
    bash \
    binutils \
    bzip2 \
    ca-certificates \
    g++ \
    g++-multilib \
    gettext \
    git \
    intltool \
    libltdl-dev \
    libssl-dev \
    libtool \
    libtool-bin \
    make \
    openssl \
    p7zip-full \
    patch \
    perl \
    pkg-config \
    sed \
    unzip \
    wget \
    lzip && \
    apt-get clean


FROM base as build

# set up the packages we need additionally for this cross build
RUN apt-get install -y \
    bison \
    flex \
    gperf \
    libc6-dev-i386 \
    libgdk-pixbuf2.0-dev \
    libxml-parser-perl \
    python3 \
    python3-mako \
    python3-setuptools \
    python-is-python3 \
    ruby \
    xz-utils \
    scons

# Default to 'master' if no build argument is passed in
ARG mxe_sha=master
# Very often master is broken, so we pass in a known good SHA
ENV _ver=${mxe_sha}

WORKDIR /win

# checkout MXE at the right version
RUN git clone https://github.com/mxe/mxe && \
    cd mxe && \
    git checkout ${_ver}

WORKDIR /win/mxe

# Move the settings into place to build everything that we need
ADD settings.mk .

# Patch the qtconnectivity build to explicilty enable native-win32-bluetooth and ensure another
# backend is not picked
ADD qtconnectivity-1.patch src/

# separate download from build so that we can redo the build
RUN make -j$(nproc) download 2>&1 | tee mxe-build.log

RUN make -j$(nproc) 2>&1 | tee -a mxe-build.log

# for some reason smtk2ssrf needs a static build of mdbtools
RUN make -j$(nproc) MXE_TARGETS=x86_64-w64-mingw32.static glib mdbtools 2>&1 | tee -a mxe-build.log

RUN rm -rf pkg log docs


FROM base as final

WORKDIR /win

COPY --from=build /win/mxe mxe
