# this file is used to build the image gpsbabel_build_environment used by travis.

FROM ubuntu:bionic as qt_install_prep
WORKDIR /app

# update environment.
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
    apt-utils \
 && apt-get upgrade -y \
 && rm -rf /var/lib/apt/lists/*

# install packages needed to install Qt
RUN apt-get update && apt-get install -y --no-install-recommends \
  libdbus-1-3 \
  libfreetype6 \
  libfontconfig1 \
  libx11-6 \
  libx11-xcb1 \
  libxext6 \
  libxkbcommon0 \
  libxkbcommon-x11-0 \
  libxrender1 \
  ca-cacert \
  wget \
 && rm -rf /var/lib/apt/lists/*

FROM qt_install_prep as qt_install
# basic build and test tools
ARG INSTALLER
ARG QT_CI_PACKAGES
ARG QT_VERSION
COPY ./extract-qt-installer ./qt-install.qs ./$INSTALLER /app/
RUN ./extract-qt-installer $INSTALLER /opt/Qt \
 && rm ./extract-qt-installer ./qt-install.qs ./$INSTALLER \
 && echo "export PATH=/opt/Qt/$QT_VERSION/gcc_64/bin:$PATH" > /opt/qt-$QT_VERSION.env

FROM ubuntu:bionic
LABEL maintainer="https://github.com/tsteven4"
WORKDIR /app
ARG QT_VERSION

# update environment.
# bionic needs a newer git than provided by ubuntu:bionic
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
    apt-utils \
    software-properties-common \
 && apt-get upgrade -y \
 && add-apt-repository ppa:git-core/ppa \
 && rm -rf /var/lib/apt/lists/*

# install packages needed for gpsbabel build
# split into multiple commands to limit layer size

# basic build and test tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    g++ \
    make \
    autoconf \
    git \
    valgrind \
    expat \
    libxml2-utils \
    bear \
    cmake \
    ninja-build \
 && rm -rf /var/lib/apt/lists/*

# alternative compiler
RUN apt-get update && apt-get install -y --no-install-recommends \
    clang \
 && rm -rf /var/lib/apt/lists/*

# pkgs needed to build document
RUN apt-get update && apt-get install -y --no-install-recommends \
    fop \
    xsltproc \
    docbook-xml \
    docbook-xsl \
 && rm -rf /var/lib/apt/lists/*

# pkgs with libraries needed by gpsbabel
RUN apt-get update && apt-get install -y --no-install-recommends \
    libusb-1.0-0-dev \
    pkg-config \
    libudev-dev \
 && rm -rf /var/lib/apt/lists/*

# dependencies of Qt
RUN apt-get update && apt-get install -y --no-install-recommends \
    libglib2.0-0 \
    libx11-xcb-dev \
    libglu1-mesa-dev \
    libasound2 \
    libxcomposite1 \
    libxcursor1 \
 && rm -rf /var/lib/apt/lists/*

# Qt
COPY --from=qt_install /opt/qt-$QT_VERSION.env /opt/qtio.env
COPY --from=qt_install /opt/Qt/$QT_VERSION /opt/Qt/$QT_VERSION
COPY --from=qt_install /opt/Qt/Licenses /opt/Qt/Licenses

# pkgs needed to generate coverage report:
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcovr \
 && rm -rf /var/lib/apt/lists/*

# install environment for locale test
RUN apt-get update && apt-get install -y --no-install-recommends \
    locales \
 && rm -rf /var/lib/apt/lists/* \
 && sed -i 's/^# *\(en_US ISO-8859-1\)/\1/' /etc/locale.gen \
 && locale-gen \
 && locale -a
