name: build

on:
    push:
      paths-ignore:
      - '**.md'
      - 'circuit'


# env:
#   UPLOAD_USER_FIRMWARE: false


jobs:
  build:
    runs-on: ubuntu-20.04
    continue-on-error: false
    strategy:
      matrix:
        target-hardware: [esp8266, esp32, esp32c3]


    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: before_install
      if: matrix.target-hardware == 'esp8266'
      run: |
        sudo apt update
        sudo apt install -y gcc git wget make libncurses-dev flex bison python python-setuptools python3-serial ninja-build
        wget https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
        curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
        python get-pip.py

    - name: install
      if: matrix.target-hardware == 'esp8266'
      run: |
        tar -xzf ./xtensa-lx106-elf-linux64-1.22.0-100-ge567ec7-5.2.0.tar.gz
        python -m pip install --user -r ./ESP8266_RTOS_SDK/requirements.txt

    - name: script
      id: script
      if: matrix.target-hardware == 'esp8266'
      run: |
        export IDF_PATH=$PWD/ESP8266_RTOS_SDK
        export PATH="$PATH:$PWD/xtensa-lx106-elf/bin"
        echo "FIRMWARE=$PWD/build" >> $GITHUB_ENV
        python ./idf.py fullclean
        python ./idf.py build
        echo "::set-output name=status::success"

    # - name: Generate release tag
    #   id: tag
    #   if: env.UPLOAD_USER_FIRMWARE == 'true' && steps.script.outputs.status == 'success' && !cancelled()
    #   run: |
    #     echo "::set-output name=release_tag::UserBuild_$(date +"%Y.%m.%d_%H-%M")"
    #     echo "::set-output name=status::success"


    - name: Build for esp32/esp32c3
      if: matrix.target-hardware != 'esp8266'
      uses: espressif/esp-idf-ci-action@v1
      with:
        esp_idf_version: v4.4.2
        target: ${{ matrix.target-hardware }}
        path: './'


    - name: Merge bin files (esp8266)
      if: matrix.target-hardware == 'esp8266'
      run: |
        git clone https://github.com/espressif/esptool.git
        git -C ./esptool/ checkout tags/v4.6.2 -b merge_wirless_bin
        sudo python3 ./esptool/esptool.py --chip ${{ matrix.target-hardware }} merge_bin  -o build/wireless_esp_dap_full.bin 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/wireless_esp_dap.bin
        sudo mv build/wireless_esp_dap.bin build/wireless_esp_dap_app.bin

    - name: Merge bin files (esp32)
      if: matrix.target-hardware == 'esp32'
      run: |
        git clone https://github.com/espressif/esptool.git
        git -C ./esptool/ checkout tags/v4.6.2 -b merge_wirless_bin
        sudo python3 ./esptool/esptool.py --chip ${{ matrix.target-hardware }} merge_bin  -o build/wireless_esp_dap_full.bin 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/wireless_esp_dap.bin
        sudo mv build/wireless_esp_dap.bin build/wireless_esp_dap_app.bin

    - name: Merge bin files (esp32c3)
      if: matrix.target-hardware == 'esp32c3'
      run: |
        git clone https://github.com/espressif/esptool.git
        git -C ./esptool/ checkout tags/v4.6.2 -b merge_wirless_bin
        sudo python3 ./esptool/esptool.py --chip ${{ matrix.target-hardware }} merge_bin  -o build/wireless_esp_dap_full.bin 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/wireless_esp_dap.bin
        sudo mv build/wireless_esp_dap.bin build/wireless_esp_dap_app.bin


    - name: Upload firmware
      uses: actions/upload-artifact@v2
      with:
        name: firmware_${{ matrix.target-hardware }}.zip
        path: |
          build/wireless_esp_dap_full.bin
          build/wireless_esp_dap_app.bin
          build/bootloader/bootloader.bin
          build/partition_table/partition-table.bin