name: Build App
on:
  push:
    branches:
      - '**'
  pull_request:
    branches: 
      - '**'

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  APP_NAME: ${{ github.event.repository.name }}

jobs:
  refs:
    name: Prepare CI Environment
    runs-on: ubuntu-22.04
    outputs:
      sha_short: ${{ steps.refs.outputs.sha_short }}
      short_ref: ${{ steps.refs.outputs.short_ref }}
      version: ${{ steps.refs.outputs.version }}
      new_release: ${{ steps.refs.outputs.new_release }}
    steps:
      - name: Source checkout
        uses: actions/checkout@v4
      - name: Update SDKs if required
        run: make update-submodules
      - name: Semantic Release
        id: semantic
        uses: cycjimmy/semantic-release-action@v4
        with:
          dry_run: true
          semantic_version: 21
          extra_plugins: |
            @semantic-release/changelog
            @semantic-release/git
            conventional-changelog-conventionalcommits@7
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Set outputs
        id: refs
        run: |
          export BRANCH=${GITHUB_REF#refs/*/}
          echo "branch: ${BRANCH}"
          export BRANCHTRANSLATED=$(echo ${BRANCH} | tr '/' '_')
          echo "short_ref: ${BRANCHTRANSLATED}"
          export VERSION=""
          if ${{ steps.semantic.outputs.new_release_published == 'true' }}; then
            export VERSION=${{ steps.semantic.outputs.new_release_version }}
          else
            export VERSION=${BRANCHTRANSLATED}_$(git rev-parse --short=12 HEAD)
          fi
          echo "new_release=${{ steps.semantic.outputs.new_release_published }}" >> $GITHUB_OUTPUT
          echo "short_ref=${BRANCHTRANSLATED}" >> $GITHUB_OUTPUT
          echo "sha_short=SHA-$(git rev-parse --short=12 HEAD)" >> $GITHUB_OUTPUT
          echo "version=${VERSION}" >> $GITHUB_OUTPUT

  build2:
    name: Build Simulator
    needs: refs
    runs-on: windows-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Checkout berry submodule
      run: |
        git submodule update --init --recursive libraries/berry

    - name: Setup MSBuild
      uses: microsoft/setup-msbuild@v2

    - name: Checkout simulator repository
      run: |
        git clone https://github.com/openshwprojects/obkSimulator
        mkdir -p ./libs_for_simulator
        cp -r ./obkSimulator/simulator/libs_for_simulator/* ./libs_for_simulator

    - name: Build project
      run: msbuild openBeken_win32_mvsc2017.vcxproj /p:Configuration=Release /p:PlatformToolset=v143
    - name: Flatten build assets
      run: |
        mkdir -p flat
        cp ./Release/openBeken_win32.exe flat/
        cp ./obkSimulator/simulator/*.dll flat/
        cp ./run_*.bat flat/
        mkdir -p flat/examples
        cp -r ./obkSimulator/examples/* flat/examples/
    - name: Run unit tests
      run: |
        ./flat/openBeken_win32.exe -runUnitTests 2
    - name: Compress build assets
      run: |
        Compress-Archive -Path flat/* -DestinationPath obkSimulator_win32_${{ needs.refs.outputs.version }}.zip
    - name: Copy build assets
      run: |
        mkdir -Force output/${{ needs.refs.outputs.version }}
        cp obkSimulator_win32_${{ needs.refs.outputs.version }}.zip output/${{ needs.refs.outputs.version }}/obkSimulator_${{ needs.refs.outputs.version }}.zip
    - name: Upload build assets
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}_sim
        path: output/${{ needs.refs.outputs.version }}/obkSimulator_${{ needs.refs.outputs.version }}.zip

  build_asan:
    name: Build Linux Simulator and Run Tests With Address Sanitizer
    needs: refs
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Checkout berry submodule
      run: |
        git submodule update --init --recursive libraries/berry
        mkdir -p libraries/berry/generate
        ./libraries/berry/tools/coc/coc -o libraries/berry/generate libraries/berry/src src/berry/modules -c include/berry_conf.h

    - name: Build project
      run: ASAN=1 make -f custom.mk
    - name: Run unit tests
      run: |
        export LSAN_OPTIONS=suppressions=platforms/linux/asan.suppressions
        ./build/win_main -runUnitTests 2

  build_ubsan:
    name: Build Linux Simulator and Run Tests With Undefined Behavior Sanitizer
    needs: refs
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Checkout berry submodule
      run: |
        git submodule update --init --recursive libraries/berry
        mkdir -p libraries/berry/generate
        ./libraries/berry/tools/coc/coc -o libraries/berry/generate libraries/berry/src src/berry/modules -c include/berry_conf.h

    - name: Build project
      run: UBSAN=1 make -f custom.mk
    - name: Run unit tests
      run: |
        export UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1:suppressions=platforms/linux/ubsan.suppressions
        ./build/win_main -runUnitTests 2

  build:
    name: Build
    needs: refs
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        include:
          # default for all platforms
          - platform: OpenBK7231T
            variant: default
          - platform: OpenBK7231N
            variant: default
          - platform: OpenXR809
            variant: default
          - platform: OpenXR806
            variant: default
          - platform: OpenXR872
            variant: default
          - platform: OpenBL602
            variant: default
          - platform: OpenW800
            variant: default
          - platform: OpenW600
            variant: default
          - platform: OpenLN882H
            variant: default
          - platform: OpenTR6260
            variant: default
          - platform: OpenRTL8710A
            variant: default
          - platform: OpenRTL8710B
            variant: default
          - platform: OpenRTL87X0C
            variant: default
          - platform: OpenRTL8720D
            variant: default
          - platform: OpenECR6600
            variant: default

          # extras for BK7231T
          - platform: OpenBK7231T
            variant: berry
          - platform: OpenBK7231T
            variant: tuyaMCU
          - platform: OpenBK7231T
            variant: powerMetering
          - platform: OpenBK7231T
            variant: irRemoteESP
          - platform: OpenBK7231T
            variant: sensors

          # extras for BK7231N
          - platform: OpenBK7231N
            variant: berry
          - platform: OpenBK7231N
            variant: tuyaMCU
          - platform: OpenBK7231N
            variant: powerMetering
          - platform: OpenBK7231N
            variant: irRemoteESP
          - platform: OpenBK7231N
            variant: sensors

          - platform: OpenBL602
            variant: berry
    steps:
      - name: Source checkout
        uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.8'
          architecture: 'x64'
      - name: Install dependencies
        run: |
          sudo dpkg --add-architecture i386
          sudo apt-get update
          sudo apt-get -y install libc6-i386 make cmake
          pip3 install pycryptodomex
          pip3 install configobj
          pip3 install toml
          pip3 install fdt
      - run: ls -la
      - name: Run make
        run: make APP_VERSION=${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }} APP_NAME=${{ matrix.platform }} VARIANT="${{ matrix.variant }}" ${{ matrix.platform }}
      - name: Save build assets
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}_${{ matrix.platform }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}
          path: |
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_UA_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_UG_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_QIO_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/OpenBK7231M_QIO_${{ needs.refs.outputs.version }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.rbl
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.img
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.fls
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}_ota.img
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}_gz.img
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}_OTA.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}_OTA.bin.xz
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}_OTA.bin.xz.ota
          if-no-files-found: warn

  build_idf:
    name: Build IDF
    needs: refs
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: OpenESP32C2
            variant: 2M
          - platform: OpenESP32C3
            variant: 2M

          - platform: OpenESP32
            variant: 4M
          - platform: OpenESP32C2
            variant: 4M
          - platform: OpenESP32C3
            variant: 4M
          - platform: OpenESP32C6
            variant: 4M
          - platform: OpenESP32S3
            variant: 4M
          - platform: OpenESP32S2
            variant: 4M
    steps:
      - name: Source checkout
        uses: actions/checkout@v4
      - name: SDK checkout
        uses: actions/checkout@v4
        with:
          repository: espressif/esp-idf
          path: sdk/esp-idf
          ref: v5.3.3
          submodules: recursive
      - uses: actions/setup-python@v5
        with:
          python-version: '3.8'
          architecture: 'x64'
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get -y install make cmake python3-venv
          pip3 install pycryptodomex
          pip3 install configobj
          pip3 install toml
          pip3 install fdt
      - name: Run make
        run: |
          cd sdk/esp-idf
          ./install.sh > /dev/null
          . ./export.sh
          . ./add_path.sh
          cd ../..
          make APP_VERSION=${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }} APP_NAME=${{ matrix.platform }} VARIANT="${{ matrix.variant }}" ${{ matrix.platform }}
      - name: Save build assets
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}_${{ matrix.platform }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}
          path: |
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.img
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.factory.bin
          if-no-files-found: warn

  build_beken:
    name: Build beken alt
    needs: refs
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        include:
          # default for all platforms
          - platform: OpenBK7231N_ALT
            variant: default
          - platform: OpenBK7231T_ALT
            variant: default
          - platform: OpenBK7231U
            variant: default
          - platform: OpenBK7238
            variant: default
          - platform: OpenBK7252
            variant: default
          - platform: OpenBK7252N
            variant: default
          
          - platform: OpenBK7238
            variant: berry
    steps:
      - name: Source checkout
        uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.8'
          architecture: 'x64'
      - name: Install dependencies
        run: |
          sudo dpkg --add-architecture i386
          sudo apt-get update
          sudo apt-get -y install libc6-i386 make
          pip3 install pycryptodomex
          pip3 install configobj
          pip3 install toml
          pip3 install fdt
      - run: ls -la
      - name: Run make
        run: make APP_VERSION=${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }} APP_NAME=${{ matrix.platform }} VARIANT="${{ matrix.variant }}" ${{ matrix.platform }}
      - name: Save build assets
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}_${{ matrix.platform }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}
          path: |
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_UA_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_UG_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_QIO_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/OpenBK7231M_ALT_QIO_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.rbl
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_Tuya_QIO_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
            output/${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}/${{ matrix.platform }}_Tuya_UA_${{ needs.refs.outputs.version }}${{ matrix.variant != 'default' && '_' || '' }}${{ matrix.variant != 'default' && matrix.variant || '' }}.bin
          if-no-files-found: warn

  merge:
    name: Merge Artifacts
    runs-on: ubuntu-22.04
    needs: [refs, build, build2, build_idf, build_beken]
    #if: always() && needs.refs.outputs.new_release == 'true' && (github.ref == 'refs/heads/main' || github.ref_name == 'alpha')
    steps:
      - name: Merge Artifacts
        uses: actions/upload-artifact/merge@v4
        with:
          name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}
          #delete-merged: true

  release:
    name: Semantic Release Images and Artifacts
    runs-on: ubuntu-22.04
    needs: [merge, refs]
    if: always() && needs.refs.outputs.new_release == 'true' && (github.ref == 'refs/heads/main' || github.ref_name == 'alpha')
    steps:
      - name: Source checkout
        uses: actions/checkout@v4
      - name: Fetch build assets
        uses: actions/download-artifact@v4
        with:
          name: ${{ env.APP_NAME }}_${{ needs.refs.outputs.version }}
          path: output/${{ needs.refs.outputs.version }}
      - name: Run Semantic Release
        id: semantic
        uses: cycjimmy/semantic-release-action@v4
        with:
          dry_run: false
          semantic_version: 21
          extra_plugins: |
            @semantic-release/changelog
            @semantic-release/git
            conventional-changelog-conventionalcommits@7
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Echo Semantic Release Versions
        if: steps.semantic.outputs.new_release_published == 'true'
        run: |
          echo ${{ steps.semantic.outputs.new_release_version }}
          echo ${{ steps.semantic.outputs.new_release_major_version }}
          echo ${{ steps.semantic.outputs.new_release_minor_version }}
          echo ${{ steps.semantic.outputs.new_release_patch_version }}
