- name: Set apt default-release to "testing"
  become: yes
  copy:
    src: "{{ role_path }}/files/20-tum.conf"
    dest: "/etc/apt/apt.conf.d/20-tum.conf"

- name: Add unstable repository
  become: yes
  ansible.builtin.apt_repository:
    repo: "deb http://deb.debian.org/debian unstable main contrib non-free-firmware"
    state: present
    update_cache: true

- name: Install build dependencies
  become: yes
  apt:
    state: latest
    install_recommends: false
    name:
      # General
      - build-essential
      - cmake
      - meson
      - checkinstall
      # Aquamarine
      - hwdata
      - libdrm-dev
      - libseat-dev
      - libinput-dev
      - libgbm-dev
      - libdisplay-info-dev
      - libegl-dev
      - libgles2-mesa-dev
      - wayland-protocols
      - libwayland-dev
      # hyprland-scanner
      - libpugixml-dev
      # hyprcursor
      - libzip-dev
      - librsvg2-dev
      - libxcb-util-dev
      # hyprgraphics
      - libjxl-dev
      - libmagic-dev
      - libspng-dev
      # Hyprland
      - libpango1.0-dev
      - libtomlplusplus-dev
      - uuid-dev
      - libxcb-errors-dev
      - libxcursor-dev
      - libxkbcommon-dev
      - libxcb1-dev
      - libxcb-icccm4-dev
      - libxcb-xfixes0-dev
      - libxcb-composite0-dev
      - libxcb-res0-dev
      - xwayland
      - libzip4t64
      - libre2-dev
      # xdg-desktop-portal-hyprland
      - qt6-base-dev
      - libspa-0.2-dev
      - libpipewire-0.3-dev
      - libsdbus-c++-dev
      # hyprland-qt-support
      - qt6-declarative-dev
      # hyprland-qtutils
      - qt6-base-private-dev
      - qt6-wayland-dev
      - qt6-wayland-private-dev

- name: Create source folders
  become: yes
  file:
    path: "/usr/local/src/{{ item }}"
    state: directory
    owner: "{{ ansible_user_id }}"
    group: "{{ ansible_user_id }}"
  loop:
    - "hyprutils"
    - "hyprwayland-scanner"
    - "Hyprland"
    - "hyprlang"
    - "hyprcursor"
    - "xdg-desktop-portal-hyprland"
    - "Aquamarine"
    - "hyprgraphics"
    - "hyprland-qt-support"
    - "hyprland-qtutils"

- name: Checkout required source repositories
  ansible.builtin.git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: "{{ item.version }}"
    force: true
    recursive: true
    depth: 1
  loop:
    - repo: "https://github.com/hyprwm/hyprutils.git"
      version: "v0.7.1"
      dest: "/usr/local/src/hyprutils"
    - repo: "https://github.com/hyprwm/hyprwayland-scanner.git"
      version: "v0.4.4"
      dest: "/usr/local/src/hyprwayland-scanner"
    - repo: "https://github.com/hyprwm/hyprlang.git"
      version: "v0.6.3"
      dest: "/usr/local/src/hyprlang"
    - repo: "https://github.com/hyprwm/hyprcursor.git"
      version: "v0.1.12"
      dest: "/usr/local/src/hyprcursor"
    - repo: "https://github.com/hyprwm/xdg-desktop-portal-hyprland.git"
      version: "v1.3.9"
      dest: "/usr/local/src/xdg-desktop-portal-hyprland"
    - repo: "https://github.com/hyprwm/aquamarine.git"
      version: "v0.8.0"
      dest: "/usr/local/src/Aquamarine"
    - repo: "https://github.com/hyprwm/hyprgraphics.git"
      version: "v0.1.3"
      dest: "/usr/local/src/hyprgraphics"
    - repo: "https://github.com/hyprwm/Hyprland.git"
      version: "v0.49.0"
      dest: "/usr/local/src/Hyprland"
    - repo: "https://github.com/hyprwm/hyprland-qt-support.git"
      version: "v0.1.0"
      dest: "/usr/local/src/hyprland-qt-support"
    - repo: "https://github.com/hyprwm/hyprland-qtutils.git"
      version: "v0.1.4"
      dest: "/usr/local/src/hyprland-qtutils"

- name: Build hyprutils
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprutils
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./build"
    - "cmake --build ./build --config Release --target all --parallel 4"

- name: Install hyprutils
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprutils

- name: Build hyprwayland-scanner
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprwayland-scanner
  loop:
    - "cmake -DCMAKE_INSTALL_PREFIX=/usr -B build"
    - "cmake --build build --parallel 4"

- name: Install hyprwayland-scanner
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprwayland-scanner

- name: Build hyprlang
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprlang
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B build"
    - "cmake --build build --config Release --target hyprlang --parallel 4"

- name: Install hyprlang
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprlang

- name: Build hyprcursor
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprcursor
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./build"
    - "cmake --build build --config Release --target all --parallel 4"

- name: Install hyprcursor
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprcursor

- name: Build Aquamarine
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/Aquamarine
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./build"
    - "cmake --build ./build --config Release --target all --parallel 4"

- name: Install Aquamarine
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/Aquamarine

- name: Build hyprgraphics
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprgraphics
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./build"
    - "cmake --build ./build --config Release --target all --parallel 4"

- name: Install hyprgraphics
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprgraphics

- name: Build Hyprland
  community.general.make:
    chdir: /usr/local/src/Hyprland
    target: all
    jobs: 4

- name: Install Hyprland
  become: true
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo make install"
    chdir: /usr/local/src/Hyprland

- name: Build xdg-desktop-portal-hyprland
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/xdg-desktop-portal-hyprland
  loop:
    - "cmake -DCMAKE_INSTALL_LIBEXECDIR=/usr/lib -DCMAKE_INSTALL_PREFIX=/usr -B build"
    - "cmake --build build --parallel 4"

- name: Install xdg-desktop-portal-hyprland
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/xdg-desktop-portal-hyprland

- name: Build hyprland-qt-support
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprland-qt-support
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -DINSTALL_QML_PREFIX=lib/x86_64-linux-gnu/qt6/qml -S . -B ./build"
    - "cmake --build ./build --config Release --target all --parallel 4"

- name: Install hyprland-qt-support
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprland-qt-support

- name: Build hyprland-qtutils
  ansible.builtin.command:
    cmd: "{{ item }}"
    chdir: /usr/local/src/hyprland-qtutils
  loop:
    - "cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./build"
    - "cmake --build ./build --config Release --target all --parallel 4"

- name: Install hyprland-qtutils
  become: yes
  ansible.builtin.command:
    cmd: "checkinstall -y -D sudo cmake --install build"
    chdir: /usr/local/src/hyprland-qtutils

- name: Reload systemd
  ansible.builtin.systemd_service:
    daemon_reload: true
    scope: user
