name: MinGW

on:
  push:
    branches: ["master"]
    tags: ["4.*"]
  pull_request:
    branches: ["master"]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: build
    strategy:
      fail-fast: false
      matrix:
        os: ["windows"]
        # Customize the CMake build type here (`Release`, `Debug`, `RelWithDebInfo`, etc.)
        BUILD_TYPE: ["Release"]
        BUILD_SHARED_LIBS: ["OFF"]
    defaults:
      run:
        shell: cmd
    runs-on: ${{ matrix.os }}-latest
    env:
      BUILD_TYPE:  ${{ matrix.BUILD_TYPE }}
      SOURCE_DIR:  ${{ github.workspace }}\.cache\source
      TOOSL_DIR:   ${{ github.workspace }}\.cache\tools
      INSTALL_DIR: ${{ github.workspace }}\.cache\install_mingw_2022_02_15
    steps:
      - uses: actions/checkout@v6
            
      - name: Install dependencies
        run: |
          C:\msys64\usr\bin\pacman.exe -S --noconfirm ^
              mingw-w64-x86_64-cmake ^
              mingw-w64-x86_64-make ^
              mingw-w64-x86_64-nsis ^
              mingw-w64-x86_64-gcc ^
              mingw-w64-x86_64-zlib ^
              mingw-w64-x86_64-openssl ^
              mingw-w64-x86_64-libevent ^
              mingw-w64-x86_64-sqlite3 ^
              mingw-w64-x86_64-hiredis ^
              mingw-w64-x86_64-postgresql ^
              mingw-w64-x86_64-libmicrohttpd ^
              git base-devel
        env:
          PATH: C:\msys64\usr\bin

      - name: Create directories
        run: |
          cmake -E make_directory ${{ env.SOURCE_DIR }}
          cmake -E make_directory ${{ env.TOOSL_DIR }}
          cmake -E make_directory ${{ env.INSTALL_DIR }}
    
      - name: Cache installed
        uses: actions/cache@v5
        id: cache-installed
        with:
          path: ${{ env.INSTALL_DIR }}
          key: coturn-cache-installed-mingw

      - name: Build Prometheus
        run: |
          cd ${{ env.SOURCE_DIR }}
          git clone https://github.com/digitalocean/prometheus-client-c.git
          cd prometheus-client-c/prom
          mkdir build
          cd build
          cmake .. -G"MinGW Makefiles" ^
              -DBUILD_SHARED_LIBS=${{ matrix.BUILD_SHARED_LIBS }} ^
              -DCMAKE_BUILD_TYPE=${{ matrix.BUILD_TYPE }} ^
              -DCMAKE_INSTALL_PREFIX=${{ env.INSTALL_DIR }}
          cmake --build . --config ${{ matrix.BUILD_TYPE }}
          cmake --build . --config ${{ matrix.BUILD_TYPE }} --target install
        env:
          MSYSTEM: MINGW64
          PATH: C:\msys64\mingw64\bin;C:\msys64\usr\bin
        working-directory: ${{ env.SOURCE_DIR }}
        if: ${{ false }}
        
      - name: Build Coturn
        run: |
          cmake -E make_directory build
          cd build
          cmake .. -G"MinGW Makefiles" ^
              -DBUILD_SHARED_LIBS=${{ matrix.BUILD_SHARED_LIBS }} ^
              -DCMAKE_BUILD_TYPE=${{ matrix.BUILD_TYPE }} ^
              -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/build/install
          cmake --build . --config ${{ matrix.BUILD_TYPE }}
          cmake --build . --config ${{ matrix.BUILD_TYPE }} --target install
        env:
          MSYSTEM: MINGW64
          PATH: C:\msys64\mingw64\bin;C:\msys64\usr\bin
          Prometheus_ROOT: ${{ env.INSTALL_DIR }}
        working-directory: ${{ github.workspace }}

      - name: Package
        run: |
          copy /Y ${{ env.INSTALL_DIR }}\bin\*.dll install\bin
          copy /Y ${{ env.INSTALL_DIR }}\lib\*.dll install\bin
          copy /Y ${{ env.RUNVCPKG_VCPKG_ROOT }}\installed\${{ env.RUNVCPKG_VCPKG_TRIPLET_OUT }}\bin\*.dll install\bin
          7z a coturn_windows_mingw.zip ${{ github.workspace }}\build\install\*
          cmake --build . --config ${{ matrix.BUILD_TYPE }} --target package
        env:
          MSYSTEM: MINGW64
          PATH: C:\msys64\mingw64\bin;C:\msys64\usr\bin
        working-directory: ${{ github.workspace }}\build
        if: ${{ matrix.BUILD_TYPE == 'Release' }}

      - name: Update artifacts
        uses: actions/upload-artifact@v6
        with:
          name: coturn_mingw_${{ matrix.os }}
          path: |
            ${{ github.workspace }}\build\coturn_windows_mingw.zip
            ${{ github.workspace }}\build\coturn*.exe
            ${{ github.workspace }}\build\coturn*.md5
        if: ${{ matrix.BUILD_TYPE == 'Release' }}
