ARG BASE_IMAGE=gitlab.catalyst.net.nz:4567/riskscape/riskscape/base
FROM ${BASE_IMAGE}

# mostly taken from https://github.com/keeganwitt/docker-gradle/
USER root

CMD ["gradle"]

RUN apt-get update \
    && apt-get -y install wget curl unzip git ruby ruby-redcarpet \
    && apt-get autoremove -yq \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    rm -rf /var/lib/apt/lists/*


ENV GRADLE_HOME /opt/gradle
ENV GRADLE_VERSION 8.14.3
ARG IMAGE_UID=1000
ARG IMAGE_GID=1000

ARG GRADLE_DOWNLOAD_SHA256=bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531
RUN set -o errexit -o nounset \
    && echo "Downloading Gradle" \
    && wget --no-verbose --output-document=gradle.zip "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" \
    \
    && echo "Checking download hash" \
    && echo "${GRADLE_DOWNLOAD_SHA256} *gradle.zip" | sha256sum --check - \
    \
    && echo "Installing Gradle" \
    && unzip gradle.zip \
    && rm gradle.zip \
    && mv "gradle-${GRADLE_VERSION}" "${GRADLE_HOME}/" \
    && ln --symbolic "${GRADLE_HOME}/bin/gradle" /usr/bin/gradle \
    \
    && echo "Adding gradle user and group" \
    && groupadd --system --gid ${IMAGE_GID} gradle \
    && useradd --system --gid gradle --uid ${IMAGE_UID} --shell /bin/bash --create-home gradle \
    && mkdir /home/gradle/.gradle \
    && mkdir /home/gradle/project-cache \
    && chown --recursive gradle:gradle /home/gradle/ \
    \
    && echo "Symlinking root Gradle cache to gradle Gradle cache" \
    && ln -s /home/gradle/.gradle /root/.gradle

# Create Gradle volume
USER gradle
VOLUME "/home/gradle/.gradle"
VOLUME "/home/gradle/project-cache"
WORKDIR /home/gradle/project

RUN set -o errexit -o nounset \
    && echo "Testing Gradle installation" \
    && gradle --version

ENTRYPOINT gradle --project-cache-dir /home/gradle/project-cache $@
