#/*
# * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# * contributor license agreements.  See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The OpenAirInterface Software Alliance licenses this file to You under
# * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
# *
# * 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.
# *-------------------------------------------------------------------------------
# * For more information about the OpenAirInterface (OAI) Software Alliance:
# *      contact@openairinterface.org
# */
#---------------------------------------------------------------------
#
# Dockerfile for the Open-Air-Interface AMF service
#   Valid for RHEL 8.2
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# BUILDER IMAGE
#---------------------------------------------------------------------
FROM registry.access.redhat.com/ubi8/ubi:latest AS oai-amf-builder

ARG NEEDED_GIT_PROXY

COPY tmp/ca/redhat-uep.pem /etc/rhsm/ca
COPY tmp/entitlement/*.pem /etc/pki/entitlement

RUN rm -f /etc/rhsm-host && \
    yum repolist --disablerepo=* && \
    subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms && \
    yum update -y && \
    yum install -y \
        psmisc \
        git \
        diffutils \
        file \
        gcc-c++ \
        cmake3 \
        ninja-build \
        boost-devel \
        openssl-devel \
        openssl \
        libasan && \
    echo "/usr/local/lib" > /etc/ld.so.conf.d/local-lib.conf && \
    echo "/usr/local/lib64" >> /etc/ld.so.conf.d/local-lib.conf

# Some GIT configuration command quite useful
RUN /bin/bash -c "if [[ -v NEEDED_GIT_PROXY ]]; then git config --global http.proxy $NEEDED_GIT_PROXY; fi" && \
    git config --global https.postBuffer 123289600 && \
    git config --global http.sslverify false

# Copying source code
WORKDIR /openair-amf/
COPY . /openair-amf

# Installing all the needed libraries/packages to build and run AMF
WORKDIR /openair-amf/build/scripts
RUN ./build_amf --install-deps --force

# Building AMF
WORKDIR /openair-amf/build/scripts
RUN ./build_amf --clean --Verbose --build-type Release --jobs && \
    mv /openair-amf/build/amf/build/amf /openair-amf/build/amf/build/oai_amf

#---------------------------------------------------------------------
# TARGET IMAGE
#---------------------------------------------------------------------
FROM registry.access.redhat.com/ubi8/ubi:latest as oai-amf
ENV TZ=Europe/Paris
# We install some debug tools for the moment in addition of mandatory libraries
RUN yum update -y && \
    yum install -y --enablerepo="ubi-8-codeready-builder" \
      tzdata \
      libasan \
      procps-ng \
      psmisc \
      net-tools \
      libevent && \
    rm -rf /var/lib/apt/lists/*

# Copying executable and generated libraries
WORKDIR /openair-amf/bin
COPY --from=oai-amf-builder \
    /openair-amf/build/amf/build/oai_amf \
    /openair-amf/scripts/entrypoint.sh \
    ./

COPY --from=oai-amf-builder \
    /usr/lib64/libconfig++.so.9 \
    /usr/lib64/libboost_system.so.1.66.0 \
    /usr/local/lib64/libpistache.so.0 \
    /usr/local/lib/libnghttp2_asio.so \
    /usr/local/lib/libnghttp2_asio.so.1 \
    /usr/lib64/mysql/libmysqlclient.so.21 \
    /usr/lib64/libsctp.so.1 \
    /usr/local/lib64/libcpprest.so.2.10 \
    /usr/lib64/libboost_random.so.1.66.0 \
    /usr/lib64/libboost_thread.so.1.66.0 \
    /usr/lib64/libboost_filesystem.so.1.66.0 \
    /usr/lib64/libboost_chrono.so.1.66.0 \
    /usr/lib64/libboost_atomic.so.1.66.0 \
    /usr/lib64/libboost_date_time.so.1.66.0 \
    /usr/lib64/libboost_regex.so.1.66.0 \
    /usr/lib64/libicudata.so.60 \
    /usr/lib64/libicui18n.so.60 \
    /usr/lib64/libicuuc.so.60 \
    /usr/lib64/

RUN ldconfig

# Copying template configuration files
WORKDIR /openair-amf/etc
COPY --from=oai-amf-builder /openair-amf/etc/amf.conf .

WORKDIR /openair-amf

# expose ports
EXPOSE 80/tcp 9090/tcp 38412/sctp

ENTRYPOINT ["/openair-amf/bin/entrypoint.sh"]
CMD ["/openair-amf/bin/oai_amf", "-c", "/openair-amf/etc/amf.conf", "-o"]
