name: Testbuild for MacOS
run-name: testbuild_macos
on:
  push:
    branches:
      - 'master'
    tags:
      - "*"
  pull_request:
    types:
      - edited
      - opened
      - synchronize
concurrency:
  # Cancel concurrent workflows for the same PR or commit hash.
  group: ${{github.workflow}}-${{github.event_name == 'pull_request' && github.head_ref || github.sha}}
  cancel-in-progress: true
jobs:
  build_macos_aarch64:
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - env: macos
    steps:
    - name: Install build dependencies
      run: |
        brew update
        brew install sdl2 openal-soft make ffmpeg molten-vk
    - name: Check out repository code
      uses: actions/checkout@v4
    - name: Build
      run: |
        # Public runners come with 3 CPUs.
        gmake -j3
    - name: Create testbuild package
      run: |
        # Create release directory tree
        mkdir -p publish/quake2-macos-${{github.sha}}/misc/docs
        # Copy release assets
        cp -r release/* publish/quake2-macos-${{github.sha}}/
        # Copy misc assets
        cp -r stuff/yq2.cfg publish/quake2-macos-${{github.sha}}/misc/yq2.cfg
        cp -r stuff/mapfixes publish/quake2-macos-${{github.sha}}/misc
        cp LICENSE publish/quake2-macos-${{github.sha}}/misc/docs/LICENSE.txt
        cp README.md publish/quake2-macos-${{github.sha}}/misc/docs/README.txt
        cp doc/010_index.md publish/quake2-macos-${{github.sha}}/misc/docs/010_index.txt
        cp doc/020_installation.md publish/quake2-macos-${{github.sha}}/misc/docs/020_installation.txt
        cp doc/030_configuration.md publish/quake2-macos-${{github.sha}}/misc/docs/030_configuration.txt
        cp doc/040_cvarlist.md publish/quake2-macos-${{github.sha}}/misc/docs/040_cvarlist.txt
        cp doc/050_commands.md publish/quake2-macos-${{github.sha}}/misc/docs/050_commands.txt
        cp doc/060_multiplayer.md publish/quake2-macos-${{github.sha}}/misc/docs/060_multiplayer.txt
        cp doc/070_packaging.md publish/quake2-macos-${{github.sha}}/misc/docs/070_packaging.txt
        cp doc/080_contributing.md publish/quake2-macos-${{github.sha}}/misc/docs/080_contributing.txt
        cp doc/090_filelists.md publish/quake2-macos-${{github.sha}}/misc/docs/090_filelists.md
    - name: Create dmg package
      run: |
        rm -rf build/macos-dmg
        mkdir -p build/macos-dmg
        # start with app bundle template
        unzip stuff/osx/quake2-appbundle.zip -d build/macos-dmg
        # add files in root of disk image
        cp LICENSE README.md build/macos-dmg
        # copy all binaries to Resources dir
        cp -r release/* "build/macos-dmg/Quake 2.app/Contents/Resources"
        # dependency: libSDL2
        cp -R /opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib "build/macos-dmg/Quake 2.app/Contents/Resources"
        install_name_tool -id "libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/libSDL2-2.0.0.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/quake2"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_gl1.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_gl3.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_gles3.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_gl4.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_soft.dylib"
        install_name_tool -change "/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib" "@executable_path/../Resources/libSDL2-2.0.0.dylib" "build/macos-dmg/Quake 2.app/Contents/Resources/ref_vk.dylib"
        # make disk image
        hdiutil create -srcfolder build/macos-dmg -layout SPUD -fs HFS+ -volname "Yamagi Quake II" "release/yquake2.dmg"
        cp release/yquake2.dmg publish/quake2-macos-${{github.sha}}/
    - name: Upload testbuild package
      uses: actions/upload-artifact@v4
      with:
        name: quake2-macos-${{github.sha}}
        path: publish/
        if-no-files-found: error
    - name: Create tag package
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      run: |
        # create archive
        mkdir yquake2remaster-${{matrix.env}}-${{github.ref_name}}
        mv publish/quake2-macos-${{github.sha}}/yquake2.dmg yquake2remaster-${{matrix.env}}-${{github.ref_name}}.dmg
        cp -rv publish/quake2-macos-${{github.sha}}/* yquake2remaster-${{matrix.env}}-${{github.ref_name}}
        zip -9r yquake2remaster-${{matrix.env}}-${{github.ref_name}}.zip yquake2remaster-${{matrix.env}}-${{github.ref_name}}
    - name: Upload Release Asset
      uses: softprops/action-gh-release@v2
      if: ${{ startsWith(github.ref, 'refs/tags/') }}
      with:
        files: |
          yquake2remaster-${{matrix.env}}-${{github.ref_name}}.zip
          yquake2remaster-${{matrix.env}}-${{github.ref_name}}.dmg
