ARG image_from
FROM --platform=linux/amd64 ${image_from}

ARG JOBS_COUNT=4
ARG image_prepare_script
ARG arch
ARG toolchain
ARG triplet

# versions and environment
ENV CUR_CMAKE_VERSION=3.20.0
ENV CUR_GOOGLETEST=v1.13.x
 
ENV QT_SELECT=5
ENV LANG=en_US.utf8


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

# prepare current distributive
COPY *.sh /soft/
RUN if [ -n "${image_prepare_script}" ]; then \
		bash /soft/${image_prepare_script} ${arch} ${toolchain}; \
	fi
RUN rm /soft/*.sh -v

# toolchain
WORKDIR /soft
COPY toolchain.cmake.in /soft/
RUN sed -e s/\${PH_ARCH}/$arch/ -e s/\${PH_TRIPLET}/$triplet/ toolchain.cmake.in > toolchain.cmake
RUN rm toolchain.cmake.in
RUN mkdir -p /usr/${triplet}/lib
RUN mkdir -p /soft/target/lib
RUN ln -s /usr/lib/os-release /usr/${triplet}/lib/os-release
RUN ln -s /usr/lib/os-release /soft/target/lib/os-release


# 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


ENV BUILD_TYPE=Release

WORKDIR /soft
