name: MinGW

on: 
  push:
    paths-ignore:
      - '.gitignore'
      - '.gitattributes'
      - '**.cmd'
      - '**.md'
      - 'AUTHORS'
      - 'NEWS'
      - 'ChangeLog'
  pull_request:
    paths-ignore:
      - '.gitignore'
      - '.gitattributes'
      - '**.cmd'
      - '**.md'
      - 'AUTHORS'
      - 'NEWS'
      - 'ChangeLog'

env:
  WDK_URL: https://go.microsoft.com/fwlink/p/?LinkID=253170
  LIBUSB0_URL: https://github.com/mcuee/libusb-win32/releases/download/release_1.4.0.0/libusb-win32-bin-1.4.0.0.zip
  LIBUSBK_URL: https://github.com/mcuee/libusbk/releases/download/V3.1.0.0/libusbK-3.1.0.0-bin.7z
  BUILD_OPTIONS: '--enable-toggable-debug --enable-examples-build --disable-debug --disable-shared'
  DRIVERS_PATHS: '--with-wdkdir="wdk/Windows Kits/8.0" --with-wdfver=1011 --with-libusb0="libusb0" --with-libusbk="libusbk/bin"'

jobs:
  MinGW-Build:
    runs-on: windows-latest

    strategy:
      matrix:
        include:
          - { sys: mingw64, env: x86_64 }
          - { sys: mingw32, env: i686 }

    defaults:
      run:
        shell: msys2 {0}

    steps:
    - name: Install MinGW
      uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.sys }}
        update: true
        install: >-
          mingw-w64-${{ matrix.env }}-toolchain
          base-devel
          autotools
          git
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
        submodules: recursive
    - name: Download support files
      shell: cmd
      run: |
        curl -L ${{ env.WDK_URL }} -o wdk-redist.msi
        curl -L ${{ env.LIBUSB0_URL }} -o libusb0-redist.zip
        curl -L ${{ env.LIBUSBK_URL }} -o libusbk-redist.7z
        msiexec /a wdk-redist.msi /qn TARGETDIR=%CD%\wdk
        7z x libusb0-redist.zip
        7z x libusbk-redist.7z
        del *.zip
        del *.7z
        move libusb-win32* libusb0
        move libusbK* libusbk
    - name: Build
      run: |
        ./bootstrap.sh
        [ "${{ matrix.env }}" == "x86_64" ] && EXTRA_OPTION=--disable-32bit
        ./configure --build=${{ matrix.env }}-w64-mingw32 --host=${{ matrix.env }}-w64-mingw32 ${{ env.BUILD_OPTIONS }} $EXTRA_OPTION ${{ env.DRIVERS_PATHS }}
        make
        mkdir -p artifacts/${{ matrix.env }}
        mv examples/*.exe artifacts/${{ matrix.env }}
    - name: Display SHA-256
      if: ${{ github.event_name == 'push' }}
      run: sha256sum artifacts/${{ matrix.env }}/*.exe
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      if: ${{ github.event_name == 'push' }}
      with:
        name: ${{ matrix.sys }}
        path: ./artifacts/*/*.exe

  Merge-Artifacts:
    runs-on: windows-latest
    needs: MinGW-Build
    steps:
      - name: Merge Artifacts
        uses: actions/upload-artifact/merge@v4
        if: ${{ github.event_name == 'push' }}
        with:
          name: MinGW
          delete-merged: true