ARG image_from
FROM ${image_from}

ARG image_prepare_script
ARG JOBS_COUNT=4

ENV QT_SELECT=5
ENV LANG=en_US.utf8
ENV CUR_FFTW_VERSION=3.3.10
ENV CUR_MICROHTTPD_VERSION=1.0.2

RUN apt-get update && apt-get install -y ca-certificates gpg

# configs
COPY .bashrc /root/.bashrc
COPY inputrc /etc/inputrc
COPY os-release /usr/lib/os-release
COPY astra.list /etc/apt/sources.list.d/astra.list
COPY key.gpg /soft/key.gpg
RUN echo 'root:12345' | chpasswd

# prepare current distributive
RUN cat /soft/key.gpg | apt-key add -
RUN apt-get update
COPY *.sh /soft/
RUN if [ -n "${image_prepare_script}" ]; then \
		bash /soft/${image_prepare_script} ${QT_FROM_SOURCE}; \
	fi

ENV BUILD_TYPE=Release

# 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 -DBUILD_SHARED_LIBS=0 /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 -DBUILD_SHARED_LIBS=0 /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  -DBUILD_SHARED_LIBS=0 /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*

# 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-*

WORKDIR /soft
