on:
  create:
    tags:
      - '[0-9]+'

name: Draft Release

jobs:
  draft_release:
    name: Create Release
    runs-on: ubuntu-latest
    continue-on-error: false
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      tag_name: ${{ steps.tag_name.outputs.tag_name }}
      version: ${{ steps.changelog.outputs.version }}
    steps:
      - uses: actions/checkout@v3

      - name: Generate body from ChangeLog
        id: changelog
        run: |
          export VERSION=`echo "${{ github.ref }}" \
              | sed -e "s/refs\/tags\///"`
          ./.github/parse_changelog.py $VERSION ChangeLog >release.txt
          echo "version=${VERSION}" >> $GITHUB_OUTPUT

      - name: Create Release
        id: create_release
        uses: ncipollo/release-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          bodyFile: release.txt
          draft: true
          prerelease: false

  mxe32:
    name: Release 32-bit Windows Installer
    runs-on: ubuntu-latest
    continue-on-error: false
    needs: draft_release
    env:
      CFLAGS: -std=c11
      CXXFLAGS: -std=c++11
    steps:
      - uses: actions/checkout@v3

      - name: Add MXE Repository to Apt
        uses: myci-actions/add-deb-repo@4
        with:
          repo: deb http://pkg.mxe.cc/repos/apt bionic main
          repo-name: mxe
          keys: ${{ secrets.MXE_REPO_KEY }}
          key-server: keyserver.ubuntu.com

      - name: Install MXE Build Environment
        run: |
          sudo apt update
          sudo apt install \
            nasm \
            libtool \
            libtool-bin \
            gperf \
            lzip \
            p7zip-full \
            intltool \
            mxe-i686-w64-mingw32.static-cc \
            mxe-i686-w64-mingw32.static-cairo \
            mxe-i686-w64-mingw32.static-pango \
            mxe-i686-w64-mingw32.static-jpeg \
            mxe-i686-w64-mingw32.static-glew \
            mxe-i686-w64-mingw32.static-freetype \
            mxe-i686-w64-mingw32.static-curl \
            mxe-i686-w64-mingw32.static-librtmp \
            mxe-i686-w64-mingw32.static-ffmpeg \
            mxe-i686-w64-mingw32.static-sdl2
          echo /usr/lib/mxe/usr/bin >> $GITHUB_PATH  # exposes it to all future steps

      - name: Configure MXE for NSIS Installer Builds
        run: |
          (cd /usr/lib/mxe/ \
          && sudo make settings.mk \
          && sudo sed -i -e "s/SKIPPLUGINS='System'/SKIPPLUGINS=''/" src/nsis.mk \
          && sudo sed -i -e "s/.*MXE_TARGETS.*/MXE_TARGETS := i686-w64-mingw32.static/" settings.mk \
          && sudo make nsis)

      - name: Run MXE Build
        run: |
          i686-w64-mingw32.static-cmake .
          make
          sed -i -e "s/Lightspark Team/The Lightspark Developers/g" CPackConfig.cmake
          make package
          mv -v Lightspark*exe installer.exe

      - name: Upload Installer Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.draft_release.outputs.upload_url }}
          asset_path: installer.exe
          asset_name: Lightspark-v${{ needs.draft_release.outputs.version }}-Installer-win32.exe
          asset_content_type: application/octet-stream

  mxe64:
    name: Release 64-bit Windows Installer
    runs-on: ubuntu-latest
    continue-on-error: false
    needs: draft_release
    env:
      CFLAGS: -std=c11
      CXXFLAGS: -std=c++11
    steps:
      - uses: actions/checkout@v3

      - name: Add MXE Repository to Apt
        uses: myci-actions/add-deb-repo@4
        with:
          repo: deb http://pkg.mxe.cc/repos/apt bionic main
          repo-name: mxe
          keys: ${{ secrets.MXE_REPO_KEY }}
          key-server: keyserver.ubuntu.com

      - name: Install MXE Build Environment
        run: |
          sudo apt update
          sudo apt install \
            nasm \
            libtool \
            libtool-bin \
            gperf \
            lzip \
            p7zip-full \
            intltool \
            mxe-x86-64-w64-mingw32.static-cc \
            mxe-x86-64-w64-mingw32.static-cairo \
            mxe-x86-64-w64-mingw32.static-pango \
            mxe-x86-64-w64-mingw32.static-jpeg \
            mxe-x86-64-w64-mingw32.static-glew \
            mxe-x86-64-w64-mingw32.static-freetype \
            mxe-x86-64-w64-mingw32.static-curl \
            mxe-x86-64-w64-mingw32.static-librtmp \
            mxe-x86-64-w64-mingw32.static-ffmpeg \
            mxe-x86-64-w64-mingw32.static-sdl2
          echo /usr/lib/mxe/usr/bin >> $GITHUB_PATH  # exposes it to all future steps

      - name: Configure MXE for NSIS Installer Builds
        run: |
          (cd /usr/lib/mxe/ \
          && sudo make settings.mk \
          && sudo sed -i -e "s/SKIPPLUGINS='System'/SKIPPLUGINS=''/" src/nsis.mk \
          && sudo sed -i -e "s/.*MXE_TARGETS.*/MXE_TARGETS := x86_64-w64-mingw32.static/" settings.mk \
          && sudo make nsis)

      - name: Run MXE Build
        run: |
          x86_64-w64-mingw32.static-cmake .
          make
          sed -i -e "s/Lightspark Team/The Lightspark Developers/g" CPackConfig.cmake
          make package
          mv -v Lightspark*exe installer.exe

      - name: Upload Installer Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.draft_release.outputs.upload_url }}
          asset_path: installer.exe
          asset_name: Lightspark-v${{ needs.draft_release.outputs.version }}-Installer-win64.exe
          asset_content_type: application/octet-stream
