ARG image_from
FROM ${image_from}

ARG image_prepare_script
ARG JOBS_COUNT=4
ARG JOBS_QT_COUNT=2
ARG QT_FROM_SOURCE=1
ARG VERSION_ID=

# versions and environment
ENV CUR_CMAKE_VERSION=3.20.0
ENV CUR_SODIUM_VERSION=1.0.21
ENV CUR_FFTW_VERSION=3.3.10
ENV CUR_ASSIMP_VERSION=4.1.0
ENV CUR_DISCOUNT_VERSION=2.2.6
ENV CUR_MICROHTTPD_VERSION=1.0.2
ENV CUR_QT_VERSION_MAJOR=5.15
ENV CUR_QT_VERSION=5.15.9
ENV CUR_GOOGLETEST=v1.13.x

ENV QT_SELECT=5
ENV LANG=en_US.utf8

# apply RU mirror
RUN sed -i 's/deb.debian.org/mirror.mephi.ru/g' /etc/apt/sources.list || true
RUN sed -i 's/deb.debian.org/mirror.mephi.ru/g' /etc/apt/sources.list.d/* || true

# configs
COPY .bashrc /root/.bashrc
COPY inputrc /etc/inputrc
RUN echo 'root:12345' | chpasswd

# prepare current distributive
COPY *.sh /soft/
RUN if [ -n "${VERSION_ID}" ]; then \
		echo "VERSION_ID=\"${VERSION_ID}\"" >> /usr/lib/os-release; \
	fi
RUN if [ -n "${image_prepare_script}" ]; then \
		bash /soft/${image_prepare_script} ${QT_FROM_SOURCE}; \
	fi
RUN rm /soft/*.sh -v


# last cmake from sources, into /opt
WORKDIR /soft
RUN wget -nv https://shstk.ru/files/cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh \
 && mkdir -p /opt/cmake \
 && bash ./cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh --skip-license --prefix=/opt/cmake \
 && rm -rf /soft/cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh
ENV PATH=/opt/cmake/bin:$PATH 

# Remove invalid FindGtest.cmake
RUN cd "$(find /opt/cmake/share -name FindGTest.cmake -type f -printf '%h' -quit)" && rm FindGTest.cmake


# GTest
WORKDIR /soft
RUN git clone -b ${CUR_GOOGLETEST} https://github.com/google/googletest.git \
 && mkdir googletest/build && cd googletest/build \
 && cmake .. \
 && make -j${JOBS_COUNT} && make install && cd /soft && rm -rf googletest


# sodium
WORKDIR /soft
RUN curl -s 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/build/sodium && cd /soft/build/sodium \
 && sh -c 'CFLAGS="-O3 -fPIC" LDFLAGS="-O3 -fPIC" /soft/libsodium-*/configure --prefix=/usr/local/ && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/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/build/fftw3 && cd /soft/build/fftw3 \
 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ -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 /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ -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 /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ -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 /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/build/assimp && cd /soft/build/assimp \
 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DASSIMP_BUILD_ASSIMP_TOOLS=0 -DASSIMP_BUILD_TESTS=0 -DBUILD_FRAMEWORK=0 /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/build/discount && mv /soft/linux/build/mktags /soft/build/discount/mktags
WORKDIR /soft/build/discount
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake \
&& make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/discount-*


# 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/build/microhttpd && cd /soft/build/microhttpd \
 && sh -c 'CFLAGS="-O3 -fPIC" LDFLAGS="-O3 -fPIC" /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}/configure --prefix=/usr/local/ --enable-shared=no --enable-static=yes && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/build/microhttpd && rm -rf /soft/libmicrohttpd-*


# Qt
WORKDIR /soft
RUN if [ "${QT_FROM_SOURCE}" = "1" ]; then \
		wget -nv https://shstk.ru/files/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 -extprefix "/usr/local" \
		-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-*; \
	fi


ENV BUILD_TYPE=Release

WORKDIR /soft
