---
# tasks file for ffdo.gitit

- name: Ensure group for gitit exists
  group: name={{gitit_group}}

- name: Ensure user for gitit exists
  user: 
    name: "{{ gitit_user }}"
    createhome: no
    home: "{{ gitit_data_dir }}"
    group: "{{ gitit_group }}"

- name: Ensure gitit ssh dir exists
  file:
    dest: "{{ gitit_data_dir }}/.ssh"
    state: directory
    mode: 0700
    owner: "{{ gitit_user }}"
    group: "{{ gitit_group }}"

- name: Ensure gitit ssh key is up to date
  copy:
    content: "{{ wiki_git_private_key }}"
    dest: "{{ gitit_data_dir }}/.ssh/id_rsa"
    mode: 0600
    owner: "{{ gitit_user }}"
    group: "{{ gitit_group }}"

- name: Ensure gitconfig exists
  template:
    src: gitconfig.j2
    dest: "{{ gitit_data_dir }}/.gitconfig"
    owner: "{{ gitit_user }}"
    group: "{{ gitit_group }}"

- name: Ensure prerequisites for gitit are instaled
  apt: name={{ item }} state=present update_cache=yes cache_valid_time=3600
  with_items:
  - git
  - gitit

- name: Install gitit configuration
  template:
    src: conf.j2
    dest: "{{ gitit_data_dir }}/gitit.conf"
    owner: "{{ gitit_user }}"
  notify:
  - Restart gitit

- name: Ensure static content directories exists
  file:
    dest: "{{ gitit_data_dir }}/static/{{item}}"
    state: directory
    owner: "{{ gitit_user }}"
  with_items:
  - css
  - img

- name: Ensure template directory exists
  file:
    dest: "{{ gitit_data_dir }}/templates"
    state: directory
    owner: "{{ gitit_user }}"

- name: Ensure custom footer exists
  template:
    src: footer.st.j2
    dest: "{{ gitit_data_dir }}/templates/footer.st"
    owner: "{{ gitit_user }}"

- name: Ensure custom css exists
  template:
    src: custom.css.j2
    dest: "{{ gitit_data_dir }}/static/css/custom.css"
    owner: "{{ gitit_user }}"

- name: Ensure wikidata directory exists
  file:
    dest: "{{ gitit_data_dir }}/wikidata"
    state: directory
    owner: "{{ gitit_user }}"

- name: Clone wiki repo
  git:
    repo: "{{ gitit_wiki_repo }}"
    dest: "{{ gitit_data_dir }}/wikidata"
    key_file: "{{ gitit_data_dir }}/.ssh/id_rsa"
    accept_hostkey: yes
    umask: "002"

- name: Ensure git hooks are in place
  copy:
    src: "{{ item }}"
    dest: "{{ gitit_data_dir }}/wikidata/.git/hooks/{{ item }}"
    owner: "{{ gitit_user }}"
    mode: 0776
  with_items:
    - post-commit
    - post-update

# TODO restore user data

- name: Let acmetool generate a key and a certificate
  become: yes
  shell: /usr/bin/acmetool want --batch {{ wiki_domain }}
  notify: Restart nginx

- name: Install nginx configs
  template:
    src: "nginx.j2"
    dest: "/etc/nginx/sites-available/wiki"
  notify:
  - Restart nginx

- name: Activate nginx configurations
  file:
    src: /etc/nginx/sites-available/wiki
    dest: /etc/nginx/sites-enabled/wiki
    state: link
  notify:
  - Restart nginx

- name: Install systemd unit
  register: gitit_systemd_unit
  template:
    src: systemd.j2
    dest: /etc/systemd/system/gitit.service

- name: Systemd daemon-reload
  when: gitit_systemd_unit|changed
  shell: systemctl daemon-reload

- name: Ensure gitit is started and enabled
  service: name=gitit state=started enabled=yes
