ARG ZAMMAD_VERSION=6.0.0-32
FROM zammad/zammad-docker-compose:${ZAMMAD_VERSION} AS builder
RUN mkdir -p /opt/zammad/contrib/link/addons
COPY addons ${ZAMMAD_DIR}/contrib/link/addons
COPY setup.rb ${ZAMMAD_DIR}/contrib/link/setup.rb
COPY install.rb ${ZAMMAD_DIR}/contrib/link/install.rb
RUN sed -i '/proxy_set_header X-Forwarded-User "";/d' ${ZAMMAD_DIR}/contrib/nginx/zammad.conf;

USER root
RUN set -ex; \
  apt-get update; \
  apt-get install -y --no-install-recommends nodejs git libclang-dev clang llvm pkg-config nettle-dev;
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ARG SEQUOIA_PROJECT_URL=https://gitlab.com/sequoia-pgp/sequoia-ffi.git
ARG SEQUOIA_GIT_TAG=main
ENV SEQUOIA_DIR=/usr/lib/sequoia
ENV LD_LIBRARY_PATH=${SEQUOIA_DIR}/target/debug
RUN git clone -b "${SEQUOIA_GIT_TAG}" --single-branch --depth 1 "${SEQUOIA_PROJECT_URL}" "${SEQUOIA_DIR}";
WORKDIR ${SEQUOIA_DIR}
RUN export PATH=~/.cargo/bin:$PATH && cargo build -p sequoia-openpgp-ffi;

WORKDIR ${ZAMMAD_DIR}
RUN  echo "gem 'ruby_openpgp', git: 'https://github.com/throneless-tech/ruby_openpgp', branch: 'signing-and-userids'" >> Gemfile.local
RUN bundle install --without test development mysql
RUN sed -i '/^[[:space:]]*# create install ready file/ i\
  echo "about to reinstall..."\n\
  bundle exec rails runner /opt/zammad/contrib/link/setup.rb\n\
  bundle exec rake zammad:package:migrate\n\
  ' /docker-entrypoint.sh
USER zammad
RUN ZAMMAD_SAFE_MODE=1 bundle exec rails runner /opt/zammad/contrib/link/install.rb

FROM node:16.18.0-slim as node

FROM zammad/zammad-docker-compose:${ZAMMAD_VERSION}
USER ${ZAMMAD_USER}
ENV SEQUOIA_DIR=/usr/lib/sequoia
ENV LD_LIBRARY_PATH=${SEQUOIA_DIR}/target/debug
ENV ZAMMAD_READY_FILE=${ZAMMAD_DIR}/var/zammad.ready
COPY --from=node /opt /opt
COPY --from=node /usr/local/bin /usr/local/bin
COPY --from=builder ${ZAMMAD_DIR} ${ZAMMAD_DIR}
COPY --from=builder ${SEQUOIA_DIR} ${SEQUOIA_DIR}
COPY --from=builder /usr/local/bundle /usr/local/bundle
COPY --from=builder /docker-entrypoint.sh /docker-entrypoint.sh
RUN ZAMMAD_SAFE_MODE=1 bundle exec rake assets:precompile


