name: build

on: [workflow_dispatch]

jobs:
  windows-msys:
    name: ${{ matrix.config }} Windows ${{ matrix.arch }}
    runs-on: windows-2019
    strategy:
      fail-fast: false
      matrix:
        arch: [x86_64]
        cc: [gcc]
        include:
          # - arch: x86
          #   msystem: MINGW32
          #   prefix: mingw-w64-i686

          - arch: x86_64
            msystem: MINGW64
            prefix: mingw-w64-x86_64

    defaults:
      run:
        shell: msys2 {0}

    steps:
    - uses: msys2/setup-msys2@v2
      with:
        install: ${{ matrix.prefix }}-qt5-base ${{ matrix.prefix }}-libxml2 git
        msystem: ${{ matrix.msystem }}
        path-type: minimal
        release: false
        update: false

    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build
      run: |
        make DOWNLOAD_GAMEPACKS=no MAKEFILE_CONF=msys2-Makefile.conf BUILD=release -j4
        wget https://www.dropbox.com/s/b1xpajzfa6yjlzf/netradiant-custom-extra-gamepacks.zip
        unzip -o netradiant-custom-extra-gamepacks.zip -d "install"
        mkdir -p install/settings
        echo > install/settings/DUMMMY

    - uses: actions/upload-artifact@v4
      with:
        name: Windows-${{ matrix.arch }}
        path: install/
        if-no-files-found: error

  linux:
    name: ${{ matrix.config }} Linux ${{ matrix.arch }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        arch: [x86_64]

    steps:
    - name: Install tools
      run: |
          sudo apt-get -qq update
          sudo apt-get -y install mesa-common-dev qtbase5-dev
    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Build
      run: |
        make DOWNLOAD_GAMEPACKS=no CXXFLAGS="-Wno-deprecated-copy" -j4
        wget https://www.dropbox.com/s/b1xpajzfa6yjlzf/netradiant-custom-extra-gamepacks.zip
        unzip -o netradiant-custom-extra-gamepacks.zip -d "install"
        7z a Linux-${{ matrix.arch }}.7z ./install/*
#       7z to preserve file permissions

    - uses: actions/upload-artifact@v4
      with:
        name: Linux-${{ matrix.arch }}
        path: Linux-${{ matrix.arch }}.7z
        if-no-files-found: error

  create-testing:
    if: github.ref == 'refs/heads/master' && ( github.event_name == 'push' || github.event_name == 'workflow_dispatch' )
    needs: [windows-msys, linux]
    runs-on: ubuntu-latest
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v4

      - name: Create binary archives
        shell: bash
        run: |
          7z a netradiant-custom-$(date +%Y%m%d)-windows-x86_64.zip ./Windows-x86_64/*
          mv Linux-x86_64/Linux-x86_64.7z netradiant-custom-$(date +%Y%m%d)-linux-x86_64.7z
#         note: dir/* doesn't match .bla (hidden) file in linux under dir/ but matches in subfolders

      - name: Create latest build
        uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          automatic_release_tag: "latest"
          prerelease: false
          title: Latest Build
          files: |
            *.zip
            *.7z
