---
name: ci
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:

  delete_previous_release:
    name: Delete previous Development Release
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - uses: actions/checkout@v3
      - name: Delete Development Release
        uses: dev-drprasad/delete-tag-and-release@v0.2.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: latest
          github_token: ${{ secrets.GITHUB_TOKEN }}

  draft_release:
    name: Create Release
    runs-on: ubuntu-latest
    continue-on-error: false
    needs: delete_previous_release
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - uses: actions/checkout@v3
      - name: Create Release
        id: create_release
        uses: ncipollo/release-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          body: This is based on the latest commit.
          tag: latest
          name: "Development release"
          draft: false
          prerelease: true

  compilers:
    name: Compiler Compatibility / ${{ matrix.config.cc }}
    runs-on: ubuntu-20.04
    continue-on-error: true
    env:
      CC: ${{ matrix.config.cc }}
      CXX: ${{ matrix.config.cxx }}
      CXXPACKAGE: ${{ matrix.config.cxxpackage }}
      CFLAGS: -std=c11
      CXXFLAGS: -std=c++11
    strategy:
      matrix:
        config:
          - cc: clang-8
            cxx: clang++-8
            cxxpackage: clang-8
          - cc: clang-11
            cxx: clang++-11
            cxxpackage: clang-11
          - cc: gcc-7
            cxx: g++-7
            cxxpackage: g++-7
          - cc: gcc-10
            cxx: g++-10
            cxxpackage: g++-10
    steps:
      - uses: actions/checkout@v3.3.0

      - name: Install Dependencies
        run: |
          sudo `which apt || which apt-get` update
          sudo `which apt || which apt-get` install \
            curl \
            cmake \
            nasm \
            $CC \
            $CXXPACKAGE \
            gettext \
            libcurl4-gnutls-dev \
            libedit-dev \
            zlib1g-dev \
            libgl1-mesa-dev \
            libavutil-dev \
            libx11-dev \
            libglu1-mesa-dev \
            libglew-dev \
            libavcodec-dev \
            libavformat-dev \
            libswresample-dev \
            libcairo2-dev \
            libpango1.0-dev \
            libpng-dev \
            libjpeg-dev \
            librtmp-dev \
            libsdl2-dev \
            libgnutls28-dev \
            liblzma-dev

      - name: Run Build
        run: |
          export CXX=`echo $CC | sed -e 's/clang/clang++/;s/gcc/g++/'`
          ./build.sh debug
  linux:
    name: Linux / ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    continue-on-error: false
    strategy:
      matrix:
        os:
          - ubuntu-22.04
          - ubuntu-20.04
    steps:
      - uses: actions/checkout@v3.3.0
      - name: Install Dependencies
        run: |
          sudo `which apt || which apt-get` update
          sudo `which apt || which apt-get` install \
            curl \
            cmake \
            nasm \
            gettext \
            libcurl4-gnutls-dev \
            libedit-dev \
            zlib1g-dev \
            libgl1-mesa-dev \
            libavutil-dev \
            libx11-dev \
            libglu1-mesa-dev \
            libglew-dev \
            libavcodec-dev \
            libavformat-dev \
            libswresample-dev \
            libcairo2-dev \
            libpango1.0-dev \
            libpng-dev \
            libjpeg-dev \
            librtmp-dev \
            libsdl2-dev \
            libgnutls28-dev \
            liblzma-dev
      - name: Run Build
        run: ./build.sh

  mxe64:
    name: Windows 64-bit (MXE Cross Build)
    runs-on: ubuntu-latest
    continue-on-error: false
    needs: draft_release
    env:
      CFLAGS: -std=c11
      CXXFLAGS: -std=c++11
    steps:
      - uses: actions/checkout@v3.3.0

      - name: Add MXE Repository to Apt
        uses: myci-actions/add-deb-repo@10
        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

  mxe32:
    name: Windows 32-bit (MXE Cross Build)
    runs-on: ubuntu-latest
    continue-on-error: false
    needs: draft_release
    env:
      CFLAGS: -std=c11
      CXXFLAGS: -std=c++11
    steps:
      - uses: actions/checkout@v3.3.0

      - name: Add MXE Repository to Apt
        uses: myci-actions/add-deb-repo@10
        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
