name: "Continuous Integration"

on:
  push:
    branches: [ master ]
  pull_request:

env:
  AREA: liechtenstein

jobs:

  Windows-Build:
    name: Windows (CMake)
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v4

    - name: Enable vcpkg cache
      uses: actions/cache@v4
      with:
        path: c:\vcpkg\installed
        key: windows-vcpkg-x64-0 # Increase the number whenever dependencies are modified
        restore-keys: windows-vcpkg-x64

    - name: Build dependencies
      run: |
        vcpkg install --triplet=x64-windows-static-md lua shapelib zlib protobuf[zlib] sqlite3 boost-program-options boost-filesystem boost-geometry boost-system boost-asio boost-interprocess boost-iostreams boost-sort rapidjson

    - name: Build tilemaker
      run: |
        mkdir ${{ github.workspace }}\build        
        cd ${{ github.workspace }}\build && cmake -DTILEMAKER_BUILD_STATIC=ON -DVCPKG_TARGET_TRIPLET="x64-windows-static-md" -DCMAKE_TOOLCHAIN_FILE="c:\vcpkg\scripts\buildsystems\vcpkg.cmake"  ..
        cd ${{ github.workspace }}\build && cmake --build . --config RelWithDebInfo

    - name: Build openmaptiles-compatible mbtiles files of Liechtenstein
      run: |
        Invoke-WebRequest -Uri https://download.geofabrik.de/europe/${{ env.AREA }}-latest.osm.pbf -OutFile ${{ env.AREA }}.osm.pbf -MaximumRedirection 5
        ${{ github.workspace }}\build\RelWithDebInfo\tilemaker.exe ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.pmtiles --verbose
        ${{ github.workspace }}\build\RelWithDebInfo\tilemaker.exe ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.mbtiles --store osm_store --verbose

    - name: 'Upload compiled executable'
      uses: actions/upload-artifact@v4
      with:
        name: tilemaker-windows
        path: |
          ${{ github.workspace }}\resources
          ${{ github.workspace }}\build\RelWithDebInfo\tilemaker.exe
          ${{ github.workspace }}\build\RelWithDebInfo\*.pdb

  unix-build:  
    strategy:  
      matrix: 
        include:
          - os: ubuntu-22.04
            triplet: x64-linux
            executable: tilemaker
            path: /usr/local/share/vcpkg/installed
            toolchain: /usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake
    name: ${{ matrix.os }} (CMake)
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v4

    - name: Enable vcpkg cache
      uses: actions/cache@v4
      with:
        path: ${{ matrix.path }}
        key: vcpkg-${{ matrix.triplet }}-0 # Increase the number whenever dependencies are modified
        restore-keys: vcpkg-${{ matrix.triplet }}

    - name: Build dependencies
      run: |
        vcpkg install --triplet=${{ matrix.triplet }} lua shapelib zlib protobuf[zlib] sqlite3 boost-program-options boost-filesystem boost-geometry boost-system boost-asio boost-interprocess boost-iostreams boost-sort rapidjson

    - name: Build tilemaker
      run: |
        mkdir build
        cd build
        cmake -DTILEMAKER_BUILD_STATIC=ON -DCMAKE_BUILD_TYPE=Release -DVCPKG_TARGET_TRIPLET=${{ matrix.triplet }} -DCMAKE_TOOLCHAIN_FILE=${{ matrix.toolchain }} -DCMAKE_CXX_COMPILER=g++ ..
        cmake --build . 
        strip tilemaker

    - name: Build openmaptiles-compatible mbtiles files of Liechtenstein
      run: |
        curl -L https://download.geofabrik.de/europe/${{ env.AREA }}-latest.osm.pbf -o ${{ env.AREA }}.osm.pbf
        ${{ github.workspace }}/build/${{ matrix.executable }} ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.pmtiles --verbose
        ${{ github.workspace }}/build/${{ matrix.executable }} ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.mbtiles --verbose --store /tmp/store

    - name: 'Upload compiled executable'
      uses: actions/upload-artifact@v4
      with:
        name: tilemaker-${{ matrix.os }}
        path: |
          ${{ github.workspace }}/resources
          ${{ github.workspace }}/build/${{ matrix.executable }}

  unix-makefile-build:
    strategy:
      matrix:
        include:
          - os: ubuntu-22.04
          - os: macos-latest
          - os: macos-14

    name: ${{ matrix.os }} (Makefile)
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v4

    - name: Install Linux dependencies
      if: ${{ matrix.os == 'ubuntu-22.04' }}
      run: |
        sudo apt install build-essential libboost-dev libboost-filesystem-dev libboost-iostreams-dev libboost-program-options-dev libboost-system-dev luajit libluajit-5.1-dev liblua5.1-0-dev libshp-dev libsqlite3-dev rapidjson-dev zlib1g-dev

    - name: Install Mac OS X dependencies
      if: ${{ matrix.os == 'macos-latest' || matrix.os == 'macos-14' }}
      run: |
        c++ --version
        brew install boost lua shapelib rapidjson

    - name: Build tilemaker
      run: |
        make -j4
        make test

    - name: Build openmaptiles-compatible mbtiles files of Liechtenstein
      run: |
        curl -L https://download.geofabrik.de/europe/${{ env.AREA }}-latest.osm.pbf -o ${{ env.AREA }}.osm.pbf
        ./tilemaker ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.pmtiles --verbose
        ./tilemaker ${{ env.AREA }}.osm.pbf --config=resources/config-openmaptiles.json --process=resources/process-openmaptiles.lua --output=${{ env.AREA }}.mbtiles --verbose --store /tmp/store


  Github-Action:
    name: Generate mbtiles with Github Action
    runs-on: ubuntu-latest

    steps:
    - name: Check out repository
      uses: actions/checkout@v4

    - name: Download PBF file
      run: curl -L https://download.geofabrik.de/europe/${AREA}-latest.osm.pbf -o ${AREA}.osm.pbf

    - name: Build openmaptiles-compatible mbtiles files of given area
      uses: ./
      with:
        input: ${{ env.AREA }}.osm.pbf 
        output: ${{ env.AREA }}.mbtiles

  docker-build:
    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: ${{ github.repository }}

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      - name: Build Docker image
        uses: docker/build-push-action@v6
        if: ${{ github.ref != 'refs/heads/master'}}
        with:
          context: .
          push: false
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        if: ${{ github.ref == 'refs/heads/master'}}
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
