name: macOS package

permissions:
  contents: read

on:
  push:
    tags: [ '**' ]
  workflow_dispatch:

jobs:
  build:
    runs-on: macos-15

    steps:
    - uses: actions/checkout@v7
    - name: "Set up python (universal2: arm64 + x86_64)"
      env:
        PYVER: '3.11.9'
        PYVER_SHORT: '3.11'
      run: |
        wget -O python.pkg "https://www.python.org/ftp/python/${PYVER}/python-${PYVER}-macos11.pkg"
        sudo installer -pkg python.pkg -target /
        echo "/Library/Frameworks/Python.framework/Versions/${PYVER_SHORT}/bin" >> $GITHUB_PATH
    - name: Install dependencies
      run: |
        python3 -m pip install --upgrade pip
        python3 -m pip install --no-binary=Pillow \
          Pillow attrs configobj py2app pycotap pygame-ce pysol-cards setuptools ttkthemes accessible-output2
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
        brew install create-dmg
    - name: Install missing pygame-ce macOS assets for py2app
      env:
        PYGAME_HIDE_SUPPORT_PROMPT: '1'
      run: |
        set -euo pipefail
        # py2app's pygame recipe copies freesansbold.ttf and pygame_icon.icns
        # from the installed pygame package. pygame-ce omits pygame_icon.icns
        # from wheels (pygame-community/pygame-ce#2780); freesansbold.ttf can
        # also be missing depending on the wheel, so restore both when needed.
        PYGAME_DIR=$(python3 -c "import pygame, os; print(os.path.dirname(pygame.__file__))")
        PYGAME_TAG=$(python3 -c "import pygame; print(pygame.__version__.split('.dev')[0])")
        BASE_URL="https://raw.githubusercontent.com/pygame-community/pygame-ce/${PYGAME_TAG}/src_py"
        for asset in freesansbold.ttf pygame_icon.icns; do
          if [ ! -f "$PYGAME_DIR/$asset" ]; then
            echo "Fetching missing $asset from pygame-ce ${PYGAME_TAG}..."
            curl -fSL -o "$PYGAME_DIR/$asset" "${BASE_URL}/${asset}"
          fi
        done
        for asset in freesansbold.ttf pygame_icon.icns; do
          if [ ! -s "$PYGAME_DIR/$asset" ]; then
            echo "ERROR: $PYGAME_DIR/$asset is missing or empty after fetch"
            exit 1
          fi
        done
    - name: Get cardsets
      run: |
        wget -O cardsets-pack.tar.xz "https://sourceforge.net/projects/pysolfc/files/PySolFC-Cardsets/minimal/PySolFC-Cardsets--Minimal-3.1.0.tar.xz/download"
        tar -xJf cardsets-pack.tar.xz
        mv PySolFC-Cardsets--Minimal-3.1.0/cardset-* data
    - name: Prepare game rules (process the HTML) and translations
      run: |
        make rules mo
    - name: Create the executable .app
      run: |
        set -euo pipefail
        PYTHONPATH="." python3 setup_osx.py py2app
        test -d dist/PySolFC.app
        zip -q -r -X PySolFC-app.zip dist
    - name: Verify the app is universal2 (arm64 + x86_64)
      run: |
        set -euo pipefail
        check_universal2() {
          archs="$(lipo -archs "$1" 2>/dev/null || true)"
          case "$archs" in
            *arm64*x86_64*|*x86_64*arm64*) ;;
            *) echo "ERROR: $1 is not universal2 (archs: ${archs:-none})"; exit 1 ;;
          esac
        }
        check_universal2 "dist/PySolFC.app/Contents/MacOS/PySolFC"
        pygame_ext=$(find dist/PySolFC.app -name 'base.cpython-*-darwin.so' -path '*/pygame/*' | head -1)
        test -n "$pygame_ext"
        check_universal2 "$pygame_ext"
        echo "OK: app and pygame-ce are both universal2."
    - name: Make the .dmg for easy installation
      run:
        create-dmg --volname "Install PySolFC"
        --volicon data/PySol.icns
        --background html-src/images/pysollogo01.png
        --window-size 800 400
        --icon PySolFC.app 200 185
        --app-drop-link 600 185
        --skip-jenkins
        PySolFC.dmg dist
    - name: Upload zipped app
      uses: actions/upload-artifact@v7
      with:
        name: pysolfc-app
        path: PySolFC-app.zip
    - name: Upload dmg
      uses: actions/upload-artifact@v7
      with:
        name: pysolfc-dmg
        path: PySolFC.dmg
