name: Build & Deploy Engine
on: [push, pull_request]
jobs:
#  cleanup:
#    runs-on: self-hosted
#    steps:
#    - name: Cleanup
#      run: rm -rf .* || true
  build:
    runs-on: ${{ matrix.os }}
    continue-on-error: true
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux build specifically want oldest Ubuntu as possible
          # to be crossdistribution compatible, otherwise use ubuntu-latest
          - os: ubuntu-22.04
            targetos: linux
            targetarch: amd64
          - os: ubuntu-22.04
            targetos: linux
            targetarch: i386
          - os: ubuntu-22.04-arm
            targetos: linux
            targetarch: arm64
          - os: ubuntu-22.04-arm
            targetos: linux
            targetarch: armhf
          - os: ubuntu-24.04 # riscv64 would benefit from having latest compilers
            targetos: linux
            targetarch: riscv64
            cross: true
          - os: ubuntu-22.04
            targetos: linux
            targetarch: ppc64el
            cross: true
#          - os: ubuntu-latest
#            targetos: linux
#            targetarch: e2k-8c
#            cross: true
          - os: ubuntu-latest
            targetos: android
            targetarch: multiarch
#          - os: ubuntu-22.04
#            targetos: motomagx
#            targetarch: armv6

          - os: ubuntu-latest
            targetos: nswitch
            targetarch: arm64
          - os: ubuntu-latest
            targetos: psvita
            targetarch: armv7hf
          - os: windows-latest
            targetos: win32
            targetarch: amd64
          - os: windows-2022 # always use the oldest possible for 32-bit because of older compilers, and better support of certain legacy OSes
            targetos: win32
            targetarch: i386
          - os: macos-14 # arm64 as per github documentation
            targetos: apple
            targetarch: arm64
          - os: macos-latest
            targetos: ios
            targetarch: arm64
          - os: macos-15-intel
            targetos: apple
            targetarch: amd64
    env:
      SDL_VERSION: 2.32.10
      FFMPEG_VERSION: 8.1
      GH_CPU_ARCH: ${{ matrix.targetarch }}
      GH_CPU_OS: ${{ matrix.targetos }}
      GH_CROSSCOMPILING: ${{ matrix.cross }}
    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        submodules: recursive
    - name: Install dependencies
      run: bash scripts/gha/deps_${{ matrix.targetos }}.sh
    - name: Build engine
      env:
        FWGS_PFX_PASSWORD: ${{ secrets.FWGS_PFX_PASSWORD }}
      run: bash scripts/gha/build_${{ matrix.targetos }}.sh
    - name: Upload engine (artifacts)
      uses: actions/upload-artifact@v7
      with:
        name: artifact-${{ matrix.targetos }}-${{ matrix.targetarch }}
        path: artifacts/*
  flatpak:
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      matrix:
        include:
          - app: su.xash.Engine.Compat.i386
    container:
      image: ghcr.io/flathub-infra/flatpak-github-actions:freedesktop-25.08
      options: --privileged
    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        submodules: recursive
    - name: Build flatpak (Compat.i386)
      uses: FWGS/flatpak-github-actions/flatpak-builder@v6.5
      with:
        bundle: ${{ matrix.app }}.flatpak
        manifest-path: scripts/flatpak/${{ matrix.app }}.yml
  release:
    name: "Upload releases"
    runs-on: ubuntu-latest
    needs: [build, flatpak]
    if: ${{ github.event_name == 'push' }}
    steps:
    - name: Checkout
      uses: actions/checkout@v6
    - name: Remove old release, fetch artifacts, repackage binaries and upload new release
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        BASE_BUILDNUM_DATE: "2015-04-01"
      run: bash scripts/gha/make_release.sh "Xash3D FWGS" "${{ github.ref_name }}"
