name: Build
on:
  push:
    branches:
      - '*'

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            env:
              OS: Linux

          - os: ubuntu-latest
            env:
              PLATFORM: mingw32
              DEFAULT_HOMEDIR: .
              OS: Windows

          - os: macos-latest
            env:
              OS: macOS

    env:
      MAKEFLAGS: -j4

    steps:
      - uses: actions/checkout@v2

      - name: setup
        if: ${{ runner.os == 'Linux' }}
        run: |
          sudo apt-get update; sudo apt install -yy mingw-w64 libgl1-mesa-dev libsdl2-dev libfreetype6-dev libxxf86vm-dev

      - name: build
        run: |
          make all
          make all ARCHIVE=1 -j1
          cd build
          version=$(git show -s --pretty=format:%ad-%h --date=short)
          for file in *.zip; do
            bname=$(basename "$file" .zip)
            bname=${bname/mingw32-/windows}
            bname=${bname/darwin-universal/macOS}
            bname=${bname/linux-/linux}
            bname=${bname/x86_64/64}
            bname=${bname/x86/32}
            mv "$file" "ioq3urt-m9-${bname}-${version}.zip"
          done
        env: ${{ matrix.env }}

      - uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.env.OS }}
          path: build/*.zip

      - uses: google-github-actions/upload-cloud-storage@main
        if: ${{ github.ref == 'refs/heads/urt' }}
        with:
          credentials: ${{ secrets.gcp_credentials }}
          path: build
          destination: builds.iourtm9.tk/builds/build-${{ github.run_number }}
          parent: false
          glob: '*.zip'
