# Base Containerfile for Debcraft
FROM debian:sid

ENV DEBIAN_FRONTEND=noninteractive

# Stretch is no longer available at deb.debian.org, use archive.debian.org instead
RUN if grep -q 'PRETTY_NAME="Debian GNU/Linux 9 (stretch)"' /etc/os-release; \
    then \
      echo "deb http://archive.debian.org/debian stretch main\ndeb http://archive.debian.org/debian-security stretch/updates main" \
        > /etc/apt/sources.list; \
    fi

# Debian build essentials and Debcraft essentials
# Unfortunately this is almost 500 MB but here is no way around it. Luckily due
# to caching all rebuilds and all later containers will build much faster.
RUN apt-get update -q && \
    apt-get install -q --yes --no-install-recommends \
      blhc \
      ccache \
      curl \
      devscripts \
      eatmydata \
      equivs \
      fakeroot \
      git \
      git-buildpackage \
      lintian \
      pristine-tar

# Validator dependencies
RUN apt-get update -q && \
    apt-get install -q --yes --no-install-recommends \
      autopkgtest \
      apt-utils \
      command-not-found \
      hunspell-en-us \
      python3-debian \
      python3-hunspell \
      python3-levenshtein \
      shellcheck \
      xxd

# The diffoscope-minimal package is available only since Debian 11 "Bullseye",
# and thus building containers of older Debian or Ubuntu releases will fail
# but can be ignored, as only diffoscope-features are affected
RUN apt-get update -q && \
    apt-get install -q --yes --no-install-recommends \
      diffoscope-minimal || true

# @TODO: The dh-debputy and python3-lsprotocol are available only in latest
# Debian Sid 2024, and thus building containers of older Debian and Ubuntu
# releases will fail and thus an override to return a successful exit code is
# needed. This can be removed once dh-debputy with dependencies is available via
# backports: https://salsa.debian.org/debian/debputy/-/issues/76#note_512583
RUN apt-get update -q && \
    apt-get install -q --yes --no-install-recommends \
      dh-debputy \
      python3-lsprotocol || true

# Activate source repositories in the Containerfile so that the large download
# is cached in the container and the validator can run quickly
COPY enable-source-repositories.sh /
RUN /enable-source-repositories.sh

# Insert extra repositories and keys right before build dependencies are to be
# installed following convention set in Salsa CI
# (https://salsa.debian.org/salsa-ci-team/pipeline#add-private-repositories-to-the-builds)
COPY ci /
RUN if [ -f ci/extra_repository.list ]; then cp --archive --verbose ci/extra_repository.list /etc/apt/sources.list.d/; fi
RUN if [ -f ci/extra_repository.sources ]; then cp --archive --verbose ci/extra_repository.sources /etc/apt/sources.list.d/; fi
RUN if [ -f ci/extra_repository.asc ]; then cp --archive --verbose ci/extra_repository.asc /etc/apt/trusted.gpg.d/; fi

# Install dependencies
#@TODO: Use 'apt-get build-dep .' as all modern Debian/Ubuntu versions support it
COPY control /
RUN apt-get update -q && \
    DEBIAN_FRONTEND=noninteractive mk-build-deps -r -i /control \
    -t 'apt-get -y -o Debug::pkgProblemResolver=yes --no-install-recommends'

# Should be automatic and not needed at all, but run one more time just to be sure
RUN update-ccache-symlinks

# Older ccache does not support '--verbose' but will print stats anyway, just
# followed by help section. Newer ccache 4.0+ (Ubuntu 22.04 "Focal", Debian 12
# "Bullseye") however require '--verbose' to show any cache hit stats at all.
RUN ccache --show-stats  --verbose || true

COPY *.sh /
