name: Merkaartor build
on: [push, pull_request]

jobs:
  windows:
    runs-on: windows-latest
    name: Windows / Qt6
    steps:
      - run: git config --global core.autocrlf input
        shell: bash
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: msys2/setup-msys2@v2
        name: Install dependencies
        with:
          msystem: MINGW64
          update: true
          path-type: strict
          install: git mingw-w64-x86_64-toolchain mingw-w64-x86_64-python-pip msys/make msys/git mingw64/mingw-w64-x86_64-gcc mingw64/mingw-w64-x86_64-qt6 mingw64/mingw-w64-x86_64-gdal mingw64/mingw-w64-x86_64-proj mingw64/mingw-w64-x86_64-openjpeg2 mingw64/mingw-w64-x86_64-json-c mingw64/mingw-w64-x86_64-cmake mingw64/mingw-w64-x86_64-exiv2 mingw64/mingw-w64-x86_64-nsis mingw64/mingw-w64-x86_64-mesa mingw64/mingw-w64-x86_64-protobuf
      - name: Build
        shell: msys2 {0}
        run: |
          ./ci/travis-windows-script.sh
      - uses: actions/upload-artifact@v4
        name: Upload artifacts to github
        with:
          name: binaries-win64
          path: build/merkaartor-*.exe
      - name: Upload artifacts
        env:
          CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
        if: contains('refs/heads/master refs/heads/gh-actions', github.ref) || startsWith(github.ref, 'refs/tags/')
        shell: bash
        run: |
          pip3 install cloudsmith-cli
          ./build/upload-artifacts.sh

  macosx:
    runs-on: macos-13
    strategy:
      matrix:
        QT_MAJOR: [6]
    name: MacOS X / Qt${{ matrix.QT_MAJOR }}
    env:
      QT_MAJOR: ${{ matrix.QT_MAJOR }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: conda-incubator/setup-miniconda@v3
        with:
          auto-update-conda: true
          channels: conda-forge,anaconda,defaults
          auto-activate-base: true
      - name: Conda info
        shell: bash -el {0}
        run: |
          conda install gdal exiv2 proj qtbase=6.7.2 qttools qt5compat cmake pkg-config poppler # poppler is required for gdal, but missing as a dependency
          git clone https://github.com/qt/qtnetworkauth.git && cd qtnetworkauth
          git checkout v6.7.2
          mkdir build && cd build
          cmake ..
          make install
      - name: Build
        shell: bash -el {0}
        run: |
          ./ci/travis-osx-script.sh
      - uses: actions/upload-artifact@v4
        name: Upload artifacts to github
        with:
          name: binaries-osx
          path: build/merkaartor-*.dmg
      - name: Upload artifacts to cloudsmith
        env:
          CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
        if: (matrix.QT_MAJOR == 6) && (contains('refs/heads/master refs/heads/gh-actions', github.ref) || startsWith(github.ref, 'refs/tags/'))
        run: |
          pip3 install cloudsmith-cli --break-system-packages
          ./build/upload-artifacts.sh

  linux-qt5:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        SNAPSHOT: [0, 1]
        CMAKE_UNITY_BUILD: ["ON", "OFF"]
    name: Linux / Ubuntu 20.04 / Qt5 (SNAPSHOT=${{ matrix.SNAPSHOT }}) / CMAKE_UNITY_BUILD=${{ matrix.CMAKE_UNITY_BUILD }}
    env:
      QT_PREFIX: 515
      QT_REPO: ppa:beineri/opt-qt-5.15.4-focal
      TRAVIS_DIST: focal
      SNAPSHOT: ${{ matrix.SNAPSHOT }}
      CMAKE_UNITY_BUILD: ${{ matrix.CMAKE_UNITY_BUILD }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install dependencies
        run: |
          ./ci/travis-linux-install.sh
      - name: Build
        run: |
          ./ci/travis-linux-script.sh

  linux-qt6:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        SNAPSHOT: [0, 1]
        CMAKE_UNITY_BUILD: ["ON", "OFF"]
    name: Linux / Ubuntu 22.04 / Qt6 (SNAPSHOT=${{ matrix.SNAPSHOT }}) / CMAKE_UNITY_BUILD=${{ matrix.CMAKE_UNITY_BUILD }}
    env:
      SNAPSHOT: ${{ matrix.SNAPSHOT }}
      CMAKE_UNITY_BUILD: ${{ matrix.CMAKE_UNITY_BUILD }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install dependencies
        run: |
          sudo apt-get update -qq
          sudo apt-get -y install gdb libgdal-dev libproj-dev qt6-base-dev qt6-base-dev-tools qt6-tools-dev build-essential libgl1-mesa-dev cmake git libexiv2-dev libqt6svg* libqt6networkauth* libqt6core5compat* qt6-l10n-tools qt6-tools-dev-tools protobuf-compiler
      - name: Build
        run: |
          ./ci/travis-linux-script.sh

  linux-appimage:
    runs-on: ubuntu-22.04
    name: Linux / AppImage
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install dependencies
        run: |
          sudo apt-get update -qq
          sudo apt-get -y install gdb libgdal-dev libproj-dev qt6-base-dev qt6-base-dev-tools qt6-tools-dev build-essential libgl1-mesa-dev cmake git libexiv2-dev libqt6svg* libqt6networkauth* libqt6core5compat* qt6-l10n-tools qt6-tools-dev-tools protobuf-compiler
          sudo apt-get -y install python3-pip libglib2.0-dev bash dash squashfs-tools zsync fakeroot
          sudo pip install git+https://github.com/AppImageCrafters/appimage-builder.git
      - name: Build
        run: |
          mkdir build && cd build
          cmake .. -DCMAKE_BUILD_TYPE=Release -DEXTRA_TESTS=OFF
          make -j3
          make DESTDIR=AppDir install
          appimage-builder --skip-tests
      - uses: actions/upload-artifact@v4
        name: Upload artifacts to github
        with:
          name: binaries-linux
          path: build/merkaartor-*.AppImage
      - name: Upload artifacts
        env:
          CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
        if: contains('refs/heads/master refs/heads/gh-actions refs/heads/appimage', github.ref) || startsWith(github.ref, 'refs/tags/')
        run: |
          pip3 install cloudsmith-cli
          ./build/upload-artifacts.sh
