{#
SPDX-FileCopyrightText: 2021 Toni Spets
SPDX-FileCopyrightText: 2022 - 2025 Slavi Pantaleev
SPDX-FileCopyrightText: 2025 Suguru Hirahara

SPDX-License-Identifier: AGPL-3.0-or-later
#}

[Unit]
Description=a puppeting appservice bridge for Zulip
{% for service in matrix_zulip_bridge_systemd_required_services_list %}
Requires={{ service }}
After={{ service }}
{% endfor %}
{% for service in matrix_zulip_bridge_systemd_wanted_services_list %}
Wants={{ service }}
{% endfor %}
DefaultDependencies=no

[Service]
Type=simple
Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}"
ExecStartPre=-{{ devture_systemd_docker_base_host_command_docker }} stop -t {{ matrix_zulip_bridge_container_stop_grace_time_seconds }} matrix-bridge-zulip
ExecStartPre=-{{ devture_systemd_docker_base_host_command_docker }} rm matrix-bridge-zulip

ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
			--rm \
			--name=matrix-bridge-zulip \
			--log-driver=none \
			--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
			--cap-drop=ALL \
			--network={{ matrix_zulip_bridge_container_network }} \
			--mount type=bind,src={{ matrix_zulip_bridge_base_path }},dst=/config \
			--label-file={{ matrix_zulip_bridge_base_path }}/labels \
			{% for arg in matrix_zulip_bridge_container_extra_arguments %}
			{{ arg }} \
			{% endfor %}
			{{ matrix_zulip_bridge_docker_image }} \
			{% if matrix_zulip_bridge_owner %}
			-o {{ matrix_zulip_bridge_owner }} \
			{% endif %}
			--config /config/registration.yaml \
			--listen-address 0.0.0.0 \
			--listen-port {{ matrix_zulip_bridge_container_http_port }} \
			{{ matrix_zulip_bridge_homeserver_url }}

{% for network in matrix_zulip_bridge_container_additional_networks %}
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-bridge-zulip
{% endfor %}

ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-bridge-zulip

ExecStop=-{{ devture_systemd_docker_base_host_command_docker }} stop -t {{ matrix_zulip_bridge_container_stop_grace_time_seconds }} matrix-bridge-zulip
ExecStop=-{{ devture_systemd_docker_base_host_command_docker }} rm matrix-bridge-zulip
Restart=always
RestartSec=30
SyslogIdentifier=matrix-bridge-zulip

[Install]
WantedBy=multi-user.target
