- name: Install ejabberd packages
  package: name={{ item }}
           state=present
  with_items:
   - ejabberd-2.1.11
  when: not is_debuntu

# need to use lineinfile and better regexp
- name: Disable updating ejabberd on CentOS
  shell: sed -i -e '/^enabled=/a exclude=ejabberd' {{ item }}
  with_items:
    - /etc/yum.repos.d/CentOS-Base.repo
    - /etc/yum.repos.d/CentOS-CR.repo
    - /etc/yum.repos.d/CentOS-fasttrack.repo
    - /etc/yum.repos.d/CentOS-Vault.repo
  when: ejabberd_xs_install and is_centos

- name: Disable updating ejabberd on Fedora
  shell: sed -i -e '/^enabled=/a exclude=ejabberd' {{ item }}
  with_items:
    - /etc/yum.repos.d/fedora.repo
    - /etc/yum.repos.d/fedora-updates.repo
    - /etc/yum.repos.d/fedora-updates-testing.repo
  when: ejabberd_xs_install and ansible_distribution == "Fedora"

- name: Configure ejabberd
  template: backup=yes
            src={{ item.src }}
            dest={{ item.dest }}
            owner=root
            group=root
            mode={{ item.mode }}
  with_items:
    - { src: 'ejabberd-xs.cfg.j2', dest: '/etc/ejabberd/ejabberd-xs.cfg' , mode: '0644' }
    - { src: 'ejabberdctl.cfg.j2', dest: '/etc/ejabberd/ejabberdctl-xs.cfg', mode: '0644' }
    - { src: 'ejabberd-xs', dest: '/etc/sysconfig/ejabberd-xs', mode: '0755' }
#    - { src: 'ejabberd-domain-config', dest: '/etc/sysconfig/olpc-scripts/domain_config.d/ejabberd', mode: '0755'}
#    - { src: 'ejabberd', dest: '/etc/sysconfig/olpc-scripts/domain_config.d/ejabberd' , mode: '0755' }
    - { src: 'ejabberd-xs.service.j2', dest: '/etc/systemd/system/ejabberd-xs.service', mode: '0755' }
    - { src: 'xs-ejabberd-srg', dest: '/usr/bin/xs-ejabberd-srg' , mode: '0755' }
    - { src: '10-ejabberdmoodle', dest: '/etc/sudoers.d/10-ejabberdmoodle', mode: '0440' }
    - { src: 'ejabberd.tmpfiles', dest: '/etc/tmpfiles.d/ejabberd.conf', mode: '0640' }
  register: ejabberd_config
  when: not is_debuntu

- name: Put the startup script in place - non debian
  template: src='ejabberd-xs.init'
            dest='/usr/libexec/ejabberd-xs'
  when: not is_debuntu

- name: Remove ejabberd_domain if domain changes
  file: path=/etc/sysconfig/ejabberd_domain_name
        state=absent
  when: ejabberd_config.changed and ejabberd_config is defined and not is_debuntu

- name: Enable ejabberd service
  file: src=/etc/systemd/system/ejabberd-xs.service
        dest=/etc/systemd/system/multi-user.target.wants/ejabberd-xs.service
        owner=root
        group=root
        state=link
  when: not is_debuntu and ejabberd_xs_enabled

- name: Start ejabberd service
  service: name=ejabberd-xs
           state=restarted
           enabled=yes
  when: ejabberd_config.changed and ejabberd_xs_enabled and not is_debuntu

- name: Wait for ejabberd service start
  wait_for: port=5280
            delay=15
            state=started
            timeout=300
  when: ejabberd_config.changed and ejabberd_xs_enabled

#- name: Create online group
#  shell: ejabberdctl srg_create Online "schoolserver" Online "Online_Users" Online
#  when: ejabberd_config.changed and not is_debuntu and ejabberd_xs_enabled

#- name: Add all users to online group
#  shell: ejabberdctl srg_user_add '@online@' "schoolserver" Online "schoolserver"
#  when: ejabberd_config.changed and not is_debuntu and ejabberd_xs_enabled
