---
inputs:
  version:
    default: latest
    description: Version of Mapnik to build & install
    required: true

runs:
  using: composite
  steps:
    - name: Cache "Checkout `Mapnik`" & "Build `Mapnik`"
      id: cache-mapnik
      uses: actions/cache@v4
      with:
        path: |
          mapnik-build
          mapnik-src
        key: ${{ matrix.image || matrix.os || matrix.box_generic || github.job }}-${{ matrix.compiler }}-mapnik-${{ inputs.version }}

    - name: Checkout `Mapnik`
      uses: actions/checkout@v4
      with:
        path: mapnik-src
        ref: ${{ inputs.version != 'latest' && format('v{0}', inputs.version) || '' }}
        repository: mapnik/mapnik
        submodules: recursive
      if: steps.cache-mapnik.outputs.cache-hit != 'true'

    - name: Build `Mapnik`
      run: |
        export CMAKE_BUILD_PARALLEL_LEVEL=${BUILD_PARALLEL_LEVEL:-$(nproc)}
        cmake -B mapnik-build -S mapnik-src \
          -DBUILD_BENCHMARK:BOOL=OFF \
          -DBUILD_DEMO_CPP:BOOL=OFF \
          -DBUILD_DEMO_VIEWER:BOOL=OFF \
          -DBUILD_TESTING:BOOL=OFF \
          -DBUILD_UTILITY_GEOMETRY_TO_WKB:BOOL=OFF \
          -DBUILD_UTILITY_MAPNIK_INDEX:BOOL=OFF \
          -DBUILD_UTILITY_MAPNIK_RENDER:BOOL=OFF \
          -DBUILD_UTILITY_OGRINDEX:BOOL=OFF \
          -DBUILD_UTILITY_PGSQL2SQLITE:BOOL=OFF \
          -DBUILD_UTILITY_SHAPEINDEX:BOOL=OFF \
          -DBUILD_UTILITY_SVG2PNG:BOOL=OFF \
          -DCMAKE_BUILD_TYPE:STRING=Release \
          -DCMAKE_INSTALL_PREFIX:PATH=/usr
        cmake --build mapnik-build
      shell: bash --noprofile --norc -euxo pipefail {0}
      if: steps.cache-mapnik.outputs.cache-hit != 'true'

    - name: Install `Mapnik`
      run: ${{ !matrix.image && 'sudo' || '' }} cmake --install mapnik-build
      shell: bash --noprofile --norc -euxo pipefail {0}
