#
#  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 required GPG keys
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 \
	&& rpm --import http://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 \
    && 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 epel repo and Install all current updates
RUN yum -q -y update \
	&& yum -y install epel-release \
	&& rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 \
	&& yum -y upgrade \
	&& yum -q clean all

# Install common requirements
RUN yum -q -y update \
	&& yum -y install \
	git \
	wget \
	unzip \
	which \
	&& yum -q clean all

# Install Ruby 2.4 from SCLO repo
RUN yum -q clean expire-cache \
	&& yum -y install centos-release-scl scl-utils-build \
	&& rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo \
	&& yum -y install \
	rh-ruby24-ruby \
	rh-ruby24-ruby-libs \
	rh-ruby24-ruby-devel \
	rh-ruby24-rubygems \
	rh-ruby24-rubygem-bundler \
	&& yum -q clean packages

# Install extra dev tools
RUN yum -q clean expire-cache \
	&& yum -y install epel-release \
	&& yum -y install \
	gcc \
	gcc-c++ \
	make \
	nodejs \
	zlib-devel \
	&& yum -q clean packages

# Install extra java deps
ARG jdk=1.8.0
RUN yum -q clean expire-cache \
	&& yum -y install tzdata-java \
	java-${jdk}-openjdk-devel \
	&& yum -q clean packages

# 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

# Enable Ruby 2.4 for any later bash session
RUN echo "#!/bin/bash" > "/usr/local/bin/withruby" \
    && echo "export PATH=\${PATH}:\${HOME}/bin" >> "/usr/local/bin/withruby" \
    && echo "source scl_source enable rh-ruby24" >> "/usr/local/bin/withruby" \
    && echo 'exec "$@"' >> "/usr/local/bin/withruby" \
    && chmod 0755 "/usr/local/bin/withruby"

ENTRYPOINT [ "/usr/local/bin/withruby" ]

# 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" "/home/${user}/Gemfile"
COPY "Gemfile.lock" "/home/${user}/Gemfile.lock"
RUN chown "${user}:${group}" "/home/${user}/Gemfile"*

# Switch to non-root user and install requirements
USER ${user}
WORKDIR /home/${user}
RUN /usr/local/bin/withruby bundler install

# Prepare locales (for Jekyll)
ARG locale=en_US.UTF-8
ENV LANG "${locale}"
ENV LC_ALL "${locale}"

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