ARG FROM_IMG=docker.io/library/haskell
ARG FROM_TAG=8.10.4

FROM ${FROM_IMG}:${FROM_TAG}

ENV LANG=de_DE.UTF-8

# compile-time dependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get -y update && apt-get install -y libpq-dev libsodium-dev
# RUN apt-get -y update && apt-get -y install llvm
# RUN apt-get -y update && apt-get -y install g++ libghc-zlib-dev libpq-dev libsodium-dev pkg-config
# RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get -y update && apt-get install -y --no-install-recommends locales locales-all

# run-time dependencies for uniworx binary
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get -y update && apt-get -y install fonts-roboto
# RUN apt-get -y update && apt-get -y install pdftk
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get -y update && apt-get -y install texlive-latex-recommended texlive-luatex texlive-plain-generic texlive-lang-german texlive-lang-english

# locally these two should be identical, so that compilation results are written out into the file dir.
# in CI-pipelines these two should be different, so that the container caches the compilation results.
ARG MOUNT_DIR=/mnt/fradrive
ARG PROJECT_DIR=/fradrive

RUN mkdir -p "${PROJECT_DIR}"
# RUN if [ "${PROJECT_DIR}" != "${MOUNT_DIR}" ] ; then cp -r "${MOUNT_DIR}"/* "${PROJECT_DIR}" ; fi

RUN mkdir -p "${PROJECT_DIR}/.stack"
ENV STACK_ROOT="${PROJECT_DIR}/.stack"

WORKDIR ${PROJECT_DIR}
ENV HOME=${PROJECT_DIR}

COPY stack.yaml ${PROJECT_DIR}/stack.yaml
COPY stack.yaml.lock ${PROJECT_DIR}/stack.yaml.lock
COPY package.yaml ${PROJECT_DIR}/package.yaml
RUN stack install yesod-bin
RUN stack install hpack

# pre-build dependencies
RUN make -- --dependencies-backend STACK_ROOT=${STACK_ROOT} IN_CONTAINER=true JOB=${JOB}
COPY . ${PROJECT_DIR}

# copy source files
#COPY app/ ${PROJECT_DIR}/app
#COPY src/ ${PROJECT_DIR}/src
#COPY models/ ${PROJECT_DIR}/models
#COPY messages/ ${PROJECT_DIR}/messages
#COPY templates/ ${PROJECT_DIR}/templates
#COPY test/ ${PROJECT_DIR}/test
#COPY testdata/ ${PROJECT_DIR}/testdata

# copy frontend files
#COPY assets/ ${PROJECT_DIR}/assets
#COPY static/ ${PROJECT_DIR}/static
#COPY well-known/ ${PROJECT_DIR}/well-known

ENV FRADRIVE_MAKE_TARGET=--start-backend
ENTRYPOINT make -- ${FRADRIVE_MAKE_TARGET} STACK_ROOT="${STACK_ROOT}" IN_CONTAINER=true CONTAINER_FILE="${CONTAINER_FILE}" JOB="${JOB}" SRC="${SRC}"

EXPOSE 3000/tcp
EXPOSE 3443/tcp