#jinja2: lstrip_blocks: True
{% if matrix_synapse_container_image_customizations_templates_enabled %}
FROM {{ matrix_synapse_docker_image }} AS templates-builder

{#
This ugly script below does quite a lot:
	- installs git and other dependencies temporarily, just so we could do a shallow-clone
	- prepare the SSH config: keyscanning (if enabled), private key (if enabled)
	- performs a git shallow clone with just the branch we need
	- makes sure the files are owned by the user that will actually run the container later
#}
{% set dependencies = ['git', 'ssh', 'openssh-client'] %}
{% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}
RUN echo '{{ matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key | b64encode }}' | base64 -d > /custom-templates-private-key
RUN chmod 400 /custom-templates-private-key
{% endif %}

RUN apt-get update && apt-get install --no-install-recommends -y {{ dependencies | join(' ') }}

{% if matrix_synapse_container_image_customizations_templates_git_repository_keyscan_enabled %}
RUN mkdir ~/.ssh
RUN chmod 700 ~/.ssh
RUN ssh-keyscan -t rsa {{ matrix_synapse_container_image_customizations_templates_git_repository_keyscan_hostname }} >> ~/.ssh/known_hosts
{% endif %}

RUN {% if matrix_synapse_container_image_customizations_templates_git_repository_ssh_private_key %}GIT_SSH_COMMAND='ssh -i /custom-templates-private-key'{% endif %} git \
	clone \
		--branch={{ matrix_synapse_container_image_customizations_templates_git_repository_branch }} \
		--depth=1 \
		--single-branch \
		--no-tags \
		{{ matrix_synapse_container_image_customizations_templates_git_repository_url }} \
		{{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}

RUN /bin/sh -c 'cd {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} && git rev-parse HEAD > git-revision.txt'
RUN rm -rf {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}/.git

RUN chown -R {{ matrix_synapse_uid }}:{{ matrix_synapse_gid }} {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}
{% endif %}

FROM {{ matrix_synapse_docker_image }}

{% if matrix_synapse_container_image_customizations_auto_accept_invite_installation_enabled %}
RUN pip install synapse-auto-accept-invite=={{ matrix_synapse_ext_synapse_auto_accept_invite_version }}
{% endif %}

{% if matrix_synapse_container_image_customizations_s3_storage_provider_installation_enabled %}
RUN pip install synapse-s3-storage-provider=={{ matrix_synapse_ext_synapse_s3_storage_provider_version }}
{% endif %}

{% if matrix_synapse_container_image_customizations_templates_enabled %}
COPY --from=templates-builder {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }} {{ matrix_synapse_container_image_customizations_templates_in_container_base_path }}
{% endif %}

{{ matrix_synapse_container_image_customizations_dockerfile_body_custom }}
