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

#   SAP: Register BOM
#   SAP WEB: deploy Web Dispatcher Parameter file install template
#   SAP WEB: Install

# TODO: Considerations
#         20G+ swap space
#         Livecache: None, HANA Internal, MaxDB External
#         message server acl
#         certificates
#         secure storage key
#

---

- name:                                "SAP Web Dispatcher Install: Set BOM facts"
  ansible.builtin.set_fact:
    sap_inifile:                       "{{ bom_base_name }}-web-{{ sid_to_be_deployed.web_sid | lower }}-{{ ansible_hostname }}.params"
    sap_inifile_template:              "web-inifile-param.j2"
    dir_params:                        "{{ tmp_directory }}/.{{ sid_to_be_deployed.web_sid | lower }}-params"

- name:                                "SAP Web Dispatcher Install: Create directories"
  ansible.builtin.file:
    path:                              "{{ item.path }}"
    state:                             directory
    mode:                              '{{ item.mode }}'
  loop:
    - { mode: '0755', path: '{{ dir_params }}' }
    - { mode: '0755', path: '{{ tmp_directory }}/{{ sid_to_be_deployed.web_sid | upper }}' }
    - { mode: '0755', path: '/etc/sap_deployment_automation/{{ sid_to_be_deployed.web_sid | upper }}' }

- name:                                "SAP Web Dispatcher Install: reset"
  ansible.builtin.file:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.web_sid | upper }}/sap_deployment_web.txt"
    state:                             absent
  when:                                reinstall

- name:                                "SAP Web Dispatcher Install: check if {{ sid_to_be_deployed.web_sid | upper }} is installed"
  ansible.builtin.stat:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.web_sid | upper }}/sap_deployment_web.txt"
  register:                            web_installed

# Returns bom object
- name:                                "SAP Web Dispatcher Install: Register BoM"
  ansible.builtin.include_role:
    name:                              roles-sap/3.3.1-bom-utility
    tasks_from:                        bom-register
  vars:
    bom_name:                          "{{ bom_base_name }}"
    task_prefix:                       "SAP Web Dispatcher Install: "
    sa_enabled:                        true

- name:                                "Web install: register variables"
  ansible.builtin.set_fact:
    web_bom_id:                        "{{ bom.product_ids.web }}"

- name:                                "Web Install"
  become:                              true
  block:

    - name:                            "SAP Web Dispatcher Install: Get the SCS Server name list"
      ansible.builtin.set_fact:
        scs_server_temp:               "{{ scs_server_temp | default([]) + [item] }}"
      with_items:
        - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_SCS') }}"
        - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_DB') }}"

    - name:                            "SAP Web Dispatcher Install: Set the SCS Server name"
      ansible.builtin.set_fact:
        scs_server:                     "{% if scs_high_availability %}{{ sap_sid | lower }}scs{{ scs_instance_number }}cl1{% else %}{{ scs_server_temp | first }}{% endif %}"

    - name:                            "SAP Web Dispatcher Install: check media exists"
      ansible.builtin.stat:
        path:                          "{{ target_media_location }}/SWPM/sapinst"
      register:                        sapinst_found

    - name:                            "ErrorHandling"
      ansible.builtin.fail:
        msg:                           "INSTALL:0008:Unable to find sapinst, please check that the installation media is mounted"
      when: not sapinst_found.stat.exists

    - name:                            "APP Install: Template processing - Create ini file {{ sap_inifile }} from {{ sap_inifile_template }}"
      ansible.builtin.template:
        src:                           "{{ sap_inifile_template }}"
        dest:                          "{{ dir_params }}/{{ sap_inifile }}"
        mode:                          0644
        force:                         true
      vars:
        task_prefix:                   "SAP Web Dispatcher Install: "
        sap_cd_package_hdbclient:      "{{ target_media_location }}/CD_HDBCLIENT/SAP_HANA_CLIENT"
        sap_cd_package_cd1:            "{{ target_media_location }}/CD_EXPORT"
        web_instance_number:           "{{ wd_instance_number }}"
        sap_ciInstanceNumber:          "{{ scs_instance_number }}"
        sap_profile_dir:               /sapmnt/{{ sap_sid | upper }}/profile
        sap_scs_hostname:              "{{ custom_scs_virtual_hostname | default(scs_server, true) }}"
        sap_webVirtualHostname:        "{{ virtual_host }}"
        sap_installSAPHostAgent:       "false"
        web_sid:                       "{{ sid_to_be_deployed.web_sid | upper }}"
        sap_sid:                       "{{ sid_to_be_deployed.sid | upper }}"

    - name:                            "SAP Web Dispatcher Install: install variables"
      ansible.builtin.debug:
        msg:
          - "INSTALLED:  {{ web_installed.stat.exists }}"
          - "INIFILE:    {{ dir_params }}/{{ sap_inifile }}"
          - "SID:        {{ sid_to_be_deployed.web_sid }}"
          - "SAPSID:     {{ sap_sid }}"
          - "PRODUCT ID: {{ web_bom_id }}"
          - "INSTANCE:   {{ web_instance_number }}"
          - "SCSHOST:    {{ custom_scs_virtual_hostname | default(scs_server, true) }}"
          - "LOCATION:   {{ target_media_location }}/SWPM"
          - "TMPDIR:     {{ tmp_directory }}/{{ sid_to_be_deployed.web_sid | upper }}"
