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

# /*---------------------------------------------------------------------------8
# |                                                                            |
# |         SAP: Register BOM                                                  |
# |         SAP: deploy Generic Parameter file template                        |
# |         SAP: SAP OS USERS and Group Creation                               |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

---

- name:                                "User Creation: Set BOM facts"
  ansible.builtin.set_fact:
    sap_inifile_template:              "generic-inifile-param.j2"
    sap_inifile:                       "{{ bom_base_name }}-generic-{{ sid_to_be_deployed.sid | lower }}-{{ ansible_hostname }}.params"
    dir_params:                        "{{ tmp_directory }}/.{{ sid_to_be_deployed.sid | lower }}-params"

- name:                                "User Creation: 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.sid | upper }}' }
    - { mode: '0755', path: '/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}' }

- name:                                "User Creation: reset"
  ansible.builtin.file:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/user_created.txt"
    state:                             absent
  when:                                reinstall

- name:                                "User Creation: check if Completed"
  ansible.builtin.stat:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/user_created.txt"
  register:                            users_created

- name:                                "User Creation: check if installed"
  ansible.builtin.debug:
    var:                               users_created
    verbosity:                         2

- name:                                "User Creation: Include 3.3.1-bom-utility role"
  ansible.builtin.include_role:
    name:                              roles-sap/3.3.1-bom-utility
    tasks_from:                        bom-register
  vars:
    bom_name:                          "{{ bom_base_name }}"
    task_prefix:                       "GENERIC: "
    sa_enabled:                        true
    always_upload_jinja_templates:     false

- name:                                "User Creation: 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:
    param_directory:                   "{{ dir_params }}"
    sap_sid:                           "{{ sid_to_be_deployed.sid }}"
    this_sidadm_uid:                   "{{ sid_to_be_deployed.sidadm_uid }}"

- name:                                "User Creation: install variables"
  ansible.builtin.debug:
    msg:
      - "INSTALLED:  {{ users_created.stat.exists }}"
      - "INIFILE:    {{ sap_inifile }}"
      - "PRODUCT ID: {{ bom.product_ids.generic }}"
    verbosity:                         2

- name:                                "User Creation"
  block:
    - name:                            "User Creation: check media exists"
      ansible.builtin.stat:
        path:                          "{{ target_media_location }}/SWPM/sapinst"
      register:                        sapinst_found

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

    - name:                            "User Creation: Progress"
      ansible.builtin.debug:
        msg:                           "Start user creation"

    - name:                            "User Creation: SAP OS USERS and Group Creation Ini file {{ sid_to_be_deployed.sid | upper }}"
      ansible.builtin.shell: |
                                       cat {{ dir_params }}/{{ sap_inifile }}
      register:                        generic_user_creation_ini

    - name:                            "User Creation: Progress"
      ansible.builtin.debug:
        var:                           generic_user_creation_ini
        verbosity:                     4

    - name:                            "User Creation: SAP OS USERS and Group Creation {{ sid_to_be_deployed.sid | upper }}"
      become:                          true
      become_user:                     root
      ansible.builtin.shell: |
                                       umask {{ custom_umask | default('022') }} ;
                                       ./sapinst SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}/{{ sap_inifile }} \
                                                 SAPINST_EXECUTE_PRODUCT_ID={{ bom.product_ids.generic }}        \
                                                 SAPINST_SKIP_DIALOGS=true                                       \
                                                 SAPINST_START_GUISERVER=false
      args:
        chdir:                         "{{ target_media_location }}/SWPM"
        creates:                       "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/user_created.txt"
      environment:
        SAPSYSTEMNAME:                 "{{ sid_to_be_deployed.sid | upper }}"
        TMPDIR:                        "{{ tmp_directory }}/{{ sid_to_be_deployed.sid | upper }}"
      register:                        generic_user_creation
      failed_when:                     generic_user_creation.rc > 0

    - name:                            "User Creation: Installation results"
      ansible.builtin.debug:
        var:                           generic_user_creation
      when:                            generic_user_creation.rc > 0

    - name:                            "User Creation: Installation results"
      become:                          true
      become_user:                     root
      when:                            generic_user_creation.rc == 0
      block:
        - name:                        "User Creation: Installation results"
          ansible.builtin.debug:
            msg:                       "User Creation succeeded"

        - name:                        "User Creation: flag"
          ansible.builtin.file:
            path:                      "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/user_created.txt"
            state:                     touch
            mode:                      0755

    - name:                            "User Creation: Cleanup ini file {{ ansible_hostname }}"
      ansible.builtin.file:
        path:                          "{{ dir_params }}/{{ sap_inifile }}"
        state:                         absent

    - name:                            "User Creation: flag"
      become:                          true
      become_user:                     root
      ansible.builtin.file:
        path:                          "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/user_created.txt"
        state:                         touch
        mode:                          0755
      when:                            generic_user_creation.rc == 0

  when:
    - not users_created.stat.exists

- name:                                "User creation status"
  ansible.builtin.debug:
    msg:                               "Users already created"
  when:
    - users_created.stat.exists

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