name: "Create release artifacts"
on:
  push:
    tags:
      - v[0-9]*

jobs:
  windows:
    runs-on: windows-2019
    steps:
      - uses: actions/checkout@v2
      - name: Get version from tag
        shell: bash
        run: | # Turn "refs/tags/vX.Y.Z" into "X.Y.Z"
          VERSION="${{ github.ref }}"
          echo "version=${VERSION##*/v}" >> $GITHUB_ENV
      - name: Get zlib, libpng and bison
        run: | # TODO: use an array
          $wc = New-Object System.Net.WebClient
          $wc.DownloadFile('https://www.zlib.net/zlib1211.zip', 'zlib.zip')
          $hash = (Get-FileHash "zlib.zip" -Algorithm SHA256).Hash
          if ($hash -ne 'd7510a8ee1918b7d0cad197a089c0a2cd4d6df05fee22389f67f115e738b178d') {
            Write-Host "zlib SHA256 mismatch! ($hash)"
            exit 1
          }
          $wc.DownloadFile('https://download.sourceforge.net/libpng/lpng1637.zip', 'libpng.zip')
          $hash = (Get-FileHash "libpng.zip" -Algorithm SHA256).Hash
          if ($hash -ne '3b4b1cbd0bae6822f749d39b1ccadd6297f05e2b85a83dd2ce6ecd7d09eabdf2') {
            Write-Host "libpng SHA256 mismatch! ($hash)"
            exit 1
          }
          $wc.DownloadFile('https://github.com/lexxmark/winflexbison/releases/download/v2.5.23/win_flex_bison-2.5.23.zip', 'winflexbison.zip')
          $hash = (Get-FileHash "winflexbison.zip" -Algorithm SHA256).Hash
          if ($hash -ne '6AA5C8EA662DA1550020A5804C28BE63FFAA53486DA9F6842E24C379EC422DFC') {
            Write-Host "bison SHA256 mismatch! ($hash)"
          }
          Expand-Archive -DestinationPath .           "zlib.zip"
          Expand-Archive -DestinationPath .           "libpng.zip"
          Expand-Archive -DestinationPath install_dir "winflexbison.zip"
          Move-Item zlib-1.2.11 zlib
          Move-Item lpng1637 libpng
      - name: Build 32-bit zlib
        run: | # BUILD_SHARED_LIBS causes the output DLL to be correctly called `zlib1.dll`
          cmake -S zlib -B zbuild32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
          cmake --build zbuild32 --config Release
          cmake --install zbuild32
      - name: Build 32-bit libpng
        run: |
          cmake -S libpng -B pngbuild32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DPNG_SHARED=ON -DPNG_STATIC=ON -DPNG_TESTS=OFF
          cmake --build pngbuild32 --config Release
          cmake --install pngbuild32
      - name: Build 32-bit Windows binaries
        run: |
          cmake -S . -B build32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Release
          cmake --build build32 --config Release
          cmake --install build32
      - name: Package 32-bit binaries
        run: |
          Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/zlib1.dll", "install_dir/bin/libpng16.dll") "rgbds-${{ env.version }}-win32.zip"
      - name: Build 64-bit zlib
        run: | # BUILD_SHARED_LIBS causes the output DLL to be correctly called `zlib1.dll`
          cmake -S zlib -B zbuild64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
          cmake --build zbuild64 --config Release
          cmake --install zbuild64
      - name: Build 64-bit libpng
        run: |
          cmake -S libpng -B pngbuild64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DPNG_SHARED=ON -DPNG_STATIC=ON -DPNG_TESTS=OFF
          cmake --build pngbuild64 --config Release
          cmake --install pngbuild64
      - name: Build 64-bit Windows binaries
        run: |
          cmake -S . -B build64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Release
          cmake --build build64 --config Release
          cmake --install build64
      - name: Package 64-bit binaries
        run: |
          Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/zlib1.dll", "install_dir/bin/libpng16.dll") "rgbds-${{ env.version }}-win64.zip"
      - name: Package sources
        run: |
          make dist
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          body: |
            Please ensure that the three assets below work properly.
            Once that's done, replace this text with the changelog, un-draft the release, and update the `release` branch.
            By the way, if you forgot to update `include/version.h`, RGBASM's version test is gonna fail in the tag's regression testing! (Use `git push --delete origin <tag>` to delete it)
          draft: true # Don't publish the release quite yet...
          prerelease: ${{ contains(github.ref, '-rc') }}
          files: |
            rgbds-${{ env.version }}-win32.zip
            rgbds-${{ env.version }}-win64.zip
            rgbds-${{ env.version }}.tar.gz
          fail_on_unmatched_files: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
