# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

---
# /*---------------------------------------------------------------------------8
# |                                                                            |
# |                Perform the MSSQL DB Instance installation                  |
# |                  Generic: Install from Microsoft bits                      |
# |                  SAP4SQL: Install from SAP provided bits                   |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

# Draft version
# Data file in a different disk


- name:                                "WIN-SQL: Create run flag directory"
  ansible.windows.win_file:
    path:                              '{{ sap_deployment_automation }}\{{ sap_sid | upper }}'
    state:                             directory

- name:                                "WIN-SQL: check if SQL Server service is started"
  ansible.windows.win_service_info:
    name:                              MSSQLServer
  register:                            sql_service

- name:                                "WIN-SQL: SQL4SAP Install || Flag File"
  ansible.windows.win_file:
    path:                              '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
    state:                             touch
  when:                                sql_service.exists

# - name:                                     "WIN-SQL: Install reset"
#   ansible.windows.win_file:
#     path:                                   '{{ target_media_location }}\sap_deployment_automation\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
#     state:                                  absent
#   when: reinstall

- name:                                "WIN-SQL: check if installed"
  ansible.windows.win_stat:
    path:                              '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
  register:                            sql_installed

# +------------------------------------4--------------------------------------*/
- name:                                "WIN-SQL: check if SQL4SAP is installed"
  block:
    - name:                            "WIN-SQL: Install status"
      ansible.builtin.debug:
        msg:                           "WIN-SQL: is already installed"

    - name:                            "WIN-SQL:: - return value"
      ansible.builtin.set_fact:
        sql4sap_already_installed:     true

  when:
    - sql_installed.stat.exists


- name:                                     "WIN-SQL: Install from Microsoft bits"
  block:
    - name:                                 "WIN-SQL: Progress"
      ansible.builtin.debug:
        msg:                                "Start SQL4SAP Installation"

    - name:                                 "WIN-SQL: Install SQL4SAP on {{ ansible_hostname }}"
      ansible.windows.win_shell: |
                                            {{ download_directory_windows }}\{{ mssserver_version }}\SQL4SAP.bat -d
      args:
        creates:                            '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
      vars:
        ansible_become:                     true
        ansible_become_method:              ansible.builtin.runas
        ansible_become_user:                '{{ sap_sid }}adm@{{ domain_name }}'
        ansible_become_password:            "{{ domain_user_password }}"
      register:                             sql_install_output
      failed_when:                          sql_install_output.rc != 0

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:                                "SQL4SAP Installation succeeded"
      changed_when:                         true
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("Execution successful")
      notify:                               "WIN-SQL: Restart SQL Server VM"

    - name:                                 "Force all notified handlers to run now"
      ansible.builtin.meta:                 flush_handlers

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:                                "WIN-SQL: A reboot is pending. We will restart the machine, so please start SQL4SAP installation again."
      changed_when:                         true
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("A reboot is pending.")
      notify:                               "WIN-SQL: Restart SQL Server VM"

    - name:                                 "Force all notified handlers to run now"
      ansible.builtin.meta:                 flush_handlers

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:
          - "SQL4SAP Installation failed, another installation might be already exist"
          - "SQL4SAP output: {{ sql_install_output }}"
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("Error during execution")

    - name:                                 "WIN-SQL: SQL4SAP Install || Flag File"
      ansible.windows.win_file:
        path:                               '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
        state:                              touch
      when:
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is not search("A reboot is pending.")

  when:
    - not sql_installed.stat.exists
    - use_sql_for_SAP

- name:                                     "WIN-SQL: Install from Microsoft "
  block:

    - name:                                 "WIN-SQL: Install SQL4SAP on {{ ansible_hostname }}"
      ansible.windows.win_shell: |
                                            {{ download_directory_windows }}\{{ mssserver_version }}\SQL4SAP.bat -d
      args:
        creates:                            '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
      vars:
        ansible_become:                     true
        ansible_become_method:              ansible.builtin.runas
        ansible_become_user:                '{{ sap_sid }}adm@{{ domain_name }}'
        ansible_become_password:            "{{ domain_user_password }}"
      register:                             sql_install_output
      failed_when:                          sql_install_output.rc != 0

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:                                "SQL4SAP Installation succeeded"
      changed_when:                         true
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("Execution successful")
      notify:                               "WIN-SQL: Restart SQL Server VM"

    - name:                                 "Force all notified handlers to run now"
      ansible.builtin.meta:                                 flush_handlers

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:                                "WIN-SQL: A reboot is pending. We will restart the machine, so please start SQL4SAP installation again."
      changed_when:                         true
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("A reboot is pending.")
      notify:                               "WIN-SQL: Restart SQL Server VM"

    - name:                                 "Force all notified handlers to run now"
      ansible.builtin.meta:                 flush_handlers

    - name:                                 "WIN-SQL: Installation results"
      ansible.builtin.debug:
        msg:
          - "SQL4SAP Installation failed, another installation might be already exist"
          - "SQL4SAP output: {{ sql_install_output }}"
      when:
        - sql_install_output.rc is defined
        - sql_install_output.rc == 0
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is search("Error during execution")

    - name:                                 "WIN-SQL: SQL4SAP Install || Flag File"
      ansible.windows.win_file:
        path:                               '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\sap_deployment_sqldb.txt'
        state:                              touch
      when:
        - sql_install_output.stdout | length > 0
        - sql_install_output.stdout is not search("A reboot is pending.")

  when:
    - not sql_installed.stat.exists
    - not use_sql_for_SAP


...

# /*---------------------------------------------------------------------------8
# |                                   END                                      |
# +------------------------------------4--------------------------------------*/
