---
- hosts: zos1.oc
  collections:
    - ibm.ibm_zos_core
  gather_facts: no
  vars:
    src_name: "TESTA.cbl"
    compile_job_name: "COMPILE"
    git_repo: "{{ tower_webhook_payload['project']['git_http_url'] }}"
    git_branch: "{{ tower_webhook_payload.ref.split('/')[2] }}"
    end_play_identifier: "{{tower_webhook_payload.commits}}"

  tasks:
    # ##########################################################################
    # Fact setting for use by this playbook
    # ##########################################################################
    - name: end_play
      meta: end_play
      when: end_play_identifier == []

    - name: Setting fact `data_set_name` for use by this sample
      set_fact:
        data_set_name: "{{ ansible_user | upper }}.{{git_branch}}.DATASET"

    - name: Fact `data_set_name` set with value
      debug:
        msg: "{{ data_set_name }}"

    - name: Setting fact `load_lib_name` for use by this sample
      set_fact:
        load_lib_name: "{{ ansible_user | upper }}.{{git_branch}}.LOAD"

    - name: Fact `load_lib_name` set with value
      debug:
        msg: "{{ load_lib_name }}"

    - name: Detecting system name
      command: uname -n
      register: result

    - name: Setting fact `system_name` for use by this sample
      set_fact:
        system_name: "{{ result.stdout }}"

    - name: Fact `system_name` set with value
      debug:
        msg: "{{ system_name }}"

    # ##########################################################################
    # Ping the host with Ansible ping module to see if the target is available
    # ##########################################################################
    - name: Ping host - {{ inventory_hostname }}
      ping:
      register: result

    - name: Response
      debug:
        msg: "{{ result.ping }}"


    # ##########################################################################
    # create temp build directory 
    # ##########################################################################
    - name: create temporary build directory
      tempfile:
         path: "/u/tmp"
         state: directory
         suffix: "build"
      register: temp_dir

    - name: Temp directory to git pull 
      debug:
        msg: "{{ temp_dir }}" 

    # ##########################################################################
    # git clone from github to z/os uss
    # ##########################################################################
    - name: Clone a private repository
      git:
        repo: "{{ git_repo }}"
        dest: "{{ temp_dir.path }}"
        version: "{{git_branch}}"

    # ##########################################################################
    # replace the compile job with branch info
    # ##########################################################################
    - name: Replace the srcfile&loadlib in compile jcl
      command: 'sed -i "s|srcfile|{{data_set_name}}({{ src_name.split(".")[0] }})|;s|loadlib|{{load_lib_name}}({{ src_name.split(".")[0] }})|" {{ temp_dir.path }}/{{compile_job_name}}'
      register: result
    
    - name: Response for replace the srcfile&loadlib in compile jcl
      debug:
        msg: "{{ result }}"  

    - name: codepage processing
      shell: 'iconv -f UTF-8 -t IBM-1047 {{ temp_dir.path }}/{{compile_job_name}} > {{ temp_dir.path }}/tmp'
      register: result
    
    - name: Response for codepage processing
      debug:
        msg: "{{ result }}"  


    # ##########################################################################
    # Create a PDS dataset and populate src from uss files to dataset
    # ##########################################################################
    - name: Create a PDS data set {{ data_set_name }}
      zos_data_set:
        name: "{{ data_set_name }}"
        type: pds
        size: 5M
        format: fb
        record_length: 80
        replace: yes
      register: result

    - name: Response for create a PDS data set {{ data_set_name }}
      debug:
        msg: "{{ result }}"

    - name: Populate {{ data_set_name }} member with data from USS file in
        in {{ temp_dir.path }}
      command: 'cp -O u {{ temp_dir.path }}/tmp "//''{{ data_set_name }}({{ compile_job_name }})''"'
      register: result

    - name: Response for populate {{ compile_job_name }} member with data from
        USS file in {{ temp_dir.path }}
      debug:
        msg: "{{ result }}"

    - name: Populate {{ data_set_name }} member with data from USS file in
        in {{ temp_dir.path }}
      command: 'cp -O u {{ temp_dir.path }}/{{ src_name }} "//''{{ data_set_name }}({{ src_name.split(".")[0] }})''"'
      register: result

    - name: Response for populate {{ src_name }} member with data from
        USS file in {{ temp_dir.path }}
      debug:
        msg: "{{ result }}"
  
    # ##########################################################################
    # clean temp build directory 
    # ##########################################################################
    - name: clean temporary build directory
      file:
       path: "{{ temp_dir.path }}"
       state: absent
      when: temp_dir.path is defined

    # ##########################################################################
    # Compile the src to load module
    # ##########################################################################

    - name: Compile {{ src_name }} 
      zos_job_submit:
        src: "{{ data_set_name }}({{ compile_job_name }})"
        location: DATA_SET
        wait: True
      register: result

    - name: Response for submit jcl
      debug:
        msg: "{{ result }}"


- hosts: localhost
  vars:
    src_name: "TESTA.cbl"
    region_name: "{{ tower_webhook_payload.ref.split('/')[2] }}"
    cics_host: "zsystem.lab.csc"
    cmci_port: "9004"
    end_play_identifier: "{{tower_webhook_payload.commits}}"
    zos_pass: !vault |
          $ANSIBLE_VAULT;1.2;AES256;zosmf_pass
          61623132653139383833633931306131633236356532303731326232303166616334346537303765
          3664313961623261653731303066656339396565333162350a303462373632366538303861303766
          33336566396635363937386135386664306263613339306337656139663130623764356461656132
          3536633636356135390a633538643136373964343439656433396262656363336339666364663365
          6139
  tasks:
    # ##########################################################################
    # run zowe cli command
    # ##########################################################################
    - name: end_play
      meta: end_play
      when: end_play_identifier == []
 
    - name: run zowe cics cli to create profile
      command: zowe profiles create cics {{ region_name }}  -H {{ cics_host }} -P {{ cmci_port }}  -u {{ ansible_user | upper }} -p {{ zos_pass }}  -o http

    - name: run zowe cics cli to set program new 
      command: zowe cics refresh program {{ src_name.split(".")[0] }} --region-name {{ region_name }} --cics-profile {{ region_name }}

    - name: delete cics profile
      command: zowe profiles delete cics {{ region_name }}

