FROM centos:6.10
MAINTAINER Andreas Romeyke <romeyke@slub-dresden.de>

# based on https://hub.docker.com/r/rpmbuild/centos6/
## next steps needed because deprecated certificates
RUN yum clean all -y
RUN yum check -y
RUN yum update ca-certificates -y
RUN yum upgrade -y

## install base environment
RUN yum install -v -y libtool
RUN yum install -v -y libtool-ltdl
RUN yum install -v -y make cmake
RUN yum install -v -y pkgconfig 
RUN yum install -v -y sudo 
RUN yum install -v -y automake autoconf 
RUN yum install -v -y wget 
RUN yum install -v -y yum-utils rpm-build 
RUN wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
# git [devtools-2] (1.8.4) for "rev-list --count" feature, git [centos6] (1.7.1) too old
RUN yum install -y devtoolset-2-git
RUN ln -s /opt/rh/devtoolset-2/root/usr/bin/git /usr/bin/git
RUN yum install -y devtoolset-2-gcc devtoolset-2-binutils
RUN yum install -y devtoolset-2-gcc-c++ devtoolset-2-libstdc++-devel devtoolset-2-toolchain
#RUN yum install -y devtoolset-2
RUN scl enable devtoolset-2 bash
RUN yum install -v -y tree
RUN yum install -v -y autoconf automake libtool pkgconfig
RUN yum install -v -y zlib-devel libsqlite-devel
RUN yum install -v -y python-devel

## add user environment
RUN useradd builder -u 1000 -m -G users,wheel && \
    echo "builder ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers && \
    echo "# macros"                      >  /home/builder/.rpmmacros && \
    echo "%_topdir    /home/builder/rpm" >> /home/builder/.rpmmacros && \
    echo "%_sourcedir %{_topdir}"        >> /home/builder/.rpmmacros && \
    echo "%_builddir  %{_topdir}"        >> /home/builder/.rpmmacros && \
    echo "%_specdir   %{_topdir}"        >> /home/builder/.rpmmacros && \
    echo "%_rpmdir    %{_topdir}"        >> /home/builder/.rpmmacros && \
    echo "%_srcrpmdir %{_topdir}"        >> /home/builder/.rpmmacros && \
    mkdir /home/builder/rpm && \
    chown -R builder /home/builder
USER builder
RUN scl enable devtoolset-2 bash
ENV FLAVOR=rpmbuild OS=centos DIST=el6 CC=/opt/rh/devtoolset-2/root/usr/bin/gcc CXX=/opt/rh/devtoolset-2/root/usr/bin/g++
ENV CXXFLAGS="-O3"
CMD /srv/pkg
WORKDIR /home/builder/
ENV DISTDIR=/home/builder/distribution/usr/local
RUN mkdir -p $DISTDIR

# get and compile libxml2, because RHEL uses outdated and errorneous version
# we disabled python support and building static variants only
RUN wget -np https://gitlab.gnome.org/GNOME/libxml2/-/archive/master/libxml2-master.tar.gz
RUN tar xfz libxml2-master.tar.gz
RUN cd libxml2-master && ./autogen.sh
RUN cd libxml2-master && ./configure --help
RUN cd libxml2-master &&\
    ./configure --prefix=$DISTDIR --without-python --disable-shared --enable-static &&\
    make -j &&\
    make install
ENV PATH=$PATH:$DISTDIR/bin
RUN echo $PATH


# build of libxslt v1.1.34 will fail, expects newer version of autotools
ENV libxsltVERSION=v1.1.33

# get and compile libxslt, because RHEL uses outdated and errorneous version
# the configure-script must be patched, because empty else branch somewhere
# also we disabled python support and crypto  and building static variants only
RUN wget -np https://gitlab.gnome.org/GNOME/libxslt/-/archive/${libxsltVERSION}/libxslt-${libxsltVERSION}.tar.gz
RUN tar xfz libxslt-${libxsltVERSION}.tar.gz
RUN cd libxslt-${libxsltVERSION} && ./autogen.sh
# patch errorneous configure for v1.1.33
RUN cd libxslt-${libxsltVERSION} && sed -i -e "16113s/.*/echo nothing/" configure
#RUN cd libxslt-${libxsltVERSION} && (cat -n configure | head -n 16122 | tail -n 10)
RUN cd libxslt-${libxsltVERSION} && \
    ./configure --prefix=$DISTDIR --without-python --without-crypto \
    --disable-shared --enable-static \
    --with-libxml-src=/home/builder/libxml2-master \
    --with-libxml-prefix=$DISTDIR/bin \
    --with-libxml-include-prefix=$DISTDIR/include \
    --with-libxml-libs-prefix=$DISTDIR/lib