# *====================================4=======================================8
#   SAP Web Dispatcher: Install
# 2230669 - System Provisioning Using a Parameter Input File
#

    - name:                            "SAP Web Dispatcher Install: Create temp directory for sid"
      ansible.builtin.file:
        path:                          "{{ tmp_directory }}/{{ sid_to_be_deployed.web_sid | upper }}"
        state:                         directory
        mode:                          0755

    - name:                            "SAP Web Dispatcher Install: progress"
      ansible.builtin.debug:
        msg:                           "Starting Web Install ({{ sid_to_be_deployed.web_sid | upper }})"

    - name:                            "SAP Web Dispatcher Install"
      ansible.builtin.shell: |
                                       umask {{ custom_umask | default('022') }} ;
                                       ./sapinst SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}/{{ sap_inifile }} \
                                                 SAPINST_EXECUTE_PRODUCT_ID={{ bom.product_ids.web }}            \
                                                 SAPINST_SKIP_DIALOGS=true                                       \
                                                 SAPINST_USE_HOSTNAME={{ virtual_host }}                         \
                                                 SAPINST_START_GUISERVER=false
      args:
        chdir:                         "{{ target_media_location }}/SWPM"
        creates:                       "/etc/sap_deployment_automation/{{ sid_to_be_deployed.web_sid | upper }}/sap_deployment_web.txt"
      environment:
        SAPSYSTEMNAME:                 "{{ sid_to_be_deployed.web_sid | upper }}"
        TMPDIR:                        "{{ tmp_directory }}/{{ sid_to_be_deployed.web_sid | upper }}"
      register:                        web_installation
      failed_when:                     web_installation.rc > 0
      when:                            node_tier == 'web'

    - name:                            "SAP Web Dispatcher Install: Installation results"
      ansible.builtin.debug:
        var:                           web_installation
      when:                            web_installation.rc > 0

    - name:                            "SAP Web Dispatcher Install: Installation results"
      ansible.builtin.debug:
        msg:                           "Web Dispatcher Installation succeeded"
      when:                            web_installation.rc == 0

    - name:                            "SAP Web Dispatcher Install: flag"
      ansible.builtin.file:
        path:                          "/etc/sap_deployment_automation/{{ sid_to_be_deployed.web_sid | upper }}/sap_deployment_web.txt"
        state:                         touch
        mode:                          0755

  when:
    - not web_installed.stat.exists

# *====================================4=======================================8

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