---
# Include variables and define needed variables.
- name: Include OS-specific variables.
  include_vars: "{{ ansible_os_family }}.yml"

- name: Ensure unzip is installed (RedHat).
  yum: pkg=unzip state=installed
  when: ansible_os_family == 'RedHat'

- name: Ensure unzip is installed (Debian).
  apt: pkg=unzip state=installed
  when: ansible_os_family == 'Debian'

- name: Create user for Gogs.
  user:
    name: "{{ gogs_user }}"
    comment: Gogs
    home: "{{ gogs_user_home }}"

- name: Check if Gogs is already installed.
  stat: path=/usr/local/bin/gogs
  register: gogs_bin

- name: Download Gogs.
  get_url:
    url: "{{ gogs_binary_url }}"
    dest: "{{ gogs_user_home }}/gogs.zip"
    owner: "{{ gogs_user }}"
    group: "{{ gogs_user }}"
  when: gogs_bin.stat.islnk is not defined

- name: Expand Gogs.
  unarchive:
    src: "{{ gogs_user_home }}/gogs.zip"
    dest: "{{ gogs_user_home }}"
    group: "{{ gogs_user }}"
    owner: "{{ gogs_user }}"
    copy: no
  when: gogs_bin.stat.islnk is not defined

- include: init-setup.yml
- include: gogs-mysql.yml

- name: Create Gogs log folder.
  file:
    path: "{{ gogs_user_home }}/gogs/log"
    state: directory
    owner: "{{ gogs_user }}"
    group: "{{ gogs_user }}"
    mode: 0755

- name: Ensure Gogs is running.
  service: name=gogs state=started enabled=yes