---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Checking if java build tools has been deployed"
      register: build_tools_deployed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/javabuildtools"
        checksum_algorithm: sha256
    
    -
      name: "Creating Java Build Tools Directories"
      ansible.builtin.file:
        mode: '0755'
        path: "{{ item }}"
        state: directory
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        recurse: true
      with_items:
        - "{{ installer_dir }}/domino-java-helpers/src/main/java/net/prominic/domino/vagrant"
        - "{{ installer_dir }}/domino-java-helpers/build"
        - "{{ installer_dir }}/domino-java-helpers/dxl"
    
    -
      name: "Copying Java helper applications source files"
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ item }}"
        dest: "{{ installer_dir }}/domino-java-helpers/src/main/java/net/prominic/domino/vagrant"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - src/main/java/net/prominic/domino/vagrant/CreateNamesDatabase.java
        - src/main/java/net/prominic/domino/vagrant/CheckNotesUser.java
        - src/main/java/net/prominic/domino/vagrant/CheckDatabase.java
        - src/main/java/net/prominic/domino/vagrant/CreateDatabase.java
        - src/main/java/net/prominic/domino/vagrant/CrossCertifyNotesID.java
        - src/main/java/net/prominic/domino/vagrant/CreateAdditionalServerID.java
        - src/main/java/net/prominic/domino/vagrant/CreateAdditionalServerJNA.java
        - src/main/java/net/prominic/domino/vagrant/DirectServerRegistration.java
        - src/main/java/net/prominic/domino/vagrant/DXLImport.java
        - src/main/java/net/prominic/domino/vagrant/HybridServerRegistration.java
        - src/main/java/net/prominic/domino/vagrant/NotesAPI.java
        - src/main/java/net/prominic/domino/vagrant/PushUserIDtoIDVault.java
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Copying Java helper applications Example Servlet"
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ item }}"
        dest: "{{ installer_dir }}/domino-java-helpers/src/main/java"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - src/main/java/ExampleServlet.java
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Copying DXL Examples"
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ item }}"
        dest: "{{ installer_dir }}/dxl"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - dxl/HelloWorld.dxl
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Copying Java helper applications gradle build files"
      ansible.builtin.copy:
        mode: '0644'
        src: "{{ item }}"
        dest: "{{ installer_dir }}/domino-java-helpers"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - domino_java_tools.javaproj
        - build.gradle
        - build-notesjava-standalone-example-apps.bsh
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Copy Gradle properties file"
      ansible.builtin.template:
        mode: '0644'
        src: "{{ item }}"
        dest: "{{ installer_dir }}/domino-java-helpers/gradle.properties"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - gradle.properties.j2
    
    # -
    #   name: "Copying Java helper applications"
    #   ansible.builtin.copy:
    #     mode: '0644'
    #     src: "{{ item }}"
    #     dest: "{{ service_home_dir }}"
    #     owner: "{{ service_user }}"
    #   with_items:
    #     - "{{ installer_dir }}/domino-java-helpers/JavaTest/Notes.jar"
    #     - "{{ installer_dir }}/domino-java-helpers/CreateNamesDatabase.exp"
    #   when: java_helpers_install and build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Changing file ownership, group and permissions of Java Build Files"
      when: build_utility_jars and not build_tools_deployed.stat.exists
      ansible.builtin.file:
        path: "{{ installer_dir }}/domino-java-helpers"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        mode: '0755'
        recurse: true
    
    -
      name: "Creating Templated JSON for Names.nsf ACLs"
      when: build_utility_jars and not build_tools_deployed.stat.exists
      ansible.builtin.template:
        dest: "{{ service_home_dir }}/default_cross_certify_acl.json"
        mode: "a+x"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        src: "default_cross_certify_acl.json.j2"
    
    -
      name: "Creating Templated Cross Certify Properties file"
      when: build_utility_jars and not build_tools_deployed.stat.exists
      ansible.builtin.template:
        dest: "{{ service_home_dir }}/CrossCertifyNotesID.properties"
        mode: "a+x"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        src: "CrossCertifyNotesID.properties.j2"
    
    -
      name: "Changing file ownership, group and permissions of Java Build Files"
      become: true
      become_user: root
      ansible.builtin.file:
        path: "{{ completed_dir }}"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        mode: '0777'
        recurse: true
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Ensure correct permissions for installer directory"
      ansible.builtin.file:
        path: "/vagrant/installers"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        mode: '0777'
      become: true
      become_user: root
      when: build_utility_jars and not build_tools_deployed.stat.exists
    
    -
      name: "Building utility jars and deploying"
      ansible.builtin.command: "{{ item }}"
      become: true
      become_user: "{{ service_user }}"
      args:
        chdir: "{{ installer_dir }}/domino-java-helpers"
      when: build_utility_jars and not build_tools_deployed.stat.exists and not use_proxy
      with_items:
        - "bash -c 'source {{ service_home_dir }}/.sdkman/bin/sdkman-init.sh && gradle clean jarIndividual --stacktrace'"
    
    -
      name: "Building utility jars and deploying"
      ansible.builtin.command: "{{ item }}"
      become: true
      become_user: "{{ service_user }}"
      args:
        chdir: "{{ installer_dir }}/domino-java-helpers"
      when: build_utility_jars and not build_tools_deployed.stat.exists and use_proxy
      with_items:
        - "bash -c 'source {{ service_home_dir }}/.sdkman/bin/sdkman-init.sh && gradle clean jarIndividual --stacktrace'"
      environment:
        http_proxy: "http://{{ proxy_server }}:{{ proxy_port }}"
        https_proxy: "http://{{ proxy_server }}:{{ proxy_port }}"
        GRADLE_OPTS: "-Dhttp.proxyHost={{ proxy_server }} -Dhttp.proxyPort={{ proxy_port }} -Dhttps.proxyHost={{ proxy_server }} -Dhttps.proxyPort={{ proxy_port }}"
    
    -
      name: "Marking jars as installed"
      when: build_utility_jars and not build_tools_deployed.stat.exists
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: touch
      with_items:
        - "{{ completed_dir }}/javabuildtools"
    
    -
      name: "Finding build Libs in domino-java-helpers/build/libs/"
      when: not build_tools_deployed.stat.exists
      ansible.builtin.find:
        paths: "{{ installer_dir }}/domino-java-helpers/build/libs/"
        file_type: file
        patterns: '*.jar'
      register: build_jar_list
    
    -
      name: "Copying Build Libs for {{ service_user }}"
      when: not build_tools_deployed.stat.exists
      ansible.builtin.copy:
        mode: '0755'
        remote_src: true
        owner: "{{ service_user }}"
        src: "{{ item.path }}"
        dest: "{{ service_home_dir }}"
      loop: "{{ build_jar_list.files }}"
