name: MinGW

on:
  push:
  pull_request:

jobs:
  build:
    strategy:
      matrix:
        arch:
          - i686
          - x86_64
          - aarch64
          - armv7

    runs-on: ubuntu-20.04
    container:
      image: amd64/ubuntu:20.04
      options: --privileged

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Update packages
        run: apt update

      - name: Install packages
        run: DEBIAN_FRONTEND=noninteractive apt install -y
          build-essential
          cmake
          dos2unix
          git
          libarchive-tools
          pkg-config
          nsis
          p7zip-full
          unzip
          wget
          zip

      - name: Set up MinGW environment
        run: |
          mkdir /usr/llvm-mingw
          wget https://github.com/mstorsjo/llvm-mingw/releases/download/20211002/llvm-mingw-20211002-msvcrt-ubuntu-18.04-x86_64.tar.xz
          bsdtar --strip-components 1 -x -v -f llvm-mingw-20211002-msvcrt-ubuntu-18.04-x86_64.tar.xz -C /usr/llvm-mingw
          rm /usr/llvm-mingw/${{ matrix.arch }}-w64-mingw32/include
          cp -a /usr/llvm-mingw/generic-w64-mingw32/include /usr/llvm-mingw/${{ matrix.arch }}-w64-mingw32
          wget https://github.com/garglk/assets/raw/master/mingw-llvm/mingw-llvm-${{ matrix.arch }}.tar.xz
          bsdtar xfv mingw-llvm-${{ matrix.arch }}.tar.xz -C /usr/llvm-mingw
          wget -P /usr/llvm-mingw/bin https://github.com/garglk/assets/raw/master/mingw-llvm/${{ matrix.arch }}-w64-mingw32-pkg-config
          mkdir NSIS
          cd NSIS
          wget https://nsis.sourceforge.io/mediawiki/images/7/78/FontName-0.7.zip
          unzip FontName-0.7.zip
          7z x -bb3 FontName-0.7.exe
          mkdir -p /usr/share/nsis/Include
          mkdir -p /usr/share/nsis/Plugins/x86-ansi
          for inc in Include/*; do iconv -f CP1251 -t UTF8 ${inc} -o /usr/share/nsis/Include/$(basename ${inc}); done
          cp Plugins/FontName.dll /usr/share/nsis/Plugins/x86-ansi
          cd ..

      - name: Build
        run: |
          echo $(date +"%Y%m%d").$(echo $GITHUB_SHA | cut -b 1-7) > VERSION
          ./windows.sh -a ${{ matrix.arch }}

      - name: Upload installer
        uses: actions/upload-artifact@v2
        with:
          name: Gargoyle-installer-${{ matrix.arch }}
          path: gargoyle-*.exe
        if: ${{ matrix.arch == 'i686' || matrix.arch == 'x86_64' }}

      - name: Upload standalone
        uses: actions/upload-artifact@v2
        with:
          name: Gargoyle-standalone-${{ matrix.arch }}
          path: gargoyle-*.zip
