---
- name: Include tasks for pre-setup checks
  ansible.builtin.include_tasks: preflight.yml

- name: Include tasks to install packages
  ansible.builtin.include_tasks: packages-{{ ansible_pkg_mgr }}.yml

- name: Create Caddy user
  ansible.builtin.user:
    name: "{{ caddy_user }}"
    system: true
    createhome: true
    home: "{{ caddy_home }}"
  register: caddy_user_details

- name: Build headers to use when making requests to github
  ansible.builtin.set_fact:
    caddy_github_headers: "{{ caddy_github_headers | combine({'Authorization': 'token ' + caddy_github_token}) }}"
  when: caddy_github_token | length > 0

- name: Get all Caddy releases
  ansible.builtin.get_url:
    url: https://api.github.com/repos/caddyserver/caddy/git/refs/tags
    dest: "{{ caddy_home }}/releases.txt"
    force: true
    headers: '{{ caddy_github_headers }}'
    owner: "{{ caddy_user }}"
    group: "{{ caddy_user_details.group }}"
    mode: 0640
  retries: 3
  delay: 2
  when: caddy_update
  register: caddy_releases_cache

- name: Set Caddy features
  ansible.builtin.copy:
    content: "{{ ','.join(caddy_packages) }}"
    dest: "{{ caddy_home }}/features.txt"
    mode: 0640
    owner: "{{ caddy_user }}"
    group: "{{ caddy_user_details.group }}"
  when: caddy_update
  register: caddy_features_cache

- name: Include tasks to build github URL if downloading from github
  ansible.builtin.include_tasks: github-url.yml
  when: caddy_use_github

- name: Download Caddy
  ansible.builtin.get_url:
    url: "{{ caddy_url }}"
    checksum: "{{ caddy_checksum_url | default(omit) }}"
    dest: "{{ caddy_home }}/{{ 'caddy.tar.gz' if caddy_use_github else 'caddy' }}"
    force: true
    timeout: "{{ caddy_download_timeout }}"
    mode: 0644
    owner: "{{ caddy_user }}"
    group: "{{ caddy_user_details.group }}"
  retries: 3
  delay: 2
  when: caddy_releases_cache.changed or caddy_features_cache.changed
  register: caddy_binary_cache
  tags: skip_ansible_lint

- name: Download Caddy
  ansible.builtin.get_url:
    url: "{{ caddy_url }}"
    checksum: "{{ caddy_checksum_url | default(omit) }}"
    dest: "{{ caddy_home }}/{{ 'caddy.tar.gz' if caddy_use_github else 'caddy' }}"
    timeout: "{{ caddy_download_timeout }}"
    mode: 0644
    owner: "{{ caddy_user }}"
    group: "{{ caddy_user_details.group }}"
  retries: 3
  delay: 2
  register: caddy_download
  tags: skip_ansible_lint

- name: Include tasks to extract files if downloading from github
  ansible.builtin.include_tasks: github-extract.yml
  when: caddy_use_github

- name: Copy Caddy Binary
  ansible.builtin.copy:
    src: "{{ caddy_home }}/caddy"
    dest: "{{ caddy_bin }}"
    mode: 0755
    remote_src: true
  notify:
    - Restart caddy

- name: Create directories
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    owner: "{{ caddy_user }}"
    mode: 0770
  with_items:
    - "{{ caddy_conf_dir }}"
    - "{{ caddy_certs_dir }}"

- name: Create log directory
  ansible.builtin.file:
    path: "{{ caddy_log_dir }}"
    state: directory
    owner: "{{ caddy_user }}"
    mode: 0775

- name: Get information about Caddy config file
  ansible.builtin.stat:
    path: "{{ caddy_conf_dir }}/{{ caddy_conf_filename }}"
  register: caddy_config_stat

- name: Create caddy config
  ansible.builtin.copy:
    content: "{{ caddy_config }}"
    dest: "{{ caddy_conf_dir }}/{{ caddy_conf_filename }}"
    owner: "{{ caddy_user }}"
    mode: 0640
  when:
    - caddy_config_update or not caddy_config_stat.stat.exists
  notify:
    - Reload caddy

- name: Template systemd service
  ansible.builtin.template:
    src: caddy.service
    dest: "/etc/systemd/system/{{ caddy_service_name }}"
    owner: root
    group: root
    mode: 0644
  notify:
    - Restart caddy

- name: Set capability on the binary file to be able to bind to TCP port <1024
  community.general.capabilities:
    path: "{{ caddy_bin }}"
    capability: cap_net_bind_service+eip
    state: present
  when: caddy_setcap

- name: Ensure caddy service is up-to-date before starting it
  ansible.builtin.meta: flush_handlers

- name: Start Caddy service
  ansible.builtin.systemd:
    name: caddy
    state: started
    enabled: true
