#
# -- Build and Package for Linux and macOS
#    build, build manual, package, and upload
#    runs manually or on completion of Create Release workflow
#
name: Build & Package Linux and macOS
on:
  # run manually
  workflow_dispatch:
  # and run on completion of "Create Release" workflow
  # uploads artifacts to release
  repository_dispatch:
    types: [release]
jobs:
  build:
    name: Build & Package on ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, ubuntu-latest]
        build_type: [Release]
        c_compiler: [gcc]
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
    steps:
    - name: Checkout GLE
      uses: actions/checkout@v4

    - name: Checkout GLE Library
      uses: actions/checkout@v4
      with:
        repository: "vlabella/gle-library"
        path: gle-library

    - name: Checkout GLE Manual
      uses: actions/checkout@v4
      with:
        repository: "vlabella/gle-manual"
        path: gle-manual

    - name: Install dependencies
      run: |
        set -x
        touch configure.args

        case $RUNNER_OS in
          Linux)
            sudo apt-get update
            # Add libjbig-dev and libzstd-dev once GLE can find them without
            # relying on nonexistent cmake config files.
            # See https://github.com/vlabella/GLE/issues/16
            sudo apt-get install cmake freeglut3-dev libboost-dev \
                libcairo-dev libdeflate-dev libgs-dev \
                libjpeg-turbo8-dev liblzma-dev libpixman-1-dev \
                libpng-dev libtiff-dev libz-dev qtbase5-dev \
                libpoppler-dev libpoppler-cpp-dev libpoppler-glib-dev libpoppler-qt5-dev \
                libglib2.0-dev extra-cmake-modules \
                ghostscript texlive texlive-latex-extra texlive-science dvipng
            jobs=$(nproc)
            ;;
          macOS)
            # Add jbigkit once GLE can find it without relying on nonexistent
            # cmake config files.
            # See https://github.com/vlabella/GLE/issues/16
            # liblzma and libz are already in macOS.
            # cmake is preinstalled with Homebrew on GitHub Actions runners and
            # trying to install it again produces an unintelligible warning.
            brew install --quiet boost cairo ghostscript jpeg-turbo \
                libdeflate libpng libtiff pixman qt@5 zstd poppler glib extra-cmake-modules \
                ghostscript texlive
            echo "-D Qt5_DIR=$(brew --prefix qt@5)/lib/cmake/Qt5" >> configure.args
            jobs=$(sysctl -n hw.activecpu)
            ;;
          Windows)
            # see build-windows.yml
            ;;
          *)
            jobs=1
        esac

        echo "--parallel $jobs" >> build.args

    - name: Configure cmake
      run: >
        xargs -t < configure.args cmake -S src -B build
        -DCMAKE_BUILD_TYPE=Release
        -DGLE_EXAMPLES_LIBRARY_PATH="${{github.workspace}}/gle-library"
        -DGLE_USER_MANUAL_PATH="${{github.workspace}}/gle-manual"

    - name: Build GLE
      run: |
        xargs -t < build.args cmake --build build --verbose

    - name: Install GLE
      run: |
        DESTDIR=destroot cmake --install build

    - name: Build GLE Manual
      working-directory: ${{github.workspace}}/gle-manual
      run: |
        export PATH=${{github.workspace}}/destroot/usr/local/bin:$PATH; make -f Makefile.gcc


#    - name: Upload Artifacts - GLE Manual PDF
#      uses: actions/upload-artifact@v4.4.3
#      with:
#        name: GLE Manual PDF
#        path: ${{github.workspace}}/gle-manual/gle-manual.pdf
#        if-no-files-found: warn
#        retention-days: 0
#        compression-level: 0

    - name: Package GLE
      working-directory: ${{github.workspace}}/build
      run: |
        case $RUNNER_OS in
          Linux)
            cpack -G "ZIP;7Z" -C Release -B destroot/usr/local
            ;;
          macOS)
            cpack -G "DragNDrop;ZIP;7Z" -C Release -B destroot/usr/local
            ;;
          *)
            jobs=1
        esac

    - name: Upload Artifacts - GLE Binary Distributables
      uses: actions/upload-artifact@v4.4.3
      with:
        name: GLE Binary Distributables ${{ runner.os }}
        path: ${{github.workspace}}/build/destroot/usr/local/gle-*.*
        if-no-files-found: warn
        retention-days: 0
        compression-level: 0

      # upload to release location if started with from release workflow
    - name: Upload Binary Distributables to Release Loaction
      if: ${{ github.event_name == 'repository_dispatch' }}
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file_glob: true
        file: ${{github.workspace}}/build/destroot/usr/local/gle-*.*
        tag: ${{ github.event.client_payload.tag }}
