# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
# WARNING: THIS DOCKERFILE IS NOT INTENDED FOR PRODUCTION USE OR DEPLOYMENT.
#
FROM debian:stretch-slim

SHELL ["/bin/bash", "-o", "pipefail", "-e", "-u", "-x", "-c"]

# Make sure noninteractie debian install is used and language variables set
ENV DEBIAN_FRONTEND=noninteractive LANGUAGE=C.UTF-8 LANG=C.UTF-8 LC_ALL=C.UTF-8 \
    LC_CTYPE=C.UTF-8 LC_MESSAGES=C.UTF-8

ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

# Note missing man directories on debian-stretch
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863199
# Install OpenJDK
RUN mkdir -pv /usr/share/man/man1 \
    && mkdir -pv /usr/share/man/man7 \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
            sudo \
            curl \
            openjdk-8-jdk \
    && apt-get autoremove -yqq --purge \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ARG RAT_VERSION="0.13"

ENV RAT_VERSION="${RAT_VERSION}" \
    RAT_JAR="/tmp/apache-rat-${RAT_VERSION}.jar" \
    RAT_URL="https://repo1.maven.org/maven2/org/apache/rat/apache-rat/${RAT_VERSION}/apache-rat-${RAT_VERSION}.jar"
ENV RAT_JAR_MD5="${RAT_JAR}.md5" \
    RAT_URL_MD5="${RAT_URL}.md5"

RUN echo "Downloading RAT from ${RAT_URL} to ${RAT_JAR}" \
    && curl -sL "${RAT_URL}" > "${RAT_JAR}" \
    && curl -sL "${RAT_URL_MD5}" > "${RAT_JAR_MD5}" \
    && jar -tf "${RAT_JAR}" \
    && md5sum -c <<<"$(cat "${RAT_JAR_MD5}") ${RAT_JAR}"

ARG  DUMB_INIT_VERSION="1.2.2"
ENV  DUMB_INIT_VERSION="${DUMB_INIT_VERSION}" \
     DUMB_INIT_FILE="/tmp/dumb-init_${DUMB_INIT_VERSION}_amd64.deb" \
     DUMB_INIT_FILE_SHA256SUMS="/tmp/sha256sums" \
     DUMB_INIT_URL="https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64.deb" \
     DUMB_INIT_URL_SHA256SUMS="https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/sha256sums"

WORKDIR /tmp
RUN echo "Downloading dumb-init from ${DUMB_INIT_URL} to ${DUMB_INIT_FILE}" \
    && curl -sL "${DUMB_INIT_URL}" > "${DUMB_INIT_FILE}" \
    && curl -sL "${DUMB_INIT_URL_SHA256SUMS}" > "${DUMB_INIT_FILE_SHA256SUMS}" \
    && sha256sum --check --ignore-missing "${DUMB_INIT_FILE_SHA256SUMS}" \
    && dpkg -i "${DUMB_INIT_FILE}"

ARG AIRFLOW_USER=airflow
ENV AIRFLOW_USER=${AIRFLOW_USER}

ARG HOME=/home/airflow
ENV HOME=${HOME}

ARG AIRFLOW_HOME=${HOME}/airflow
ENV AIRFLOW_HOME=${AIRFLOW_HOME}

ARG AIRFLOW_SOURCES=/opt/airflow
ENV AIRFLOW_SOURCES=${AIRFLOW_SOURCES}

RUN adduser airflow \
    && echo "airflow ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/airflow \
    && chmod 0440 /etc/sudoers.d/airflow

COPY --chown=airflow:airflow scripts ${AIRFLOW_SOURCES}/scripts

USER airflow

WORKDIR ${AIRFLOW_SOURCES}

ENTRYPOINT ["/opt/airflow/scripts/ci/in_container/run_check_licence.sh"]
