# kate: hl earthfile;

VERSION --shell-out-anywhere 0.6

# <beginfold id='1'>BEGIN</beginfold id='1'> Global parameters
ARG DEVTOOLSET_VERSION = 11
ARG DEVTOOLSET = devtoolset-${DEVTOOLSET_VERSION}
# <endfold id='1'>END</endfold id='1'> Global parameters

ARG param_1 = "String w/ variable interpolation ${DEVTOOLSET}"
ARG param_2 = 'String w/o variable interpolation ${DEVTOOLSET}'
# Run shell command
ARG param_3 = $(echo "String w/ variable interpolation ${DEVTOOLSET}")

<endfold id='2'></endfold id='2'><beginfold id='2'>sample-target:</beginfold id='2'>
    FROM alpine
    ARG stage
    COPY --if-exists files/${stage}/ /files/
    SAVE ARTIFACT --keep-ts /files /share

<endfold id='2'></endfold id='2'><beginfold id='2'>repos-conf-apt:</beginfold id='2'>
    ARG image
    ARG ppa

    FROM ${image}

    ENV DEBIAN_FRONTEND=noninteractive
    ENV DEBCONF_NONINTERACTIVE_SEEN=true

    <beginfold id='3'>IF</beginfold id='3'> [ "${ppa}" != '' ]
        ARG _more_cmds = add-apt-repository -y -n ppa:${ppa}/ppa
    ELSE
        ARG _more_cmds = :
    <endfold id='3'>END</endfold id='3'>

    RUN --mount type=secret,id=+secrets/repo_gpg_key,target=/run/secrets/private-archive-keyring.gpg \
        --mount type=cache,target=/var/cache/apt/archives \
        --mount type=cache,target=/var/lib/apt/lists \
        set -x; \
        debconf-set-selections /tmp/debconf-preset.txt \
     && rm -rf /etc/apt/apt.conf.d/docker-clean \
     && apt-get update \
     && apt-get install -y software-properties-common \
     && add-apt-repository --remove multiverse \
     && gpg -v --dearmor - </run/secrets/private-archive-keyring.gpg > /usr/share/keyrings/private-archive-keyring.gpg \
     && ${_more_cmds}

    SAVE ARTIFACT /etc/apt/sources.list /sources.list
    SAVE ARTIFACT /usr/share/keyrings/private-archive-keyring.gpg /private-archive-keyring.gpg

    <beginfold id='3'>IF</beginfold id='3'> [ "${ppa}" != '' ]
        SAVE ARTIFACT /etc/apt/sources.list.d/${ppa}*.list /sources.list.d
        SAVE ARTIFACT /etc/apt/trusted.gpg.d/${ppa}*.gpg /trusted.gpg.d
    <endfold id='3'>END</endfold id='3'>


<endfold id='2'></endfold id='2'><beginfold id='2'>APT_INSTALL:</beginfold id='2'>
    FUNCTION

    ARG image
    ARG packages = ''
    ARG packages_on_hold = ''
    ARG upgrade = 0
    ARG clean = 0
    ARG pm_aux_options = ''
    ARG more_rm_files = ''
    ARG more_cmds = :

    <beginfold id='3'>IF</beginfold id='3'> [ ! -f /root/debconf-preset.txt ]
        COPY (+repos-conf-apt/debconf-preset.txt --image=${image}) /root/debconf-preset.txt
        ARG _debconf_cmd_cond = debconf-set-selections /root/debconf-preset.txt
    ELSE
        ARG _debconf_cmd_cond = :
    <endfold id='3'>END</endfold id='3'>

    <beginfold id='3'>IF</beginfold id='3'> [ ! -f /usr/share/keyrings/private-archive-keyring.gpg ]
        COPY (+repos-conf-apt/private-archive-keyring.gpg --image=${image}) \
            /usr/share/keyrings/private-archive-keyring.gpg
        # NOTE Also copy the `sources.list` file with `multiverse` disabled!
        COPY (+repos-conf-apt/sources.list --image=${image}) /etc/apt
    <endfold id='3'>END</endfold id='3'>

    # ...

<endfold id='2'></endfold id='2'><beginfold id='2'>MAKE_PYTHON_CI_IMAGE:</beginfold id='2'>
    FUNCTION

    ARG image
    ARG python = python3
    ARG python_pkg = ${python}
    ARG python_bin = ${python}
    ARG python_ver = 3.11

    DO +APT_INSTALL --packages="${python_pkg} git"

    ARG _chroot_base = $(dirname <beginfold id='4'>$(</beginfold id='4'>cat /etc/debian_chroot<endfold id='4'>)</endfold id='4'>)

    DO ./share+IMAGE_INIT \
        --image_description="CI image for Python projects" \
        --debian_chroot=${_chroot_base}/python/${python_ver}/ci \
        --storage_name=none

    COPY --dir (+prep-python-ci/local --image=${image}) /usr

    ENV SETUPTOOLS_USE_DISTUTILS='stdlib'
    ENV PIP_DISABLE_PIP_VERSION_CHECK=1


<endfold id='2'></endfold id='2'><beginfold id='2'>all:</beginfold id='2'>
    LOCALLY

    # Primary images
    BUILD +ci
    BUILD +clang-ci
    BUILD +python-ci

    <beginfold id='3'>IF</beginfold id='3'> [ "${BUILD_TESTING}" -gt 0 ]
        # Also include all tests
        BUILD +tests
    <endfold id='3'>END</endfold id='3'>

<endfold id='2'></endfold id='2'><beginfold id='2'>tests:</beginfold id='2'>
    LOCALLY

    BUILD +ci-tests
    BUILD +clang-ci-tests
    BUILD +python-ci-tests

<endfold id='2'></endfold id='2'><beginfold id='2'>ci-tests:</beginfold id='2'>
    FROM +ci
    COPY .env /
    DO share+RUN_TESTS --VERBOSE=${VERBOSE}

<endfold id='2'></endfold id='2'><beginfold id='2'>clang-ci-tests:</beginfold id='2'>
    FROM +clang-ci
    COPY .env /
    ENV EXPECT_CLANG_VERSION=${CLANG_VERSION}
    DO share+RUN_TESTS --VERBOSE=${VERBOSE} --script=ci-tests.sh

<endfold id='2'></endfold id='2'><beginfold id='2'>python-ci-tests:</beginfold id='2'>
    FROM +python-ci
    COPY .env /
    DO share+RUN_TESTS --VERBOSE=${VERBOSE}
