# we need to install the rpm in order to get the dependencies
# but we only need to do this the first time

- name: add a repo def for ubuntu
  template: dest=/etc/apt/sources.list.d/
            src=owncloud.list
  when: is_ubuntu

- name: See if the owncloud startup page exists
  stat: path={{ owncloud_prefix }}/owncloud/index.php
  register: owncloud_page

- name: Install owncloud package
  package: name={{ item }}
           state=present
  with_items:
      - curl
      - owncloud
  when: owncloud_page.stat.exists is defined and not owncloud_page.stat.exists

- name: Remove owncloud package
  package: name=owncloud
           state=absent
  when: owncloud_page.stat.exists is defined and not owncloud_page.stat.exists

- name: remove config files for package
  file: src=/etc/apache2/conf-available/owncloud.conf
        dest=/etc/apache2/conf-enabled/owncloud.conf
        state=absent

- name: remove config files for package
  file: path=/etc/apache2/conf-available/owncloud.conf
        state=absent

#- name: Remove /etc/owncloud to avoid confusion as we use the config in {{ owncloud_prefix }}/config/
#  file: path=/etc/owncloud
#        state=absent

# but we use the tar file to get the latest version

- name: Get the owncloud software
  get_url: url={{ iiab_download_url }}/{{ owncloud_src_file }}  dest={{ downloads_dir }}/{{ owncloud_src_file }}
  when: internet_available
  async: 300
  poll: 5

- name: Copy it to permanent location /opt
  unarchive: src={{ downloads_dir }}/{{ owncloud_src_file }}
             dest={{ owncloud_prefix }}
             creates={{ owncloud_prefix }}/owncloud/version.php
  when: not is_F18

# ansible 1.4.1 does not have "creates"
- name: Copy it to permanent location /opt
  unarchive: src={{ downloads_dir }}/{{ owncloud_src_file }}
             dest={{ owncloud_prefix }}
  when: is_F18

- name: in Centos, the following config dir is symlink to /etc/owncloud
  file: path=/etc/owncloud
        state=directory

- name: Add autoconfig file
  template: src=autoconfig.php.j2
            dest={{ owncloud_prefix }}/owncloud/config/autoconfig.php
            owner={{ apache_user }}
            group=apache
            mode=0640

- name: Make apache owner
  file: path={{ owncloud_prefix }}/owncloud
        owner={{ apache_user }}
        group=apache
        recurse=yes
        state=directory

- name: Create data directory library
  file: path={{ item }}
        mode=0750
        owner={{ apache_user }}
        group=apache
        state=directory
  with_items:
    - "{{ owncloud_data_dir }}"

- name: Create a mysql database for owncloud
  mysql_db: name={{ owncloud_dbname }}
  when: mysql_enabled and owncloud_enabled

- name: Create a user to access the owncloud database
  mysql_user: name={{ owncloud_dbuser }} host={{ item }} password={{ owncloud_dbpassword }} priv={{ owncloud_dbname }}.*:ALL,GRANT
  with_items:
        - "{{ owncloud_dbhost }}"
        - 127.0.0.1
        - ::1
        - localhost
  when: mysql_enabled and owncloud_enabled

- name: Restart apache, so it picks up the new aliases
  service: name={{ apache_service }} state=restarted
  when: not owncloud_enabled

# Enable owncloud by copying template to httpd config

- include_tasks: owncloud_enabled.yml
  when: owncloud_enabled

- name: Add 'owncloud' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: owncloud
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: owncloud
    - option: description
      value: '"OwnCloud is a local server-based facility for sharing files, photos, contacts, calendars, etc."'
    - option: path
      value: "{{ owncloud_prefix }}/owncloud"
    - option: source
      value: "{{ owncloud_src_file }}"
    - option: enabled
      value: "{{ owncloud_enabled }}"
