name: Windows build

on:
  push:
    tags:
      - 'latest'
      - 'v*'
    branches-ignore:
      - master

jobs:
  build:
    strategy:
      matrix:
        include:
         - { version: MINGW32, mingw: mingw32, arch: win32, env: i686,   ipf_win_file: ipflib42_w32.zip,     ipf_dll: ipflib42_w32/CAPSImg.dll, ipf_license: ipflib42_w32/LICENSE.txt }
         - { version: MINGW64, mingw: mingw64, arch: win64, env: x86_64, ipf_win_file: ipflib42_win_x64.zip, ipf_dll: CAPSImg_x64.dll,          ipf_license: LICENSE.txt }
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v2
      - uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.version }}
          update: true
          install: >-
            ctags
            gcc
            git
            make
            zip
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-diffutils
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-freetype
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-libpng
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-pkg-config
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-SDL2
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-toolchain
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-zlib
            ${{matrix.mingw}}/mingw-w64-${{matrix.env}}-7zip
      # This is awful but we have to pass the mingw{32,64}/include directory so that
      # zlib.h can be found because pkg-config doesn't return any cflags for
      # zlib.
      - name: Build caprice
        shell: msys2 {0}
        run: |
          make ARCH=${{matrix.arch}} CXX=g++ WINE=\"\" CFLAGS="-ID:/a/_temp/msys64/${{matrix.mingw}}/include/" debug -j 4 -k
          make ARCH=${{matrix.arch}} CXX=g++ WINE=\"\" CFLAGS="-ID:/a/_temp/msys64/${{matrix.mingw}}/include/" -j 4 clean
          make ARCH=${{matrix.arch}} CXX=g++ WINE=\"\" CFLAGS="-ID:/a/_temp/msys64/${{matrix.mingw}}/include/ -Ii686-linux-gnu-capsimage/include" -j 4 -k
          make ARCH=${{matrix.arch}} CXX=g++ WINE=\"\" CFLAGS="-ID:/a/_temp/msys64/${{matrix.mingw}}/include/ -Ii686-linux-gnu-capsimage/include" WINE=\"\" -j 4 -k unit_test
          make ARCH=${{matrix.arch}} CXX=g++ WINE=\"\" CFLAGS="-ID:/a/_temp/msys64/${{matrix.mingw}}/include/ -Ii686-linux-gnu-capsimage/include" WINE=\"\" -j 4 -k e2e_test
          ./test/integrated/test_win_package.sh ${{matrix.arch}}
          ./test/integrated/test_win_deps.sh ${{matrix.arch}}

      - name: Publish latest
        uses: softprops/action-gh-release@v2
        if: github.ref == 'refs/tags/latest'
        with:
          name: latest
          tag_name: latest
          message: Caprice32 latest
          body: Latest build of Caprice 32, with the newest cool features and the most recent bugs.
          files: release/cap32-${{matrix.arch}}.zip
          fail_on_unmatched_files: true
          draft: false
          prerelease: false
          make_latest: true
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          name: ${{ github.ref_name }}
          tag_name: ${{ github.ref_name }}
          message: Caprice32 ${{ github.ref }}
          body: Caprice32 ${{ github.ref }}. Release notes to be added manually.
          files: release/cap32-${{matrix.arch}}.zip
          fail_on_unmatched_files: true
          draft: false
          prerelease: false
          make_latest: true
          token: ${{ secrets.GITHUB_TOKEN }}
