# Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.

FROM base as muslib

ARG TEMP_REGION=""
ARG MUSL_LOCATION=http://more.musl.cc/10/x86_64-linux-musl/x86_64-linux-musl-native.tgz
ARG ZLIB_LOCATION=https://zlib.net/fossils/zlib-1.2.11.tar.gz

ENV TOOLCHAIN_DIR=/usr/local/musl \
    CC=$TOOLCHAIN_DIR/bin/gcc

RUN echo "$TEMP_REGION" > /etc/dnf/vars/ociregion \
    && rm -rf /etc/yum.repos.d/ol8_graalvm_community.repo \
    && mkdir -p $TOOLCHAIN_DIR \
    && microdnf install -y wget tar gzip make \
    && wget $MUSL_LOCATION && tar -xvf  x86_64-linux-musl-native.tgz -C $TOOLCHAIN_DIR --strip-components=1  \
    && wget $ZLIB_LOCATION && tar -xvf zlib-1.2.11.tar.gz \
    && cd zlib-1.2.11 \
    && ./configure --prefix=$TOOLCHAIN_DIR --static \
    && make && make install


FROM base as final

COPY --from=muslib /usr/local/musl /usr/local/musl

RUN echo "" > /etc/dnf/vars/ociregion

ENV TOOLCHAIN_DIR=/usr/local/musl \
    CC=$TOOLCHAIN_DIR/bin/gcc

ENV PATH=$TOOLCHAIN_DIR/bin:$PATH