# syntax=docker/dockerfile:1-labs
FROM registry.git.amazingcat.net/cattr/core/wolfi-os-image/cattr-dev:latest AS builder

ARG SENTRY_DSN
ARG APP_VERSION
ARG APP_ENV=production
ARG REVERB_SCHEME=http
ARG REVERB_PORT=8080
ARG BACKEND_MODULES="cattr/gitlab_integration-module cattr/redmine_integration-module"
ENV IMAGE_VERSION=5.0.0
ENV APP_VERSION $APP_VERSION
ENV SENTRY_DSN $SENTRY_DSN
ENV APP_ENV $APP_ENV
ENV YARN_ENABLE_GLOBAL_CACHE=true
ENV REVERB_APP_KEY="cattr"
ENV REVERB_APP_SECRET="secret"
ENV REVERB_HOST="127.0.0.1"
ENV REVERB_SCHEME $REVERB_SCHEME
ENV REVERB_PORT $REVERB_PORT

ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=300000

COPY --chown=root:root .root-fs/etc/php82 /etc/php82

WORKDIR /app

COPY --chown=www:www . /app

USER www:www

RUN set -x && \
    mkdir -p /tmp/composer && \
    : > /tmp/gitconfig && \
    unset GITHUB_TOKEN GH_TOKEN COMPOSER_AUTH COMPOSER_TOKEN COMPOSER_PREFER_SOURCE GIT_HTTP_EXTRAHEADER GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0 GIT_CONFIG_KEY_1 GIT_CONFIG_VALUE_1 GIT_CONFIG_KEY_2 GIT_CONFIG_VALUE_2 && \
    export \
      HOME=/tmp \
      XDG_CONFIG_HOME=/tmp \
      COMPOSER_HOME=/tmp/composer \
      COMPOSER_AUTH='{}' \
      COMPOSER_PREFER_DIST=1 \
      GIT_CONFIG_GLOBAL=/tmp/gitconfig \
      GIT_CONFIG_SYSTEM=/tmp/gitconfig \
      GIT_CONFIG_NOSYSTEM=1 \
      GIT_CONFIG_PARAMETERS= \
      GIT_TERMINAL_PROMPT=0 \
      GIT_ASKPASS=/bin/false \
      SSH_ASKPASS=/bin/false \
      GIT_CONFIG_COUNT=3 \
      GIT_CONFIG_KEY_0=credential.helper \
      GIT_CONFIG_VALUE_0= \
      GIT_CONFIG_KEY_1=http.useNetrc \
      GIT_CONFIG_VALUE_1=false \
      GIT_CONFIG_KEY_2=http.https://github.com/.extraheader \
      GIT_CONFIG_VALUE_2= && \
    php /usr/bin/composer.phar require -n --no-ansi --no-install --no-update --no-audit --prefer-dist $BACKEND_MODULES && \
    php /usr/bin/composer.phar update -n --no-autoloader --no-install --no-ansi --prefer-dist $BACKEND_MODULES && \
    php /usr/bin/composer.phar install -n --no-dev --no-cache --no-ansi --no-autoloader --no-dev --prefer-dist && \
    php /usr/bin/composer.phar dump-autoload -n --optimize --apcu --classmap-authoritative

RUN set -x && \
    yarn && \
    yarn prod && \
    rm -rf node_modules

RUN set -x && \
    php artisan storage:link

FROM registry.git.amazingcat.net/cattr/core/wolfi-os-image/cattr:latest AS runtime

ARG SENTRY_DSN
ARG APP_VERSION
ARG APP_ENV=production
ARG APP_KEY="base64:PU/8YRKoMdsPiuzqTpFDpFX1H8Af74nmCQNFwnHPFwY="
ARG REVERB_APP_SECRET="secret"
ARG REVERB_SCHEME=http
ARG REVERB_PORT=8080
ENV IMAGE_VERSION=5.0.0
ENV REVERB_APP_KEY="cattr"
ENV REVERB_HOST="127.0.0.1"
ENV REVERB_SCHEME $REVERB_SCHEME
ENV REVERB_PORT $REVERB_PORT
ENV REVERB_APP_SECRET $REVERB_APP_SECRET
ENV DB_CONNECTION=mysql
ENV DB_HOST=db
ENV DB_USERNAME=root
ENV DB_PASSWORD=password
ENV LOG_CHANNEL=stderr
ENV APP_VERSION $APP_VERSION
ENV SENTRY_DSN $SENTRY_DSN
ENV APP_ENV $APP_ENV
ENV APP_KEY $APP_KEY
ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=300000

COPY --from=builder /app /app

COPY --chown=root:root .root-fs /

VOLUME /app/storage

#HEALTHCHECK --interval=5m --timeout=10s \
#  CMD wget --spider -q "http://127.0.0.1:8090/status"

EXPOSE 80
