# This Dockerfile demonstrates how to build and test the Mod9 ASR C++ Library in a RHEL7 system.

# Base image is RHEL 7, which uses gcc-4.8.5.
ARG BASE_IMAGE=registry.access.redhat.com/ubi7/ubi:latest

# This should also work in the CentOS analog of RHEL 7:
#ARG BASE_IMAGE=centos:7

FROM $BASE_IMAGE

# Install build tools.
RUN yum install -y gcc-c++ make

# Copy all assets from this Docker build context.
WORKDIR /opt/mod9-asr-cpp
COPY . /opt/mod9-asr-cpp

# Download a build dependency and a test audio file.
RUN curl -LO https://github.com/nlohmann/json/releases/download/v3.10.2/json.hpp
RUN cd example && curl -L -O mod9.io/hi.wav -O mod9.io/hi-8k.mulaw

# Make a (non-deterministic) tarball of these source-only assets.
# Name it "cpp" instead of "c++" so it's URL-friendly characters.
# Also include the version name so clients don't untar and collide with prior versions.
# TODO: use tar v1.28+ with the --sort=name option, and a fixed --mtime, then `gzip -n`.
RUN cd /opt && \
    cpplib_version=$(grep '^SO_VERSION' mod9-asr-cpp/Makefile | cut -d= -f2) && \
    mv mod9-asr-cpp mod9-asr-cpp-${cpplib_version} && \
    ln -s mod9-asr-cpp-${cpplib_version} mod9-asr-cpp && \
    tar cvfz mod9-asr-cpp-src-${cpplib_version}.tar.gz mod9-asr-cpp-${cpplib_version}

# Build the (static) library.
RUN make

# Build and test the example program (statically linked).
RUN cd example && \
    make && \
    ./mod9-asr-example mod9.io 9900 hi.wav hi-8k.mulaw

# Also build and test the shared library.
RUN make shared && \
    cd example && \
    make clean && \
    LDFLAGS="-L.. -Wl,-rpath -Wl,$(realpath ..)" \
    LDLIBS="-lpthread -lmod9-asr" \
    make && \
    ./mod9-asr-example mod9.io 9900 hi.wav hi-8k.mulaw

# Clean up intermediate files
RUN make clean && make -C example clean
