---

- name: Install dependencies
  package: name={{ squashtm_packages }}
  tags: squashtm

- name: Detect exact JRE version
  block:
    - command: rpm -q java-11-openjdk
      changed_when: False
      register: squashtm_jre11_version
    - set_fact: squashtm_jre11_version={{ squashtm_jre11_version.stdout | trim }}
  tags: squashtm

- when: squashtm_install_mode != 'none'
  block:

    - name: Download Squash TM
      get_url:
        url: "{{ squashtm_archive_url }}"
        dest: "{{ squashtm_root_dir }}/tmp/"
        checksum: sha256:{{ squashtm_archive_sha256 }}

    - name: Extract Squash TM archive
      unarchive:
        src: "{{ squashtm_root_dir }}/tmp/squash-tm-{{ squashtm_version }}.RELEASE.tar.gz"
        dest: "{{ squashtm_root_dir }}/tmp/"
        remote_src: True

    - name: Move Squash TM to the app dir
      synchronize:
        src: "{{ squashtm_root_dir }}/tmp/squash-tm/"
        dest: "{{ squashtm_root_dir }}/app/"
        delete: True
        compress: False
      delegate_to: "{{ inventory_hostname }}"

  tags: squashtm

- name: Install the permission script
  template: src=perms.sh.j2 dest={{ squashtm_root_dir }}/perms.sh mode=755
  register: squashtm_perm_script
  tags: squashtm

- name: Fix permissions
  command: "{{ squashtm_root_dir }}/perms.sh"
  when: squashtm_install_mode != none or squashtm_perm_script.changed
  tags: squashtm

- name: Install backup hooks
  template: src={{ item }}-backup.j2 dest=/etc/backup/{{ item }}.d/squashtm owner=root group=root mode=700
  loop:
    - pre
    - post
  tags: squashtm

- name: Install service unit
  template: src=squash-tm.service.j2 dest=/etc/systemd/system/squash-tm.service
  notify: restart squash-tm
  register: squashtm_unit
  tags: squashtm

- name: Reload systemd
  systemd: daemon_reload=True
  when: squashtm_unit.changed
  tags: squashtm

- when: squashtm_db_engine == 'postgres'
  block:
    - name: Create the PostgreSQL role
      postgresql_user:
        db: postgres
        name: "{{ squashtm_db_user }}"
        password: "{{ squashtm_db_pass }}"
        login_host: "{{ squashtm_db_server }}"
        login_port: "{{ squashtm_db_port }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

    - name: Create the PostgreSQL database
      postgresql_db:
        name: "{{ squashtm_db_name }}"
        encoding: UTF-8
        template: template0
        owner: "{{ squashtm_db_user }}"
        login_host: "{{ squashtm_db_server }}"
        login_port: "{{ squashtm_db_port }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

  tags: squashtm

- when: squashtm_db_engine == 'mysql'
  import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ squashtm_db_name }}"
    - db_user: "{{ squashtm_db_user }}"
    - db_server: "{{ squashtm_db_server }}"
    - db_port: "{{ squashtm_db_port }}"
    - db_pass: "{{ squashtm_db_pass }}"
  tags: squashtm

- name: Import initial DB structure (postgresql)
  postgresql_db:
    db: "{{ squashtm_db_name }}"
    state: restore
    target: "{{ squashtm_root_dir }}/app/database-scripts/postgresql-full-install-version-{{ squashtm_version }}.RELEASE.sql"
    login_host: "{{ squashtm_db_server }}"
    login_port: "{{ squashtm_db_port }}"
    login_user: "{{ squashtm_db_user }}"
    login_password: "{{ squashtm_db_pass }}"
  when: squashtm_install_mode == 'install' and squashtm_db_engine == 'postgres'
  tags: squashtm

- name: Import the initial DB structure (mysql)
  mysql_db:
    db: "{{ squashtm_db_name }}"
    state: import
    target: "{{ squashtm_root_dir }}/app/database-scripts/mysql-full-install-version-{{ squashtm_version }}.RELEASE.sql"
    login_host: "{{ squashtm_db_server }}"
    login_port: "{{ squashtm_db_port }}"
    login_user: "{{ squashtm_db_user }}"
    login_password: "{{ squashtm_db_pass }}"
  when: squashtm_install_mode == 'install' and squashtm_db_engine == 'mysql'
  tags: squashtm

- name: List database upgrade scripts
  shell: >
    ls /opt/squash_tm/app/database-scripts/{{ (squashtm_db_engine == 'postgres') | ternary('postgresql', 'mysql') }}-* |
      grep 'upgrade-to' |
      perl -pe 's/.*upgrade\-to\-(\d+(\.\d+)+).*/$1/' |
      sort --version-sort
  register: squashtm_db_upgrade_scripts
  changed_when: False
  tags: squashtm

- name: Upgrade the database
  mysql_db:
    db: "{{ squashtm_db_name }}"
    state: import
    target: "{{ squashtm_root_dir }}/app/database-scripts/{{ (squashtm_db_engine == 'postgres') | ternary('postgresql', 'mysql') }}-upgrade-to-{{ item }}.sql"
    login_host: "{{ squashtm_db_server }}"
    login_port: "{{ squashtm_db_port }}"
    login_user: "{{ squashtm_db_user }}"
    login_password: "{{ squashtm_db_pass }}"
  when:
    - squashtm_install_mode == 'upgrade'             # Only apply scripts when upgrading Squash TM
    - squashtm_current_version is version(item, '<') # Apply DB scripts if they are for a more recent version than currently installed
    - squashtm_version is version(item, '>=')        # Do not apply scripts for more recent version than the one being installed
  ignore_errors: True
  loop: "{{ squashtm_db_upgrade_scripts.stdout_lines }}"
  tags: squashtm
