---
# 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: yes
    home: "{{gitit_data_dir}}"
    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:
#    src: "{{gitit_wiki_repo}}"
#    dest: "{{gitit_data_dir}}/wikidata"

# TODO restore user data

- 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