name: macOS Build

# permissions needed for release mechanism
permissions:
  contents: write

on:
  push:
    branches: [ master, devel ]
  workflow_dispatch:   # allows manual trigger from GitHub Actions UI
  schedule:
    - cron: '0 6 * * 1'  # every Monday 6am UTC, adjust as desired

jobs:
  build-macos:
    runs-on: macos-latest

    steps:
      - name: Checkout wfview
        uses: actions/checkout@v4
        with:
          path: wfview

      - name: Install Homebrew dependencies
        run: |
          brew install qt@5 portaudio hidapi opus eigen

      - name: Set up sibling directories
        run: |
          BREW_PREFIX=$(brew --prefix)

          # RTAudio - compiled directly into wfview by qmake
          git clone --depth=1 https://github.com/thestk/rtaudio.git rtaudio

          # r8brain resampler - headers only
          git clone --depth=1 https://github.com/avaneev/r8brain-free-src.git r8brain-free-src

          # Eigen - headers only
          # The .pro expects ../eigen, brew installs it elsewhere so we clone it
          git clone --depth=1 --branch 3.4.0 https://gitlab.com/libeigen/eigen.git eigen

          # Opus headers - .pro expects ../opus/include
          # Brew has them, just symlink into expected location
          mkdir -p opus
          ln -s ${BREW_PREFIX}/include/opus opus/include

          # QCustomPlot - clone and build shared library
          # git clone --depth=1 https://gitlab.com/DerManu/QCustomPlot.git qcustomplot

      - name: Download QCustomPlot tgz
        run: |
          echo "=== Downloading QCP tgz file ==="
          mkdir -p qcustomplot
          curl -L "https://www.qcustomplot.com/release/2.1.1/QCustomPlot.tar.gz" \
            -o qcustomplot.tar.gz
          tar -xzf qcustomplot.tar.gz -C qcustomplot --strip-components=1
          #echo "=== QCustomPlot contents after extract ==="
          #ls qcustomplot/
          echo "=== Finished with QCP downloading step. ==="

      - name: Inspect structure
        run: |
          echo "=== Begin diagnostic output ==="
          echo "=== Top level ==="
          ls qcustomplot/
          #echo "=== Complete listing ==="
          #ls -R .
          echo "=== End diagnostic output ==="

      - name: Build QCustomPlot shared library
        run: |
          echo "=== Begin QCP build process ==="
          BREW_PREFIX=$(brew --prefix)
          export PATH="${BREW_PREFIX}/opt/qt@5/bin:$PATH"
          mkdir -p qcustomplot/qcustomplot-sharedlib/build
          cd qcustomplot/qcustomplot-sharedlib/build
          # Note, add QMAKE_APPLE_DEVICE_ARCHS="x86_64 arm64" for universal build
          # Otherwise will default to just arm64.
          cat > qcustomplot-sharedlib.pro << 'EOF'
          TEMPLATE = lib
          TARGET = qcustomplot
          QT += widgets printsupport
          CONFIG += release shared
          DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY
          SOURCES += ../../qcustomplot.cpp
          HEADERS += ../../qcustomplot.h
          EOF
          qmake qcustomplot-sharedlib.pro CONFIG+=release
          make -j$(sysctl -n hw.logicalcpu)
          cp ../../qcustomplot.h .
          echo "Checking QCP build..."
          pwd
          ls -lR .
          echo "=== Done with QCP build.==="
      - name: Add Qt5 to PATH
        run: |
          BREW_PREFIX=$(brew --prefix)
          echo "${BREW_PREFIX}/opt/qt@5/bin" >> $GITHUB_PATH
          echo "PKG_CONFIG_PATH=${BREW_PREFIX}/opt/qt@5/lib/pkgconfig" >> $GITHUB_ENV
          echo "BREW_PREFIX=${BREW_PREFIX}" >> $GITHUB_ENV

      - name: Verify qmake
        run: |
          qmake --version
          pwd
          ls
      - name: Run qmake
        working-directory: wfview
        run: |
          # Note, you can change QMAKE_APPLE_DEVICE_ARCHS to "x86_64 arm64"
          # Must also change this on QCP. 
          sed -i '' 's/x86_64 arm64/arm64/g' wfview.pro
          qmake wfview.pro \
            CONFIG+=release \
            CONFIG+=sdk_no_version_check \
            QMAKE_APPLE_DEVICE_ARCHS="arm64" \
            INCLUDEPATH+="${BREW_PREFIX}/include" \
            INCLUDEPATH+="../qcustomplot/qcustomplot-sharedlib/build" \
            LIBS+="-L${BREW_PREFIX}/lib" \
            LIBS+="-L../qcustomplot/qcustomplot-sharedlib/build"

      - name: Build
        working-directory: wfview
        run: make -j$(sysctl -n hw.logicalcpu)

      - name: Deploy and Package macOS App
        run: |
          echo "=== Fixing QCustomPlot dependency path ==="
          # Point the executable to the absolute path of our locally built QCustomPlot library
          # so macdeployqt can find it, bundle it, and fix the internal relative paths.
          install_name_tool -change \
            "libqcustomplot.1.dylib" \
            "$PWD/qcustomplot/qcustomplot-sharedlib/build/libqcustomplot.1.dylib" \
            wfview/wfview.app/Contents/MacOS/wfview

          # Use Qt's deployment tool to bundle libraries and create a .dmg
          echo "=== Deploying ==="
          ${BREW_PREFIX}/opt/qt@5/bin/macdeployqt wfview/wfview.app -dmg
          echo "=== DONE deploying ==="
      - name: Publish Beta Release
        uses: softprops/action-gh-release@v2
        with:
          # Creates a unique tag for each run, e.g., "beta-142"
          tag_name: beta-${{ github.run_number }}
          name: "macOS Beta Build #${{ github.run_number }}"
          body: "Automated beta build generated from commit ${{ github.sha }}."
          prerelease: true # This is the magic flag that marks it as a beta!
          files: wfview/wfview.dmg
          
      - name: Upload wfview DMG artifact
        uses: actions/upload-artifact@v4
        with:
          name: wfview-macos-installer
          path: wfview/wfview.dmg  # Uploads the single .dmg file
          if-no-files-found: warn

