---
- name: Create filebrowser user
  ansible.builtin.user:
    name: arkiv
    password: "!"
    home: "/home/arkiv"
    create_home: true
    system: true
    shell: /bin/bash
    comment: "arkiv system user"
  become: true

- name: Create binary directory
  ansible.builtin.file:
    path: /opt/filebrowser
    state: directory
    owner: arkiv
    mode: "0700"
  become: true
  notify: Restart filebrowser
- name: Download program
  ansible.builtin.get_url:
    url: "{{ arkivet_download_url }}"
    dest: /tmp/filebrowser.tar.gz
    owner: ansible
    mode: "0700"

- name: Untar
  ansible.builtin.unarchive:
    src: /tmp/filebrowser.tar.gz
    dest: /opt/filebrowser
    mode: "0700"
    owner: arkiv
    remote_src: true
  become: true
  notify: Restart filebrowser

- name: Create mount directory
  ansible.builtin.file:
    state: directory
    owner: arkiv
    path: /mnt/arkivet
    mode: "0777"
  become: true

- name: Mount unraid server share
  ansible.posix.mount:
    src: //192.168.1.100/arkivet
    path: /mnt/arkivet
    opts: "rw,vers=3,noserverino,file_mode=0666,dir_mode=0777,username={{ arkivet_mount_user }},password={{ arkivet_mount_password }}"
    fstype: cifs
    boot: true
    state: mounted
  become: true
  notify: Restart filebrowser

- name: Template config
  ansible.builtin.template:
    src: config.yml.j2
    dest: /opt/filebrowser/config.yml
    owner: arkiv
    mode: "0600"
  become: true
  notify: Restart filebrowser

- name: Copy systemd service
  ansible.builtin.copy:
    src: filebrowser.service
    dest: /etc/systemd/system/filebrowser.service
    owner: root
    mode: "0644"
  become: true
  notify: Restart filebrowser

- name: Ensure service started
  ansible.builtin.systemd:
    name: filebrowser.service
    state: started
    enabled: true
    daemon_reload: true
  become: true

- name: Copy nginx config
  ansible.builtin.copy:
    src: filebrowser.conf
    dest: /etc/nginx/sites-enabled/filebrowser.conf
    owner: root
    mode: "0644"
  become: true
  notify: Reload nginx