RUN cd libxslt-${libxsltVERSION} && make -j && make install




# get and compile libzen
RUN git clone https://github.com/MediaArea/ZenLib.git

# next lines only usefull if mediaconch build with cmake
#RUN cd ZenLib && \
#    mkdir build &&\
#    cd build &&\
#    cmake ../Project/CMake -DCMAKE_INSTALL_PREFIX=$DISTDIR &&\
#    make -j &&\
#    make install

# we patched autogen.sh and configure.ac because precedence of user settings is broken
# also we building static variants only
RUN cd ZenLib &&\
    cd Project/GNU/Library && \
    sed -e "s/-O2/-O3/g" -i autogen.sh && \
    sed -e "s/-O2/-O3/g" -i configure.ac && \
    ./autogen.sh
RUN cd ZenLib/Project/GNU/Library &&\
   ./configure --prefix=$DISTDIR  --disable-shared --enable-static &&\
    make -j &&\
    make install
ENV ZenLibDir $DISTDIR


# get and compile libmediainfo
RUN git clone https://github.com/MediaArea/MediaInfoLib.git

# next lines only usefull if mediaconch build with cmake
#RUN cd MediaInfoLib &&\
#    mkdir build &&\
#    cd build &&\
#    cmake ../Project/CMake -DCMAKE_INSTALL_PREFIX=$DISTDIR &&\
#    make -j &&\
#    make install

# we patched autogen.sh and configure.ac because precedence of user settings is broken
# also we building static variants only
RUN cd MediaInfoLib &&\
    cd Project/GNU/Library && \
    sed -e "s/-O2/-O3/g" -i autogen.sh && \
    sed -e "s/-O2/-O3/g" -i configure.ac && \
    ./autogen.sh
RUN cd MediaInfoLib/Project/GNU/Library/ &&\
   ./configure --prefix=$DISTDIR  --disable-shared --enable-static &&\
    make -j &&\
    make install

# because Mediaconch configure script expects libml2 and libxslt as hardcoded
# names, we set symlinks
RUN ln -s libxml2-master libxml2
RUN ln -s libxslt-${libxsltVERSION} libxslt

# get and compile mediaconch
RUN git clone https://github.com/MediaArea/MediaConch_SourceCode.git
    #cmake ../Project/CMake -DCMAKE_INSTALL_PREFIX=$DISTDIR &&\

# we patched autogen.sh and configure.ac because precedence of user settings is broken
# also we building static variants only
# HINT: the -lrt switch is needed for RHEL6, because glibc is outdated
RUN cd MediaConch_SourceCode &&\
    cd Project/GNU/CLI && \
    sed -e "s/-O2/-O3/g" -i autogen.sh && \
    sed -e "s/-O2/-O3/g" -i configure.ac && \
    ./autogen.sh
RUN cd MediaConch_SourceCode/Project/GNU/CLI/ &&\
   ./configure --help
RUN cd MediaConch_SourceCode/Project/GNU/CLI/ &&\
   CXXFLAGS="-O3 -I$DISTDIR/include -L/lib -L/lib64 -lrt -L$DISTDIR/lib -L$DISTDIR/lib64" \
   ./configure --prefix=$DISTDIR --without-sqlite --without-jansson\
   --without-libevent  --disable-shared --enable-static --enable-staticlibs

RUN cd MediaConch_SourceCode/Project/GNU/CLI/ &&\
    make -j &&\
    make install

RUN tree $DISTDIR
RUN cd $DISTDIR/../../ && tar -cz -f /tmp/mediaconch.tgz *
RUN ls -lha /tmp/
RUN echo "Finished, in /tmp/mediaconch.tgz should be the binary found"
