name: CMake
on: [push, pull_request]
jobs:
  Ubuntu:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - { os: ubuntu-22.04, toolchain: gcc.cmake,              pkgs: "libwxgtk3.0-gtk3-dev libvtk9-dev",        args: "-DUSE_BUNDLED_CATCH2=ON"                         }
          - { os: ubuntu-22.04, toolchain: clang.cmake,            pkgs: "catch2 libwxgtk3.0-gtk3-dev libvtk9-dev", args: ""                                                }
          - { os: ubuntu-24.04, toolchain: gcc.cmake,              pkgs: "catch2 libwxgtk3.2-dev libvtk9-dev",      args: ""                                                }
          - { os: ubuntu-24.04, toolchain: clang-sanitizers.cmake, pkgs: "catch2 libwxgtk3.2-dev libvtk9-dev",      args: ""                                                }
          - { os: ubuntu-24.04, toolchain: clang.cmake,            pkgs: "catch2 libwxgtk3.2-dev libvtk9-dev",      args: "-DENABLE_CLANG_TIDY=ON -DCMAKE_BUILD_TYPE=Debug" }
    steps:
      - uses: actions/checkout@v4
      - name: install dependencies
        run: |
          sudo apt -qq update
          sudo apt install -y ${{ matrix.pkgs }} texlive-binaries texlive-metapost libproj-dev libshp-dev survex imagemagick ghostscript ninja-build clang-tidy gettext libfmt-dev
      - name: install additional texlive languages
        if: matrix.os == 'ubuntu-24.04'
        run:  sudo apt install -y texlive-lang-czechslovak texlive-lang-cyrillic
      - name: Configure CMake
        run: |
          mkdir build && cd build && \
          cmake -G Ninja \
                -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/${{ matrix.toolchain }} \
                -DCMAKE_C_FLAGS="-Werror" \
                -DCMAKE_CXX_FLAGS="-Werror" \
                -DUSE_BUNDLED_SHAPELIB=OFF \
                ${{ matrix.args }} ..
      - run: cmake --build build
      - run: cmake --build build -t test
      - name: Run samples
        if: matrix.os == 'ubuntu-24.04'
        run: cmake --build build -t samples-verify-crc -- -j 4
      - run: cmake --build build -t samples-verify-ref -- -j 4
      - uses: actions/upload-artifact@v4
        if: matrix.os == 'ubuntu-24.04' && always()
        with:
          name: samples-${{ matrix.os }}-${{ matrix.toolchain }}
          path: build/samples/
      - name: Install
        run: sudo cmake --build build -t install

  macOS:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-13, macos-14, macos-15]
    steps:
      - uses: actions/checkout@v4
      - name: install dependencies
        run: |
          find /usr/local/bin -lname '*/Library/Frameworks/Python.framework/*' -delete -print
          brew unlink python && brew link --overwrite python
          brew update  # try to comment out in case of package problems
          brew install wxwidgets vtk proj xquartz ninja basictex fmt catch2 imagemagick
      - name: Configure CMake
        run: |
          mkdir build && cd build && cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/sanitizers.cmake -DCMAKE_C_FLAGS="-Werror" -DCMAKE_CXX_FLAGS="-Werror" -DOPENGL_gl_LIBRARY:FILEPATH=/opt/X11/lib/libGL.dylib -DOPENGL_glu_LIBRARY:FILEPATH=/opt/X11/lib/libGLU.dylib -DBUILD_THBOOK=OFF ..
      - run: cmake --build build -t therion loch utest
      - run: cmake --build build -t test
      - name: Run samples
        run: |
          eval "$(/usr/libexec/path_helper)"
          sudo tlmgr update --self
          sudo tlmgr install collection-langcyrillic
          sudo tlmgr install collection-langczechslovak
          cmake --build build -t samples-verify-crc -- -j 4
          cmake --build build -t samples-verify-ref -- -j 4
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: samples-${{ matrix.os }}
          path: build/samples/
      - name: Install
        run: sudo cmake --build build -t install

  Windows:
    runs-on: windows-latest
    strategy:
      matrix:
        config:
          - { msystem: UCRT64,  arch: ucrt-x86_64,  args: ""                                                                                   }
          - { msystem: CLANG64, arch: clang-x86_64, args: "-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/sanitizers.cmake -DENABLE_PROJ_CMAKE=ON" }
    defaults:
      run:
        shell: msys2 {0}
    env:
      THDIR: d:/a/therion
    steps:
      - uses: actions/checkout@v4
      - uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.config.msystem }}
          update: true
          install: >
            make
            git
            python
            diffutils
            mingw-w64-${{ matrix.config.arch }}-freetype
            mingw-w64-${{ matrix.config.arch }}-cmake
            mingw-w64-${{ matrix.config.arch }}-proj
            mingw-w64-${{ matrix.config.arch }}-shapelib
            mingw-w64-${{ matrix.config.arch }}-vtk
            mingw-w64-${{ matrix.config.arch }}-wxWidgets3.2
            mingw-w64-${{ matrix.config.arch }}-gcc
            mingw-w64-${{ matrix.config.arch }}-make
            mingw-w64-${{ matrix.config.arch }}-bwidget
            mingw-w64-${{ matrix.config.arch }}-fmt
            mingw-w64-${{ matrix.config.arch }}-catch
      - run: mkdir build && cd build && cmake -G "MSYS Makefiles" -DCMAKE_CXX_FLAGS="-Werror" -DBUILD_THBOOK=OFF ${{ matrix.config.args }} ..
      - run: cmake --build build -t therion loch utest -- -j 4
      - name: Set up the batteries
        run: |
          reg add HKCU\\Software\\Therion //v InstallDir //t REG_SZ //d "${THDIR}/therion-batteries"
          mkdir -p $HOME/.therion
          echo -e "mpost-path ${THDIR}/therion-batteries/bin/windows/mpost.exe\npdftex-path ${THDIR}/therion-batteries/bin/windows/pdftex.exe\nidentify-path ${THDIR}/therion-batteries/bin/identify.exe\nconvert-path ${THDIR}/therion-batteries/bin/convert.exe\n" > $HOME/.therion/therion.ini
          wget -qO - https://github.com/therion/therion-batteries/archive/master.tar.gz | tar -xzf - && mv therion-batteries-master ../therion-batteries
          cp -r ../therion-batteries/_proj/{proj-$(awk -F "=" '/version/{print $2}' build/innosetup.ini),proj-datumgrid}/ ../therion-batteries/lib/
      - run: cmake --build build -t test
      - run: cmake --build build -t samples-verify-crc -- -j 4
      - run: cmake --build build -t samples-verify-ref -- -j 4
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: samples-windows-${{ matrix.config.msystem }}
          path: build/samples/
      - run: cmake --build build -t deploy -- -j 4

  # MXE:
  #   runs-on: ${{ matrix.os }}
  #   strategy:
  #     matrix:
  #       include:
  #         - { os: ubuntu-20.04, osname: focal, pkg: i686,   cmd: i686,   wine-pkg: wine32, wine: wine }
  #         - { os: ubuntu-20.04, osname: focal, pkg: x86-64, cmd: x86_64, wine-pkg: wine64, wine: wine64-stable }
  #   steps:
  #     - uses: actions/checkout@v4
  #     - name: install dependencies
  #       run: |
  #         sudo dpkg --add-architecture i386
  #         sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 86B72ED9
  #         sudo add-apt-repository 'deb [arch=amd64] https://mirror.mxe.cc/repos/apt ${{ matrix.osname }} main'
  #         sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
  #         sudo apt -qq update
  #         sudo apt install -yqq --allow-downgrades libgd3/focal libpcre2-8-0/focal libpcre2-16-0/focal libpcre2-32-0/focal libpcre2-posix2/focal
  #         sudo apt purge -yqq libmono* moby* mono* php* libgdiplus libpcre2-posix3 libzip4
  #         sudo apt install -y ${{ matrix.wine-pkg }}
  #         sudo apt install -y ninja-build
  #         sudo apt install -y mxe-${{ matrix.pkg }}-w64-mingw32.static-binutils mxe-${{ matrix.pkg }}-w64-mingw32.static-bzip2 mxe-${{ matrix.pkg }}-w64-mingw32.static-expat mxe-${{ matrix.pkg }}-w64-mingw32.static-freetype-bootstrap mxe-${{ matrix.pkg }}-w64-mingw32.static-gcc mxe-${{ matrix.pkg }}-w64-mingw32.static-gettext mxe-${{ matrix.pkg }}-w64-mingw32.static-glib mxe-${{ matrix.pkg }}-w64-mingw32.static-harfbuzz mxe-${{ matrix.pkg }}-w64-mingw32.static-jpeg \
  #                             mxe-${{ matrix.pkg }}-w64-mingw32.static-libiconv mxe-${{ matrix.pkg }}-w64-mingw32.static-libpng mxe-${{ matrix.pkg }}-w64-mingw32.static-tiff mxe-${{ matrix.pkg }}-w64-mingw32.static-vtk mxe-${{ matrix.pkg }}-w64-mingw32.static-wxwidgets mxe-${{ matrix.pkg }}-w64-mingw32.static-xz mxe-${{ matrix.pkg }}-w64-mingw32.static-zlib mxe-${{ matrix.pkg }}-w64-mingw32.static-proj
  #     - name: run build
  #       run: |
  #         export PATH=/usr/lib/mxe/usr/bin:$PATH
  #         mkdir build && cd build
  #         ${{ matrix.cmd }}-w64-mingw32.static-cmake -G Ninja \
  #             -DMXE_USE_CCACHE=OFF \
  #             -DCMAKE_CROSSCOMPILING_EMULATOR=${{ matrix.wine }} \
  #             -DCMAKE_C_FLAGS="-Werror" \
  #             -DCMAKE_CXX_FLAGS="-Werror" \
  #             -DBUILD_SHARED_LIBS=OFF \
  #             -DBUILD_THBOOK=OFF ..
  #         ninja
