#
#  This work is protected under copyright law in the Kingdom of
#  The Netherlands. The rules of the Berne Convention for the
#  Protection of Literary and Artistic Works apply.
#  Digital Me B.V. is the copyright owner.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

# Pull base image from official repo
FROM centos:centos7.7.1908

# Import local GPG keys and enable epel repo
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 && \
    yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs epel-release && \
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 && \
    yum -q -y clean all --enablerepo='*'

# Install common requirements
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      git \
      wget \
      unzip \
      which \
    && \
    yum -q -y clean all --enablerepo='*'

# Import extra GPG keys
RUN rpm --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppet && \
    rpm --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs && \
    rpm --import http://yum.mariadb.org/RPM-GPG-KEY-MariaDB

# Enable Software Collections
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      centos-release-scl \
      scl-utils-build \
    && \
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo && \
    yum -q -y clean all --enablerepo='*'

# Install Ruby 2.6 from SCLO repo
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      rh-ruby26-ruby \
      rh-ruby26-ruby-libs \
      rh-ruby26-ruby-devel \
      rh-ruby26-rubygems \
      rh-ruby26-rubygem-bundler \
      && \
    yum -q -y clean all --enablerepo='*'

# Install extra dev tools
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      gcc \
      gcc-c++ \
      make \
      zlib-devel \
    && \
    yum -q -y clean all --enablerepo='*'

# Install NodeJS 10 from SCLO repo
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      rh-nodejs10-nodejs \
      rh-nodejs10-npm \
    && \
    yum -q -y clean all --enablerepo='*'

# Enable SCLs for any later bash session
COPY scl_enable.sh /usr/local/bin/scl_enable
ENV BASH_ENV="/usr/local/bin/scl_enable" \
    ENV="/usr/local/bin/scl_enable" \
    PROMPT_COMMAND=". /usr/local/bin/scl_enable"
COPY container-entrypoint.sh /usr/local/bin/container-entrypoint
RUN chmod 0755 /usr/local/bin/container-entrypoint
ENTRYPOINT [ "/usr/local/bin/container-entrypoint" ]

# Configure desired timezone
ENV TZ=Europe/Amsterdam
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone

# Install JDK
ARG jdk=1.8.0
RUN yum -q clean expire-cache && \
    yum -q -y update && \
    yum -y install --setopt=tsflags=nodocs \
      tzdata-java \
      java-${jdk}-openjdk-devel \
    && \
    yum -q -y clean all --enablerepo='*'

# Create a symbolic link to latest JVM - for legacy scripts
RUN mkdir /usr/java && \
    ln -s /usr/lib/jvm/java-${jdk}-openjdk /usr/java/latest

# Parameters for default user:group
ARG uid=1000
ARG user=android
ARG gid=1000
ARG group=android

# Add user to build
RUN groupadd -g "${gid}" "${group}" && \
    useradd -ms /bin/bash -g "${group}" -u "${uid}" "${user}"

# Copy requirements in non-root user home directory
COPY Gemfile Gemfile.lock "/home/${user}/"
RUN chown "${user}:${group}" "/home/${user}/Gemfile"*

# Switch to non-root user
USER ${user}
WORKDIR /home/${user}

# Install Fastlane for this user
RUN source /usr/local/bin/scl_enable && \
    echo "gem: --no-document --user-install --bindir /home/${user}/bin" >> /home/${user}/.gemrc && \
    echo "gempath: /home/${user}/.gem/ruby:/home/${user}/.bundle/gems/ruby/2.6.0:/opt/rh/rh-ruby26/root/usr/share/gems" >> .gemrc && \
    gem install bundler --version `sed -n -r -e '/BUNDLED WITH/,$ { s/\s+([.0-9]+)/\1/ p }' Gemfile.lock` && \
    bundle config --global path /home/${user}/.bundle/gems && \
    bundle config --global bin /home/${user}/bin && \
    bundle install && \
    rm -rf /home/${user}/.bundle/cache

# Install Firebase CLI for this user
RUN source /usr/local/bin/scl_enable && \
    echo "prefix = /home/${user}" >> /home/${user}/.npmrc && \
    npm install -g firebase-tools

# Prepare locales and other variables
ARG locale=en_US.UTF-8
ENV LANG "${locale}"
ENV LC_ALL "${locale}"
ENV USER ${user}

# Get script directory from lazyLib at last to avoid warning w/o invalidating the cache 
ARG dir=.
