# Copyright 2021 Red Hat
#
# 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.
#
# ------------------------------------------------------------------------
#
FROM registry.redhat.io/ubi8/ubi-minimal:latest as builder

COPY $REMOTE_SOURCES $REMOTE_SOURCES_DIR  

WORKDIR /

RUN microdnf -y --setopt=install_weak_deps=0 --setopt=tsflags=nodocs install \
    gcc gcc-c++ make cmake \
    cyrus-sasl-devel openssl-devel libuuid-devel \
    python3-devel swig \
    libnghttp2-devel \
    wget tar patch findutils git libasan libubsan \
 && microdnf clean all -y

WORKDIR /build
COPY . .

ARG VERSION=UNKNOWN
ENV VERSION=$VERSION
RUN scripts/compile.sh
RUN tar zxpf /qpid-proton-image.tar.gz --one-top-level=/image && tar zxpf /skupper-router-image.tar.gz --one-top-level=/image && tar zxpf /libwebsockets-image.tar.gz --one-top-level=/image

FROM registry.redhat.io/ubi8/ubi-minimal:latest

#COPY $REMOTE_SOURCES $REMOTE_SOURCES_DIR

# gdb and sanitizers are part of final image as they can be used as debug options for Skupper
RUN microdnf -y --setopt=install_weak_deps=0 --setopt=tsflags=nodocs install \
    glibc \
    cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl-gssapi openssl \
    python3 \
    libnghttp2 \
    gdb libasan libubsan \
    gettext hostname iputils \
 && microdnf clean all

WORKDIR /
COPY --from=builder /image /

WORKDIR /home/skrouterd/etc
WORKDIR /home/skrouterd/bin
COPY --from=builder $REMOTE_SOURCES_DIR/skupper-router/app/scripts/* /home/skrouterd/bin

ARG version=latest
ENV VERSION=${version}
ENV QDROUTERD_HOME=/home/skrouterd

ENV ASAN_OPTIONS="disable_coredump=0 detect_odr_violation=0 strict_string_checks=1 detect_stack_use_after_return=1 check_initialization_order=1 strict_init_order=1 detect_invalid_pointer_pairs=2"
ENV LSAN_OPTIONS="disable_coredump=0 suppressions=/lsan.supp"
ENV TSAN_OPTIONS="disable_coredump=0 history_size=4 second_deadlock_stack=1 suppressions=/tsan.supp"
ENV UBSAN_OPTIONS="disable_coredump=0 print_stacktrace=1 print_summary=1"

EXPOSE 5671 5672 45671 55671
CMD ["/home/skrouterd/bin/launch.sh"]      

LABEL \
      com.redhat.component="skupper-router-container"  \
      description="Skupper Router container image"  \
      io.k8s.description="A service router for building scalable, available, and performant virtual application networks"  \
      io.k8s.display-name="Skupper Router"  \
      io.openshift.expose-services="5672:amqp,5671:amqps,8672:http"  \
      io.openshift.tags="messaging,amq,integration"  \
      maintainer="Mike Cressman <mcressma@redhat.com>"  \
      name="application-interconnect/skupper-router-rhel8"  \
      summary="Skupper Router"  \
      version="2.0.2"
