ARG DOCKER_PREFIX=wapmobil/
FROM ${DOCKER_PREFIX}debian
ARG JOBS_COUNT=4
ARG JOBS_QT_COUNT=2

WORKDIR /soft/picross

RUN apt-get update && apt-get install -y g++-arm-linux-gnueabihf && rm -rf /var/cache/apt/archives/*

COPY sysroot.tar.bz2 sysroot.tar.bz2
COPY toolchain-RPi.cmake /soft/

WORKDIR /soft/pi
RUN tar -xjf /soft/picross/sysroot.tar.bz2 && rm /soft/picross/sysroot.tar.bz2


WORKDIR /soft

ENV _TARGET=linux-gnueabihf
ENV CROSS_COMPILE=arm-${_TARGET}-


# sodium
WORKDIR /soft
RUN curl https://download.libsodium.org/libsodium/releases/libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 -o libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 && tar -xf libsodium-${CUR_SODIUM_VERSION}.tar.gz -C /soft \
 && rm -f libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 && mkdir -p /soft/pi/build/sodium && cd /soft/pi/build/sodium \
 && sh -c 'CFLAGS="-O3" LDFLAGS="-O3" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}gcc CXX=${CROSS_COMPILE}g++ /soft/libsodium-*/configure --host=arm-${_TARGET} --prefix=/soft/pi/usr && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/pi/build/sodium && rm -rf /soft/libsodium-*


# fftw3
WORKDIR /soft
RUN wget -nv http://www.fftw.org/fftw-${CUR_FFTW_VERSION}.tar.gz \
 && tar -xf fftw-${CUR_FFTW_VERSION}.tar.gz -C /soft \
 && rm -f fftw-${CUR_FFTW_VERSION}.tar.gz \
 && mkdir -p /soft/pi/build/fftw3 && cd /soft/pi/build/fftw3 \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/pi/usr -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-RPi.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/pi/usr -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-RPi.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/pi/usr -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-RPi.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*


# assimp
WORKDIR /soft
RUN wget -nv https://github.com/assimp/assimp/archive/v${CUR_ASSIMP_VERSION}.tar.gz \
 && tar -xf v${CUR_ASSIMP_VERSION}.tar.gz -C /soft \
 && rm -f v${CUR_ASSIMP_VERSION}.tar.gz pax_global_header \
 && mkdir -p /soft/pi/build/assimp && cd /soft/pi/build/assimp \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/pi/usr -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DASSIMP_BUILD_ASSIMP_TOOLS=0 -DASSIMP_BUILD_TESTS=0 -DBUILD_FRAMEWORK=0 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-RPi.cmake /soft/assimp-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/assimp-*


# markdown
WORKDIR /soft
RUN wget -nv https://github.com/Orc/discount/archive/v${CUR_DISCOUNT_VERSION}.tar.gz \
 && tar -xf v${CUR_DISCOUNT_VERSION}.tar.gz -C /soft \
 && rm -f v${CUR_DISCOUNT_VERSION}.tar.gz
RUN sed -i 's/DESTRUCTOR/ /' /soft/discount-${CUR_DISCOUNT_VERSION}/setup.c
WORKDIR /soft/discount-${CUR_DISCOUNT_VERSION}/cmake
COPY discount.cmake.patch /soft/discount-${CUR_DISCOUNT_VERSION}/cmake/cmake.patch
RUN patch < cmake.patch
WORKDIR /soft/linux/build/discount
RUN cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-${CUR_DISCOUNT_VERSION}/cmake && make -j2 && mv mktags .. && rm -rf ./* \
&& mkdir -p /soft/pi/build/discount && mv /soft/linux/build/mktags /soft/pi/build/discount/mktags
WORKDIR /soft/pi/build/discount
RUN cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/soft/pi/usr -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-RPi.cmake -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake && make install -j${JOBS_COUNT} && rm -rf ./*


# microhttpd
WORKDIR /soft/
RUN wget -nv https://mirror.tochlab.net/pub/gnu/libmicrohttpd/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && tar -xf /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz -C /soft/ \
 && rm /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && mkdir -p /soft/pi/build/microhttpd && cd /soft/pi/build/microhttpd \
 && sh -c 'CFLAGS="-O3" LDFLAGS="-O3" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}gcc CXX=${CROSS_COMPILE}g++ /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}/configure --host=arm-${_TARGET} --prefix=/soft/pi/usr --enable-shared=no --enable-static=yes --enable-tests=no --enable-examples=no && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/pi/build/microhttpd && rm -rf /soft/libmicrohttpd-*


# Qt
WORKDIR /soft
ENV SYSROOT=/soft/pi
RUN ln -s /usr/bin/arm-linux-gnueabihf-g++     /usr/bin/arm-linux-gnueabi-g++
RUN ln -s /usr/bin/arm-linux-gnueabihf-gcc     /usr/bin/arm-linux-gnueabi-gcc
RUN ln -s /usr/bin/arm-linux-gnueabihf-ar      /usr/bin/arm-linux-gnueabi-ar
RUN ln -s /usr/bin/arm-linux-gnueabihf-nm      /usr/bin/arm-linux-gnueabi-nm
RUN ln -s /usr/bin/arm-linux-gnueabihf-strip   /usr/bin/arm-linux-gnueabi-strip
RUN ln -s /usr/bin/arm-linux-gnueabihf-objcopy /usr/bin/arm-linux-gnueabi-objcopy
RUN wget -nv http://qt-mirror.dannhauer.de/archive/qt/${CUR_QT_VERSION_MAJOR}/${CUR_QT_VERSION}/single/qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz \
 && tar -xf qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz -C /soft \
 && rm -f qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz \
 && mkdir -p /soft/build/qt && cd /soft/build/qt \
 && /soft/qt-everywhere-src-${CUR_QT_VERSION}/configure \
 -opensource -confirm-license -release -shared -no-pch -xplatform "linux-arm-gnueabi-g++" -extprefix "/soft/pi/usr" \
 -sysroot ${SYSROOT} -gcc-sysroot -device-option CROSS_COMPILE=${CROSS_COMPILE} \
 MYSQL_INCDIR=/soft/pi/usr/include/mysql MYSQL_LIBDIR=/soft/pi/usr/lib/arm-linux-gnueabihf \
 -opengl desktop -qt-libpng -system-zlib -qt-pcre -make tools -nomake tests -nomake examples -no-compile-examples \
 -skip qtwebengine -skip qtwebglplugin -skip doc -skip qt3d -skip qtactiveqt -skip qtquick3d -openssl-linked -xcb \
 && make -j${JOBS_QT_COUNT} && make install && rm -rf ./* && rm -rf /soft/qt-everywhere-src-*


ENV BUILD_TYPE=Release

WORKDIR /soft
