- name: Setup FFX node
  hosts: all
  vars:
    ffxRepoUrl: https://gitea.maveno.de/Javanaut/ffx.git
  tasks:

    - name: Update system and install packages
      become: true
      when: ansible_os_family == 'Debian'
      ansible.builtin.apt:
        update_cache: true
        name:
          - python3-virtualenv
          - cpulimit
          - ffmpeg
          - git
          - screen

    - name: Update system and install packages
      become: true
      when: ansible_os_family == 'Archlinux'
      ansible.builtin.pacman:
        update_cache: true
        name:
          - cpulimit
          - ffmpeg
          - git
          - screen

    - name: Create sync dir
      become: true
      file:
        path: "{{ ffxHomeDirectory }}/.local/var/sync/ffx"
        state: directory
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0755

    - name: Ensure local etc directory
      become: true
      file:
        path: "{{ ffxHomeDirectory }}/.local/etc"
        state: directory
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0755

    - name: Ensure local src directory
      become: true
      file:
        path: "{{ ffxHomeDirectory }}/.local/src"
        state: directory
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0755

    - name: Ensure local share directory
      become: true
      file:
        path: "{{ ffxHomeDirectory }}/.local/share"
        state: directory
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0755


    - name: Clone ffx repository
      become: true
      become_user: "{{ ffxSystemUsername }}"
      ansible.builtin.git:
        repo: "{{ ffxRepoUrl }}"
        dest: "{{ ffxHomeDirectory }}/.local/src/ffx"
        version: dev


    - name: Install FFX package in venv 
      become: true
      become_user: "{{ ffxSystemUsername }}"
      ansible.builtin.pip:
        name: .
        chdir: "{{ ffxHomeDirectory }}/.local/src/ffx"
        virtualenv: "{{ ffxHomeDirectory }}/.local/share/ffx.venv"


    - name: Add TMDB API token placeholer to .bashrc
      become: true
      become_user: "{{ ffxSystemUsername }}"
      ansible.builtin.lineinfile:
        path: "{{ ffxHomeDirectory }}/.bashrc"
        insertbefore: BOF
        line: >-
          export TMDB_API_KEY="{{ ffxTmdbApiKey | default('<TMDB API key>') }}"

    - name: Add ffx alias to .bashrc
      become: true
      become_user: "{{ ffxSystemUsername }}"
      ansible.builtin.lineinfile:
        path: "{{ ffxHomeDirectory }}/.bashrc"
        insertbefore: BOF
        line: >-
          alias ffx="{{ ffxHomeDirectory }}/.local/share/ffx.venv/bin/ffx


    - name: Ensure local sync directory
      become: true
      file:
        path: "{{ ffxHomeDirectory }}/.local/var/sync/ffx"
        state: directory
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0755

    - name: Create ffx config file
      become: true
      become_user: "{{ ffxSystemUsername }}"
      vars:
        ffxConfiguration:
          databasePath: "{{ ffxHomeDirectory }}/.local/var/sync/ffx/ffx.db"
          metadata:
            signature:
              RECODED_WITH: FFX
            remove:
              - VERSION-eng
              - creation_time
              - NAME
            streams:
              remove:
                - BPS
                - NUMBER_OF_FRAMES
                - NUMBER_OF_BYTES
                - _STATISTICS_WRITING_APP
                - _STATISTICS_WRITING_DATE_UTC
                - _STATISTICS_TAGS
                - BPS-eng
                - DURATION-eng
                - NUMBER_OF_FRAMES-eng
                - NUMBER_OF_BYTES-eng
                - _STATISTICS_WRITING_APP-eng
                - _STATISTICS_WRITING_DATE_UTC-eng
                - _STATISTICS_TAGS-eng
      ansible.builtin.copy:
        content: "{{ ffxConfiguration | to_json }}"
        dest: "{{ ffxHomeDirectory }}/.local/etc/ffx.json"
        owner: "{{ ffxSystemUsername }}"
        group: "{{ ffxSystemUsername }}"
        mode: 0644
