name: macos-snapshot

#on:
#  push:
#    branches:
#      - "*-macos"
#  schedule:
#   - cron: "30 6 * * 0,1,3,5,6"

jobs:
  build:
    runs-on: macos-11
    env:
      OPENSCAD_LIBRARIES: /Users/runner/work/openscad/libraries/install/
      LIBRARIES_CACHE: libraries.tar.gz
    steps:
    - name: Checkout OpenSCAD
      uses: actions/checkout@v4
      with:
        submodules: 'recursive'
    - name: Install Homebrew packages
      run: |
        brew update
        brew install automake meson
    - name: Cache Libraries
      id: cache-libraries
      uses: actions/cache@v3
      with:
        path: ${{ env.OPENSCAD_LIBRARIES }}
        key: ${{ runner.os }}-libraries2-${{ hashFiles('./scripts/macosx-build-dependencies.sh', '.github/workflows/macos-release.yml') }}
        restore-keys: ${{ runner.os }}-libraries2-
    - name: Build Dependencies
      run: |
        export PKG_CONFIG_PATH="$OPENSCAD_LIBRARIES/lib/pkgconfig"
        export DYLD_LIBRARY_PATH="$OPENSCAD_LIBRARIES/lib"
        export DYLD_FRAMEWORK_PATH="$OPENSCAD_LIBRARIES/lib"
        echo "PWD = $CI_BASEDIR"
        echo "PATH = $PATH"
        echo "DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH"
        # Pick up our own Qt
        export PATH=$OPENSCAD_LIBRARIES/bin:$PATH
        ./scripts/macosx-build-dependencies.sh -d double_conversion eigen gmp mpfr glew gettext libffi freetype ragel harfbuzz libzip libxml2 fontconfig hidapi libuuid lib3mf poppler pixman cairo glib2 boost cgal qt5 opencsg qscintilla sparkle
    - name: Package Dependencies as an artifact
      run: |
        mkdir -p ${{ runner.temp }}/out
        tar cz -C "$OPENSCAD_LIBRARIES" -f ${{ runner.temp }}/out/"$LIBRARIES_CACHE" .
        shasum -a 512 ${{ runner.temp }}/out/"$LIBRARIES_CACHE" > ${{ runner.temp }}/out/"$LIBRARIES_CACHE".sha512
    - name: Build OpenSCAD
      run: |
        echo "Start building OpenSCAD: $(date)"
        export PKG_CONFIG_PATH="$OPENSCAD_LIBRARIES/lib/pkgconfig"
        export DYLD_LIBRARY_PATH="$OPENSCAD_LIBRARIES/lib"
        export DYLD_FRAMEWORK_PATH="$OPENSCAD_LIBRARIES/lib"
        export NUMCPU=$(($(sysctl -n hw.ncpu) * 3 / 2))
        time ./scripts/release-common.sh -snapshot
        cd build
        OPENSCAD_NAME=$(ls OpenSCAD-*.dmg)
        shasum -a 256 "$OPENSCAD_NAME" > "$OPENSCAD_NAME".sha256
        shasum -a 512 "$OPENSCAD_NAME" > "$OPENSCAD_NAME".sha512
        cp -v "$OPENSCAD_NAME"* ${{ runner.temp }}/out/
        echo "Finished building OpenSCAD: $(date)"
    - name: Verify OpenSCAD binary
      run: |
        ./scripts/macosx-sanity-check.py ./build/OpenSCAD.app/Contents/MacOS/OpenSCAD
    - name: Upload Artifacts
      uses: actions/upload-artifact@v4
      with:
        name: Build Artifacts
        path: ${{ runner.temp }}/out/
