---
  # "Install a converted application and link it to the FWD distribution."
  
  - name: "Create the application directory." 
    file:
       path: "{{ fwd_install_root }}/{{ fwd_app_name }}"
       state: directory
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0550
       
  - name: "Check if the application directory is empty."
    find:
       paths: "{{ fwd_install_root }}/{{ fwd_app_name }}/"
    register: dir_files
    changed_when: false

  - name: "Unzip the converted application configuration."
    command: unzip -q "{{ fwd_install_root }}/{{ fwd_app_cfg_zip }}" -d "{{ fwd_install_root }}/{{ fwd_app_name }}/"
    when: dir_files.matched|int == 0

  - name: "Check if the legacy source code zip file exists."
    stat:
       path: "{{ fwd_install_root }}/{{ fwd_app_code_zip }}"
    register: legacy_code
    changed_when: false

  - name: "Unzip the legacy application code and resources."
    command: unzip -q "{{ fwd_install_root }}/{{ fwd_app_code_zip }}" -d "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/"
    when: legacy_code.stat.exists and dir_files.matched|int == 0

  - name: "Download the FWD Possenet archive."
    get_url:
       url: "{{ fwd_possenet_url }}"
       dest: "{{ fwd_install_root }}/fwd_possenet.zip"
       owner: "{{ fwd_acct_name }}"
       group: "{{ fwd_acct_name }}"
       mode: 0644
    register: possenet_result
    when: fwd_possenet|bool
       
  - name: "Unzip the Possenet sources."
    command: unzip -q "{{ fwd_install_root }}/fwd_possenet.zip" -d "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/"
    register: unzip_result
    when: fwd_possenet|bool and possenet_result and dir_files.matched|int == 0

  - name: "Check if the patch zip file exists."
    stat:
       path: "{{ fwd_install_root }}/{{ fwd_app_patches_zip }}"
    register: patch_file
    changed_when: false

  - name: "Unzip the patches for the legacy application code."
    command: unzip -q "{{ fwd_install_root }}/{{ fwd_app_patches_zip }}" -d "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/"
    register: patch_files
    when: patch_file.stat.exists and dir_files.matched|int == 0
    
  - name: "Check if the {{ fwd_app_name }}.df schema file exists."
    stat:
       path: "{{ fwd_install_root }}/{{ fwd_app_name }}/data/{{ fwd_app_name }}.df"
    register: schema_file
    changed_when: false
    
  - name: "Check if the {{ fwd_install_root }}/{{ fwd_app_schemata_zip }} zip exists."
    stat:
       path: "{{ fwd_install_root }}/{{ fwd_app_schemata_zip }}"
    register: schema_zip
    changed_when: false

  - name: "Extract and move the schema .df file to data/{{ fwd_app_name }}.df"
    shell: unzip "{{ fwd_install_root }}/{{ fwd_app_schemata_zip }}" && mv `unzip -Z  -1 "{{ fwd_install_root }}/{{ fwd_app_schemata_zip }}"` "{{ fwd_app_name }}.df"
    args:
       chdir: "{{ fwd_install_root }}/{{ fwd_app_name }}/data/"
    when: schema_zip.stat.exists and not schema_file.stat.exists

  - name: "Check if the patches.sh script exists."
    stat:
       path: "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/patches.sh"
    register: patch_script
    changed_when: false

  - name: "Set executable permissions for patches.sh"
    file:
       path: "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/patches.sh"
       mode: 0775
    when: patch_script.stat.exists

  - name: "Apply the patches to the legacy application code."
    shell: "{{ fwd_install_root }}/{{ fwd_app_name }}/abl/patches.sh"
    args:
       chdir: "{{ fwd_install_root }}/{{ fwd_app_name }}/abl"
    when: patch_script.stat.exists and patch_files.changed
      
  - name: "Create the symlink to the FWD build."
    file:
       src: "{{ fwd_install_root }}/p2j/"
       dest: "{{ fwd_install_root }}/{{ fwd_app_name }}/p2j"
       state: link

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