name: Windows build

on:
  workflow_dispatch:
  push:
    branches:
      - master
      - fixes
      - develop
      - release/*
    tags:
      - 'v[0-9]+.*'
  pull_request:
    branches:
      - master
      - fixes
      - develop
      - release/*

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release


jobs:
  mosquitto:
    runs-on: windows-2022

    strategy:
      matrix:
        arch: [x86, x64]
        include:
          - arch: x86
            buildloc: build
            nsis: mosquitto.nsi
            platform: WIN32
            programfiles: "Program Files (x86)"
            triplet: x86-windows
          - arch: x64
            buildloc: build64
            nsis: mosquitto64.nsi
            platform: x64
            programfiles: "Program Files"
            triplet: x64-windows-release

    steps:
      - uses: actions/checkout@v6

      - name: vcpkg build
        uses: johnwason/vcpkg-action@v7
        id: vcpkg
        with:
          manifest-dir: ${{ github.workspace }}
          triplet: ${{ matrix.triplet }}
          token: ${{ github.token }}

      - name: Configure CMake
        run: |
          cmake -B ${{github.workspace}}/${{ matrix.buildloc }} `
            -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} `
            -DCMAKE_GENERATOR_PLATFORM=${{ matrix.platform }} `
            -DCMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake `
            -DHTTP_API_DIR="C:\\${{ matrix.programfiles }}\\Mosquitto\\dashboard" `
            -DVCPKG_MANIFEST_MODE=ON `
            -DVCPKG_TARGET_TRIPLET=${{ matrix.triplet }} `
            -DWITH_HTTP_API=ON `
            -DWITH_TESTS=OFF `
            -DWITH_WEBSOCKETS=ON

      - name: Build
        run: cmake --build ${{github.workspace}}/${{ matrix.buildloc }} --config ${{env.BUILD_TYPE}}

      - uses: suisei-cn/actions-download-file@v1.6.1
        id: vcredist
        name: Download VC redistributable
        with:
          url: https://aka.ms/vs/17/release/vc_redist.${{ matrix.arch }}.exe
          target: ${{github.workspace}}/installer/

      - name: Installer
        uses: joncloud/makensis-action@v5.0
        with:
          script-file: ${{github.workspace}}/installer/${{ matrix.nsis }}

      - name: Upload installer to artifacts
        uses: actions/upload-artifact@v6
        with:
          name: installer${{ matrix.arch }}
          path: ${{ github.workspace }}/installer/mosquitto*.exe
