---
- name: Make sure retrace user exists
  user:
    name: retrace
    group: retrace
    system: yes
    create_home: no
  register: retrace_user_info

- name: Ensure retrace user has correct UID and home directory
  when: >
    retrace_user_info.changed or
    retrace_user_info.uid != retrace_user_uid_gid or
    retrace_user_info.home != retrace_user_home
  block:
  - name: Stop httpd service before modifying user
    service:
      name: httpd
      state: stopped

  # Create UID with our reserved UID. This is normaly done during the
  # installation of the retrace-server package but it's not yet installed
  # at this momement.
  - name: Ensure correct UID and home directory
    user:
      name: retrace
      uid: "{{ retrace_user_uid_gid }}"
      home: "{{ retrace_user_home }}"
      create_home: yes

  # NOTE: We do not attempt to change the GID here if it's incorrect
  # since groups are not managed locally (via /etc/group) on these servers.
  # TODO: Find the correct way to do this and if it's even necessary.
  # - name: Ensure correct GID
  #   group:
  #     name: retrace
  #     gid: "{{ retrace_user_uid_gid }}"
  #     system: yes
  #     state: present

  - name: Restart httpd service after modifications are done
    service:
      name: httpd
      state: started

- name: Create directory for repositories
  file:
    path: /srv/retrace/repos
    state: directory
    mode: 0755
    owner: retrace
    group: retrace

- name: Create directory for retrace tasks
  file:
    path: /srv/retrace/tasks
    state: directory
    mode: 0755
    owner: retrace
    group: retrace

- file:
    path: /srv/retrace/hardlink-local
    state: directory
    mode: 0755
    owner: retrace
    group: retrace
