---
  # "Install a FWD distribution and build it."
  
  - name: "Download the FWD distribution archive."
    get_url:
       url: "{{ fwd_distro_url }}"
       dest: "{{ fwd_install_root }}/fwd.zip"
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0644
    register: download_result
       
  - name: "Remove the previous FWD install."
    file:
       path: "{{ fwd_install_root }}/p2j"
       state: absent
    when: download_result.changed
       
  - name: "Check if FWD install folder exists"
    stat:
       path: "{{ fwd_install_root }}/p2j"
    register: fwd

       
  # starting in Ansible 2.2, the unarchive module and remote_src = true can be used to unzip
  # the already downloaded archive
  - name: "Unzip the distribution."
    command: unzip -q "{{ fwd_install_root }}/fwd.zip" -d "{{ fwd_install_root }}"
    register: unzip_result
    #unarchive:
    #   src: "{{ fwd_install_root }}/fwd.zip"
    #   dest: "{{ fwd_install_root }}"
    #   remote_src: true
    #   owner: "{{ fwd_acct_name }}"
    #   group: "{{ fwd_acct_name }}"
    #   mode: 0755
    when: download_result.changed or not(fwd.stat.isdir is defined and fwd.stat.isdir)
    
  # starting in Ansible 2.2, the unarchive module can be used above and it should be able to handle
  # the ownership and perms
  - name: "Force ownership and permissions to be correct."
    file:                               
       path: "{{ fwd_install_root }}/p2j"
       state: directory
       recurse: yes
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0755
    when: unzip_result.changed and unzip_result|success
    
  - name: "Build FWD."  
    shell: ./gradlew all
    args:
       chdir: "{{ fwd_install_root }}/p2j"
    become_method: sudo
    become_user: "{{ fwd_acct_name }}"
    register: build_result
    when: unzip_result.changed and unzip_result|success

  - name: "Ensure that the ownership and permissions are correct (all contents)." 
    file:
       path: "{{ fwd_install_root }}/p2j/"
       state: directory
       follow: no
       recurse: yes
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0775

  - name: "Install text collator."
    copy:
       src: "{{ fwd_install_root }}/p2j/build/lib/p2jspi.jar"
       dest: "/usr/lib/jvm/default-java/jre/lib/ext/p2jspi.jar"
       remote_src: yes
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0644
       
