- hosts: m-server.home.lan
  name: Build tasmota
  ignore_unreachable: false
  # vars:
  #   DOCKER_IMAGE: docker-tasmota
  #   FWS: tasmota
  become: true
  tasks:
    - name: Change conf
      community.general.git_config:
        name: safe.director
        scope: global
        value: /share/docker_data/docker-tasmota/Tasmota

    # - name: Pull tasmota
    #   ansible.builtin.shell:
    #     cmd: 'git config --global --add safe.directory /share/docker_data/docker-tasmota/Tasmota'
    - name: Remove existing directory
      ansible.builtin.file:
        path: /share/docker_data/docker-tasmota/Tasmota
        state: absent
        
    - name: Checkout a github repo and use refspec to fetch all pull requests
      ansible.builtin.git:
        repo: 'https://github.com/arendst/Tasmota.git'
        dest: /share/docker_data/docker-tasmota/Tasmota
        version: '{{ BRANCH }}'

    # - name: Fetch tasmota
    #   ansible.builtin.shell:
    #     cmd: 'git fetch https://github.com/arendst/Tasmota.git {{ BRANCH }}'
    #     chdir: /share/docker_data/docker-tasmota/Tasmota


    - name: Git checkout
      ansible.builtin.git:
        repo: 'https://github.com/arendst/Tasmota.git'
        dest: /share/docker_data/docker-tasmota/Tasmota
        version: '{{ BRANCH }}'

    # - name: Checkout tasmota branch
    #   ansible.builtin.shell:
    #     cmd: 'git checkout --force {{ BRANCH }}'
    #     chdir: /share/docker_data/docker-tasmota/Tasmota
    - name: Just get information about the repository whether or not it has already been cloned locally
      ansible.builtin.git:
        repo: https://github.com/arendst/Tasmota.git
        dest: /share/docker_data/docker-tasmota/Tasmota
        update: true

    # - name: Pull tasmota
    #   ansible.builtin.shell:
    #     cmd: 'git pull'
    #     chdir: /share/docker_data/docker-tasmota/Tasmota

    - name: Copy platformio_override
      ansible.builtin.command:
        cmd: 'cp platformio_override.ini Tasmota/platformio_override.ini'
        chdir: /share/docker_data/docker-tasmota/
      register: my_output
      changed_when: my_output.rc != 0

    - name: Copy user_config_override
      ansible.builtin.command:
        cmd: 'cp user_config_override.h Tasmota/tasmota/user_config_override.h'
        chdir: /share/docker_data/docker-tasmota/
      register: my_output
      changed_when: my_output.rc != 0

    - name: Build tasmota
      ansible.builtin.command:
        cmd: 'docker run --rm -v /share/docker_data/docker-tasmota/Tasmota:/tasmota -u 0:0 {{ DOCKER_IMAGE }} -e {{ FWS }}'
        chdir: /share/docker_data/docker-tasmota/
      when: FWS != "all"
      register: my_output
      changed_when: my_output.rc != 0

    - name: Build tasmota
      ansible.builtin.command:
        cmd: 'docker run --rm -v /share/docker_data/docker-tasmota/Tasmota:/tasmota -u 0:0 {{ DOCKER_IMAGE }}'
        chdir: /share/docker_data/docker-tasmota/
      when: FWS == "all"
      register: my_output
      changed_when: my_output.rc != 0

    - name: Create a directory if it does not exist
      ansible.builtin.file:
        path: /share/docker_data/webhub/fw/{{ BRANCH }}
        state: directory
        mode: '0755'

    - name: Build tasmota
      ansible.builtin.shell:
        cmd: 'mv /share/docker_data/docker-tasmota/Tasmota/build_output/firmware/* /share/docker_data/webhub/fw/{{ BRANCH }}/'
