---
- name: Test scripts on ash1d server
  hosts: ash1d
  gather_facts: yes
  vars:
    scripts:
      - simple-test.sh
      - test-webshare-proxies.py
      - oracle-server-setup.sh

  tasks:
    - name: Check if scripts exist in home directory
      stat:
        path: "{{ ansible_env.HOME }}/{{ item }}"
      register: script_files
      loop: "{{ scripts }}"

    - name: Display script file status
      debug:
        msg: "Script {{ item.item }} exists: {{ item.stat.exists }}"
      loop: "{{ script_files.results }}"

    - name: Make scripts executable
      file:
        path: "{{ ansible_env.HOME }}/{{ item.item }}"
        mode: '0755'
      when: item.stat.exists
      loop: "{{ script_files.results }}"

    - name: Test simple-test.sh script
      command: "{{ ansible_env.HOME }}/simple-test.sh"
      register: simple_test_result
      when: script_files.results[0].stat.exists
      ignore_errors: yes

    - name: Display simple-test.sh output
      debug:
        msg: "{{ simple_test_result.stdout_lines }}"
      when: simple_test_result is defined

    - name: Display simple-test.sh errors
      debug:
        msg: "{{ simple_test_result.stderr_lines }}"
      when: simple_test_result is defined and simple_test_result.stderr_lines

    - name: Check Python version for test-webshare-proxies.py
      command: python3 --version
      register: python_version
      ignore_errors: yes

    - name: Display Python version
      debug:
        msg: "Python version: {{ python_version.stdout }}"

    - name: Test test-webshare-proxies.py script (dry run)
      command: "python3 {{ ansible_env.HOME }}/test-webshare-proxies.py --help"
      register: webshare_test_result
      when: script_files.results[1].stat.exists
      ignore_errors: yes

    - name: Display test-webshare-proxies.py help output
      debug:
        msg: "{{ webshare_test_result.stdout_lines }}"
      when: webshare_test_result is defined

    - name: Check oracle-server-setup.sh script syntax
      command: "bash -n {{ ansible_env.HOME }}/oracle-server-setup.sh"
      register: oracle_syntax_check
      when: script_files.results[2].stat.exists
      ignore_errors: yes

    - name: Display oracle-server-setup.sh syntax check result
      debug:
        msg: "Oracle script syntax check: {{ 'PASSED' if oracle_syntax_check.rc == 0 else 'FAILED' }}"
      when: oracle_syntax_check is defined

    - name: Show first 20 lines of oracle-server-setup.sh
      command: "head -20 {{ ansible_env.HOME }}/oracle-server-setup.sh"
      register: oracle_script_preview
      when: script_files.results[2].stat.exists

    - name: Display oracle script preview
      debug:
        msg: "{{ oracle_script_preview.stdout_lines }}"
      when: oracle_script_preview is defined

    - name: Check system information
      setup:
        filter: ansible_distribution,ansible_distribution_version,ansible_architecture,ansible_memtotal_mb,ansible_processor_cores

    - name: Display system information
      debug:
        msg: |
          System: {{ ansible_distribution }} {{ ansible_distribution_version }}
          Architecture: {{ ansible_architecture }}
          Memory: {{ ansible_memtotal_mb }}MB
          CPU Cores: {{ ansible_processor_cores }}
