---
## Install Guide here: https://help.hcltechsw.com/sametime/12/admin/installing.html
-
  name: "Creating installation directories for Sametime"
  ansible.builtin.file:
    mode: '0644'
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ installer_dir }}/sametime/archives"
    - "{{ installer_dir }}/sametime/Sametime"

-
  name: "Checking if Sametime installer is at sametime/archives/{{ sametime_archive }}"
  register: sametime_installer_check
  ansible.builtin.stat:
    path: "{{ installer_dir }}/sametime/archives/{{ sametime_archive }}"
    get_md5: false

-
  name: "Checking if Sametime is installed: {{ sametime_version }}"
  register: sametime_installed_check
  ansible.builtin.stat:
    path: "{{ completed_dir }}/sametime_installed"
    get_md5: false

-
  name: "Downloading Sametime from {{ domino_installer_base_url }}"
  register: sametimeresult
  until: "sametimeresult is not failed"
  retries: 3
  ansible.builtin.get_url:
    mode: '0755'
    url: "{{ domino_installer_base_url }}/Sametime/{{ sametime_version }}/{{ sametime_archive }}"
    dest: "{{ installer_dir }}/sametime/archives/{{ sametime_archive }}"
    username: "{{ domino_installer_url_user }}"
    password: "{{ secrets.domino_installer_url_pass }}"
  when: not sametime_installer_check.stat.exists and not sametime_installed_check.stat.exists

-
  name: "Extracting Sametime from {{ sametime_archive }}"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.unarchive:
    mode: "a+x"
    owner: "{{ domino_user }}"
    group: "{{ domino_group }}"
    src: "{{ installer_dir }}/sametime/archives/{{ sametime_archive }}"
    dest: "{{ installer_dir }}/sametime/Sametime"
    creates: "{{ installer_dir }}/sametime/Sametime/Sametime-{{ sametime_version }}-for-domino-1201-linux"
    remote_src: true

-
  name: "Stopping Domino for Sametime Installation"
  when: not sametime_installed_check.stat.exists
  become: true
  ansible.builtin.service:
    name: domino
    state: stopped
    enabled: true

-
  name: "Configuring Sametime and Starting Service"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.shell: "{{ item }}"
  become: true
  args:
    executable: "/bin/bash"
    chdir: "{{ installer_dir }}/sametime/Sametime/Sametime-{{ sametime_version }}/linux"
  with_items:
    - ./install silent
    - touch {{ completed_dir }}/sametime_install

-
  name: "Registering sametime installation Output"
  ansible.builtin.shell: cat /var/log/volt_install_*.log
  register: sametime_install
  changed_when: false

-
  name: "Outputting Sametime installation logs"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.debug:
    var: sametime_install.stdout_lines

-
  name: "Creating Database chatlogging for Sametime"
  when: not sametime_installed_check.stat.exists
  community.mongodb.mongodb_user:
    login_user: "{{ mongodb_admin_user }}"
    login_password: "{{ mongodb_admin_password }}"
    login_port: "27017"
    database: admin
    user: sametimeUser
    password: sametime
    state: present
    roles:
      - db: chatlogging
        role: readWrite
      - db: mobileOffline
        role: readWrite
      - db: meeting
        role: readWrite
      - db: meeting
        role: dbAdmin
      - db: admin
        role: userAdminAnyDatabase

-
  name: "Initializing Databases"
  when: not sametime_installed_check.stat.exists
  community.mongodb.mongodb_index:
    login_user: "{{ mongodb_admin_user }}"
    login_password: "{{ mongodb_admin_password }}"
    login_port: "27017"
    indexes:
      - database: chatlogging
        collection: "EVENTS"
        keys:
          _id: 1
        options:
          name: "dummy"
        state: present
      - database: chatlogging
        collection: "SESSIONS"
        options:
          name: "dummy"
        keys:
          _id: 1
        state: present

# -
#   name: "Initializing Databases"
#   community.mongodb.mongodb_shell:
#     mongo_cmd: mongosh
#     db: chatlogging
#     login_user: "{{ mongodb_admin_user }}"
#     login_password: "{{ mongodb_admin_password }}"
#     login_port: "27017"
#     eval: "{{ item }}"
#   with_items:
#     - 'db.EVENTS.insertOne({"_id" : "dummy"})'
#     - 'db.SESSIONS.insertOne({"_id" : "dummy"})'

-

  name: "Stopping MongoDB"
  when: not sametime_installed_check.stat.exists
  become: true
  ansible.builtin.service:
    name: mongod
    state: stopped
    enabled: true

-
  name: "Binding to all interfaces and Setting Replication set in MongoDB"
  ansible.builtin.lineinfile:
    mode: '0644'
    path: "{{ item.dir }}"
    regexp: "{{ item.regexp }}"
    insertafter: "{{ item.insertafter }}"
    create: true
    line: "{{ item.line }}"
  with_items:
    - { regexp: "^#replication:", insertafter: "", line: 'replication:', dir: "/etc/mongod.conf" }
    - { regexp: "  replSetName: rs0", insertafter: "replication:", line: '  replSetName: rs0', dir: "/etc/mongod.conf" }
    - { regexp: "  bindIpAll: true", insertafter: "bindIp: 127.0.0.1", line: '  bindIpAll: true', dir: "/etc/mongod.conf" }

-

  name: "Starting MongoDB"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.service:
    name: mongod
    state: started
    enabled: true

-
  name: "Initializing Databases"
  when: not sametime_installed_check.stat.exists
  community.mongodb.mongodb_shell:
    mongo_cmd: mongosh
    db: admin
    login_user: "{{ mongodb_admin_user }}"
    login_password: "{{ mongodb_admin_password }}"
    login_port: "27017"
    eval: "{{ item }}"
  with_items:
    - 'rs.initiate()'

-
  name: "Ensuring replicaset rs0 exists"
  when: not sametime_installed_check.stat.exists
  community.mongodb.mongodb_replicaset:
    login_user: "{{ mongodb_admin_user }}"
    login_password: "{{ mongodb_admin_password }}"
    login_port: "27017"
    replica_set: rs0
    members: localhost:27017
    validate: false

-
  name: "Starting Domino"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.service:
    name: domino
    state: started
    enabled: true

-
  name: "Setting Sametime to installed"
  when: not sametime_installed_check.stat.exists
  ansible.builtin.file:
    mode: '0644'
    path: "{{ item }}"
    state: touch
  with_items:
    - "{{ completed_dir }}/sametime_installed"
