---
- name: Configure z/VM s390x Build Environment for Arch Linux
  hosts: zvm-build
  become: yes
  vars:
    build_user: archbuild
    build_home: /home/archbuild
    arch_root: /opt/arch-s390x
    
  tasks:
    - name: Install EPEL repository for RHEL 9
      dnf:
        name: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm"
        state: present
        disable_gpg_check: yes
      when: ansible_distribution == "RedHat"

    - name: Install development tools and build dependencies
      dnf:
        name:
          # Core build tools
          - gcc
          - gcc-c++
          - make
          - cmake
          - autoconf
          - automake
          - libtool
          - pkg-config
          - bison
          - flex
          - gettext
          - texinfo
          
          # Version control
          - git
          - subversion
          
          # Archive tools
          - tar
          - gzip
          - bzip2
          - xz
          - unzip
          - zip
          
          # Development libraries
          - glibc-devel
          - glibc-static
          - libstdc++-devel
          - libstdc++-static
          - ncurses-devel
          - readline-devel
          - zlib-devel
          - openssl-devel
          - libffi-devel
          - expat-devel
          - gmp-devel
          - mpfr-devel
          - libmpc-devel
          
          # Python development
          - python3-devel
          - python3-pip
          - python3-setuptools
          
          # Perl development
          - perl
          - perl-devel
          - perl-ExtUtils-MakeMaker
          
          # Documentation tools
          - asciidoc
          - xmlto
          - docbook-xsl
          
          # Debugging tools
          - gdb
          - strace
          - ltrace
          - valgrind
          
          # Network tools
          - wget
          - curl
          - rsync
          
          # System tools
          - sudo
          - screen
          - tmux
          - vim
          - nano
          
          # Build system extras
          - ninja-build
          - meson
          - ccache
          
          # Patch and diff tools
          - patch
          - diffutils
        state: present

    - name: Create build user
      user:
        name: "{{ build_user }}"
        comment: "Arch Linux s390x Build User"
        shell: /bin/bash
        home: "{{ build_home }}"
        create_home: yes
        groups: wheel
        append: yes

    - name: Configure sudo for build user
      lineinfile:
        path: /etc/sudoers.d/archbuild
        line: "{{ build_user }} ALL=(ALL) NOPASSWD: ALL"
        create: yes
        mode: '0440'

    - name: Create Arch Linux build directories
      file:
        path: "{{ item }}"
        state: directory
        owner: "{{ build_user }}"
        group: "{{ build_user }}"
        mode: '0755'
      loop:
        - "{{ arch_root }}"
        - "{{ arch_root }}/sources"
        - "{{ arch_root }}/build"
        - "{{ arch_root }}/packages"
        - "{{ arch_root }}/tools"
        - "{{ arch_root }}/logs"
        - "{{ arch_root }}/patches"
        - "{{ build_home }}/makepkg"
        - "{{ build_home }}/makepkg/sources"
        - "{{ build_home }}/makepkg/srcpackages"
        - "{{ build_home }}/makepkg/packages"

    - name: Install Python build dependencies
      pip:
        name:
          - mako
          - jinja2
          - pyelftools
          - requests
        state: present

    - name: Copy build environment configuration
      blockinfile:
        path: "{{ build_home }}/.bashrc"
        block: "{{ lookup('file', 'files/bashrc-archbuild') }}"
        owner: "{{ build_user }}"
        group: "{{ build_user }}"
        create: yes

    - name: Clone Arch Linux PKGBUILDs repository
      git:
        repo: https://github.com/archlinux/svntogit-packages.git
        dest: "{{ arch_root }}/sources/packages"
        depth: 1
      become_user: "{{ build_user }}"

    - name: Clone Arch Linux community PKGBUILDs
      git:
        repo: https://github.com/archlinux/svntogit-community.git
        dest: "{{ arch_root }}/sources/community"
        depth: 1
      become_user: "{{ build_user }}"

    - name: Copy makepkg configuration
      copy:
        src: files/makepkg-s390x.conf
        dest: "{{ build_home }}/.makepkg.conf"
        owner: "{{ build_user }}"
        group: "{{ build_user }}"
        mode: '0644'

    - name: Copy build scripts
      copy:
        src: "files/{{ item }}"
        dest: "{{ arch_root }}/tools/{{ item }}"
        owner: "{{ build_user }}"
        group: "{{ build_user }}"
        mode: '0755'
      loop:
        - build-package.sh
        - bootstrap-toolchain.sh

    - name: Copy screen configuration
      copy:
        src: files/screenrc
        dest: "{{ build_home }}/.screenrc"
        owner: "{{ build_user }}"
        group: "{{ build_user }}"
        mode: '0644'

    - name: Display setup summary
      debug:
        msg:
          - "z/VM s390x build environment configured successfully!"
          - "Build user: {{ build_user }}"
          - "Arch root: {{ arch_root }}"
          - "To start building:"
          - "  1. SSH as {{ build_user }}"
          - "  2. Run: {{ arch_root }}/tools/bootstrap-toolchain.sh"
          - "  3. Or build individual packages: {{ arch_root }}/tools/build-package.sh <package>"