name: Build MapCache on Windows
on: [ push, pull_request ]

jobs:

    build-matrix:
      strategy:
        matrix:
          os: [ windows-2019 ]
          option: [ default ]
      runs-on: ${{matrix.os}}
      steps:
        - name: Checkout repository
          uses: actions/checkout@v2

        - name: Install dependencies
          run: |
            Set-Location -Path "${{github.workspace}}"
            New-Item -Path . -Name "sdk" -ItemType "directory"
            Set-Location -Path "sdk"
            curl -O https://download.gisinternals.com/sdk/downloads/release-1928-x64-dev.zip
            unzip -qq release-1928-x64-dev.zip
            $sdkprefix = "${{github.workspace}}\sdk\release-1928-x64"
            Set-Location -Path "$sdkprefix\lib"
            Copy-Item -Path "libfcgi.lib" -Destination "fcgi.lib"
            Copy-Item -Path "apr-1.lib" -Destination "apr-1-1.lib"
            Copy-Item -Path "libapr-1.lib" -Destination "apr-1.lib"
            Copy-Item -Path "aprutil-1.lib" -Destination "aprutil-1-1.lib"
            Copy-Item -Path "libaprutil-1.lib" -Destination "aprutil-1.lib"

        - name: Build MapCache
          run: |
            $sdkprefix = "${{github.workspace}}\sdk\release-1928-x64"
            Set-Location -Path "${{github.workspace}}"
            New-Item -Path . -Name "build" -ItemType "directory"
            Set-Location -Path "build"
            cmake -DCMAKE_PREFIX_PATH="$sdkprefix" -DWITH_APACHE=OFF -DWITH_FCGI=ON -DWITH_PCRE=ON -DWITH_TIFF=OFF "${{github.workspace}}"
            cmake --build . --config Release
            Copy-Item -Destination "$sdkprefix\bin" -Path "Release\mapcache.dll", "cgi\Release\mapcache.fcgi.exe", "util\Release\mapcache_seed.exe", "contrib\mapcache_detail\Release\mapcache_detail.exe"
            Set-Location -Path "$sdkprefix\bin"
            Compress-Archive -DestinationPath "${{github.workspace}}\mapcache.zip" -Path "${{github.workspace}}\mapcache.xml", "mapcache.dll", "mapcache.fcgi.exe", "mapcache_seed.exe", "mapcache_detail.exe"

        - name: Upload binary artifacts
          uses: actions/upload-artifact@v2
          with:
            name: binaries
            path: mapcache.zip
            retention-days: 15

        - name: Setup tests
          run: |
            $sdkprefix = "${{github.workspace}}\sdk\release-1928-x64"
            Set-Location -Path "${{github.workspace}}\build"
            Copy-Item -Path "..\tests\data\world.tif" -Destination .
            New-Item -Path "mapcache.xml"
            Set-Content -Path "mapcache.xml" -Value @"
            <?xml version=`"1.0`" encoding=`"UTF-8`"?>
            <mapcache>
              <source name=`"global-tif`" type=`"gdal`">
                <data>${{github.workspace}}\build\world.tif</data>
              </source>
              <cache name=`"disk`" type=`"disk`">
                <base>${{github.workspace}}\build</base>
              </cache>
              <tileset name=`"global`">
                <cache>disk</cache>
                <source>global-tif</source>
                <grid maxzoom=`"17`">GoogleMapsCompatible</grid>
                <format>JPEG</format>
                <metatile>1 1</metatile>
              </tileset>
              <locker type=`"disk`">
                <directory>${{github.workspace}}\build</directory>
              </locker>
              <service type=`"wms`" enabled=`"true`"/>
              <service type=`"wmts`" enabled=`"true`"/>
              <log_level>debug</log_level>
            </mapcache>
            "@

        - name: Run tests
          run: |
            $sdkprefix = "${{github.workspace}}\sdk\release-1928-x64"
            Set-Location -Path "$sdkprefix\bin"
            $env:GDAL_DATA = "$sdkprefix\bin\gdal-data"
            $env:PROJ_LIB = "$sdkprefix\bin\proj7\share"
            .\mapcache_seed.exe -c "${{github.workspace}}\build\mapcache.xml" -t global --force -z 0,1
            Copy-Item -Path "${{github.workspace}}\build\global\GoogleMapsCompatible\00\000\000\000\000\000\000.jpg" -Destination "${{github.workspace}}\seed.jpg"
            $match = (.\gdal\apps\gdalinfo.exe -checksum "${{github.workspace}}\seed.jpg" | Select-String -CaseSensitive -Pattern "Checksum=21336" -Quiet)
            if ( $match ) {
              "Success"
            } else {
              "Failure: Did not get expected checksum"
              exit 1
            }
            $env:MAPCACHE_CONFIG_FILE = "${{github.workspace}}\build\mapcache.xml"
            $env:REQUEST_METHOD = "GET"
            $env:PATH_INFO = "/wmts/1.0.0/global/default/GoogleMapsCompatible/0/0/0.jpg"
            $env:QUERY_STRING = ""
            Start-Process -FilePath ".\mapcache.fcgi.exe" -RedirectStandardOutput "${{github.workspace}}\fcgi.jpg" -NoNewWindow -Wait
            perl.exe -0777 -pi -e 'binmode ARGV;binmode ARGVOUT;s/[A-Z][-:; ,\/=A-Za-z0-9]*\r\n//g;s/\r\n//' "${{github.workspace}}\fcgi.jpg"
            $match = (.\gdal\apps\gdalinfo.exe -checksum "${{github.workspace}}\fcgi.jpg" | Select-String -CaseSensitive -Pattern "Checksum=21336" -Quiet)
            if ( $match ) {
              "Success"
            } else {
              "Failure: Did not get expected checksum"
              exit 1
            }

        - name: Upload test artifacts
          uses: actions/upload-artifact@v2
          with:
            name: test-results
            path: |
              seed.jpg
              fcgi.jpg
            retention-days: 2
