FROM centos:7

RUN yum install -y epel-release
RUN yum install -y yum install https://centos7.iuscommunity.org/ius-release.rpm

RUN set -x \
    && yum -y install \
        libstdc++ \
        libstdc++-static \
        readline \
        openssl \
        lz4 \
        binutils \
        ncurses \
        libgomp \
        lua \
        curl \
        tar \
        zip \
        unzip \
        libunwind \
        zlib \
    && yum -y install \
        perl \
        gcc-c++ \
        cmake \
        lz4-devel \
        binutils-devel \
        lua-devel \
        make \
        git \
        autoconf \
        automake \
        libtool \
        wget

RUN yum -y install ncurses-static readline-static zlib-static pcre-static glibc-static

RUN yum -y install python-devel python-pip

RUN set -x && \
    cd / && \
    curl -O -L https://www.openssl.org/source/openssl-1.1.0h.tar.gz && \
    tar -xvf openssl-1.1.0h.tar.gz && \
    cd openssl-1.1.0h && \
    ./config --libdir=lib && \
    make && make install

RUN set -x && \
    cd / && \
    wget http://download.icu-project.org/files/icu4c/62.1/icu4c-62_1-src.tgz && \
    tar -xvf icu4c-62_1-src.tgz && \
    cd icu/source && \
    ./configure --with-data-packaging=static --enable-static --enable-shared && \
    make && make install

RUN set -x && \
    cd / && \
    LD_LIBRARY_PATH=/usr/local/lib64 curl -O -L http://download.savannah.nongnu.org/releases/libunwind/libunwind-1.3-rc1.tar.gz && \
    tar -xvf libunwind-1.3-rc1.tar.gz && \
    cd libunwind-1.3-rc1 && \
    ./configure --enable-static --enable-shared && \
    make && make install

COPY . /tarantool

RUN set -x && \
    cd tarantool && \
    git submodule init && \
    git submodule update

WORKDIR /tarantool

RUN set -x && \
    find . -name 'CMakeFiles' -type d -exec rm -rf {} + && \
    find . -name 'CMakeCache.txt' -type f -delete

RUN pip install -r /tarantool/test-run/requirements.txt

RUN set -x \
    && (cd /tarantool; \
       cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
             -DENABLE_DIST:BOOL=ON \
             -DBUILD_STATIC=ON \
             -DOPENSSL_USE_STATIC_LIBS=ON \
             -DOPENSSL_ROOT_DIR=/usr/local \
             .) \
    && make -C /tarantool -j

RUN cd /tarantool && make install

ARG RUN_TESTS
RUN if [ -n "${RUN_TESTS}" ]; then \
        set -x && \
        cd test && \
        /usr/bin/python test-run.py --force; \
    fi

ENTRYPOINT /bin/bash