---
- name: Deploy FreeIPA package auto-installer
  hosts: all
  become: yes

  tasks:

    - name: Install script
      copy:
        src: ansipa-install-packages.sh
        dest: /usr/local/bin/ansipa-install-packages.sh
        mode: '0755'

    - name: Install systemd service
      copy:
        dest: /etc/systemd/system/ansipa-install.service
        mode: '0644'
        content: |
          [Unit]
          Description=Install packages based on FreeIPA dev_pkg_* groups
          After=network-online.target sssd.service

          [Service]
          Type=oneshot
          ExecStart=/usr/local/bin/ansipa-install-packages.sh

    - name: Install systemd timer
      copy:
        dest: /etc/systemd/system/ansipa-install.timer
        mode: '0644'
        content: |
          [Unit]
          Description=Periodic FreeIPA package sync

          [Timer]
          OnBootSec=2min
          OnUnitActiveSec=30min

          [Install]
          WantedBy=timers.target

    - name: Install Flatpak installer script
      copy:
        src: ansipa-install-flatpaks.sh
        dest: /usr/local/bin/ansipa-install-flatpaks.sh
        mode: '0755'

    - name: Install Flatpak systemd service
      copy:
        dest: /etc/systemd/system/ansipa-install-flatpaks.service
        mode: '0644'
        content: |
          [Unit]
          Description=Install Flatpaks based on FreeIPA dev_fp_* groups
          After=network-online.target sssd.service
          Wants=network-online.target

          [Service]
          Type=oneshot
          ExecStart=/usr/local/bin/ansipa-install-flatpaks.sh
          StandardOutput=journal
          StandardError=journal

    - name: Install Flatpak systemd timer
      copy:
        dest: /etc/systemd/system/ansipa-install-flatpaks.timer
        mode: '0644'
        content: |
          [Unit]
          Description=Periodic FreeIPA Flatpak sync

          [Timer]
          OnBootSec=4min
          OnUnitActiveSec=30min

          [Install]
          WantedBy=timers.target

    - name: Reload systemd
      command: systemctl daemon-reload

    - name: Enable and start package timer
      systemd:
        name: ansipa-install.timer
        enabled: yes
        state: started

    - name: Enable and start Flatpak timer
      systemd:
        name: ansipa-install-flatpaks.timer
        enabled: yes
        state: started
