---

- name: Create download folder
  ansible.builtin.file:
    path: "/tmp/filebrowser"
    state: directory
    mode: "0755"

- name: Download Filebrowser binary from github
  ansible.builtin.unarchive:
    src: "https://github.com/filebrowser/filebrowser/releases/download/v{{ filebrowser_version }}/linux-amd64-filebrowser.tar.gz"
    dest: "/tmp/filebrowser"
    remote_src: true

- name: Moving filebrowser binary to /usr/local/bin
  ansible.builtin.copy:
    src: /tmp/filebrowser/filebrowser
    dest: /usr/local/bin/filebrowser
    owner: root
    group: root
    mode: "0755"
    remote_src: true

- name: Create configuration folder
  ansible.builtin.file:
    path: "{{ filebrowser_config_dir }}"
    owner: "{{ filebrowser_user }}"
    group: "{{ filebrowser_group }}"
    mode: "0755"
    state: directory

- name: Deploying configuration file
  ansible.builtin.template:
    src: "settings.json.j2"
    dest: "{{ filebrowser_config_dir }}/settings.json"
    owner: "{{ filebrowser_user }}"
    group: "{{ filebrowser_group }}"
    mode: "0644"
  notify: Restart

- name: Deploying service file
  ansible.builtin.template:
    src: "filebrowser.service.j2"
    dest: "/lib/systemd/system/filebrowser.service"
    owner: "root"
    group: "root"
    mode: "0644"
  notify:
    - Daemon reload

- name: Running handlers for daemon reload
  ansible.builtin.meta: flush_handlers

- name: Enabling filebrowser service
  ansible.builtin.systemd:
    name: filebrowser.service
    state: started
    enabled: true

- name: Create log folder
  ansible.builtin.file:
    path: "{{ filebrowser_log_dir }}"
    owner: root
    group: adm
    mode: "0755"
    state: directory

- name: Deploy rsyslog config
  ansible.builtin.copy:
    src: rsyslog.conf
    dest: /etc/rsyslog.d/filebrowser.conf
    owner: root
    group: root
    mode: "0644"

- name: Deploy logrotate config
  ansible.builtin.copy:
    src: logrotate.conf
    dest: /etc/logrotate.d/filebrowser
    owner: root
    group: root
    mode: "0644"

- name: Create custom branding folder
  ansible.builtin.file:
    path: "/etc/filebrowser/branding/img/icons"
    owner: "{{ filebrowser_user }}"
    group: "{{ filebrowser_group }}"
    mode: "0755"
    state: directory
