name: re3 conan+cmake build

on:
  workflow_dispatch:

jobs:
  build-cmake:
    strategy:
      matrix:
        include:
          - os: 'windows-latest'
            platform: 'gl3'
            gl3_gfxlib: 'glfw'
            audio: 'openal'
          # - os: 'windows-latest'
          #   platform: 'gl3'
          #   gl3_gfxlib: 'sdl2'
          #   audio: 'openal'
          - os: 'windows-latest'
            platform: 'd3d9'
            audio: 'openal'
          # - os: 'windows-latest'
          #   platform: 'd3d9'
          #   audio: 'miles'
          - os: 'ubuntu-22.04'  # Updated from 18.04 (EOL)
            platform: 'gl3'
            gl3_gfxlib: 'glfw'
            audio: 'openal'
          # - os: 'ubuntu-22.04'
          #   platform: 'gl3'
          #   gl3_gfxlib: 'sdl2'
          #   audio: 'openal'
          - os: 'macos-latest'
            platform: 'gl3'
            gl3_gfxlib: 'glfw'
            audio: 'openal'
          # - os: 'macos-latest'
          #   platform: 'gl3'
          #   gl3_gfxlib: 'sdl2'
          #   audio: 'openal'
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.platform == 'ps2' || matrix.gl3_gfxlib == 'sdl2' || matrix.audio == 'miles' }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Checkout Miles SDK
        if: ${{ matrix.audio == 'miles' }}
        uses: actions/checkout@v4
        with:
          repository: 'withmorten/re3mss'
          path: 're3mss'

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Set Xcode 11 (macOS only)
        if: runner.os == 'macOS'
        run: |
          sudo xcode-select --switch /Applications/Xcode_11.7.app

      - name: Configure Conan
        run: |
          python -m pip install conan
          conan config init
          conan config set log.print_run_commands=True
          conan config set general.revisions_enabled=1
          conan profile detect --force

      - name: Update Conan settings
        shell: python
        run: |
          import os, yaml
          settings_path = os.path.expanduser("~/.conan/settings.yml")
          with open(settings_path) as f:
            yml = yaml.safe_load(f)
          yml.setdefault("os", {}).setdefault("playstation2", None)
          if "3.2" not in yml.get("compiler", {}).get("gcc", {}).get("version", []):
            yml.setdefault("compiler", {}).setdefault("gcc", {}).setdefault("version", []).append("3.2")
            yml["compiler"]["gcc"]["version"].sort()
          with open(settings_path, "w") as f:
            yaml.safe_dump(yml, f)

      - name: Prepare host profile
        shell: bash
        run: |
          if [ "${{ matrix.platform }}" = "ps2" ]; then
            cp vendor/librw/conan/playstation2 host_profile
          else
            conan profile detect --force > host_profile
          fi

      - name: Export conan recipes
        run: |
          conan export vendor/librw/cmake/ps2/cmaketoolchain ps2dev-cmaketoolchain/master@
          conan export vendor/librw librw/master@
          if [ "${{ matrix.audio }}" = "miles" ]; then
            conan export re3mss miles-sdk/master@
          fi

      - name: Install dependencies
        run: |
          conan install . re3/master@ \
            -if build \
            -o re3:audio=${{ matrix.audio }} \
            -o librw:platform=${{ matrix.platform }} \
            -o librw:gl3_gfxlib=${{ matrix.gl3_gfxlib || 'glfw' }} \
            --build missing \
            -pr:h ./host_profile \
            -pr:b default \
            -s re3:build_type=RelWithDebInfo \
            -s librw:build_type=RelWithDebInfo
        env:
          CONAN_SYSREQUIRES_MODE: enabled

      - name: Build project
        run: |
          conan build . -if build -bf build -pf package

      - name: Create package
        run: |
          conan package . -if build -bf build -pf package

      - name: Generate distributable package
        working-directory: ./build
        run: |
          cpack -C RelWithDebInfo

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: "re3_${{ matrix.os }}_${{ matrix.platform }}_${{ matrix.audio }}"
          path: build/*.zip
          if-no-files-found: error
