version: 0.0.1-branch-{branch}-build-{build}
cache:
    - c:\protobuf
    - c:\protoc
    - c:\zlib
environment:
    matrix:
        - vc_arch: amd64
          choco_arch:
          nuget_arch: x64
          target_arch: x86_64
          qt_ver: 5.5\msvc2013_64
          bintray_path: Win64
          MYSQL_DRIVER_URL: https://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-6.1.6-winx64.zip
          MYSQL_DRIVER_ARCHIVE: mysql-connector-c-6.1.6-winx64.zip
          MYSQL_DRIVER_NAME: mysql-connector-c-6.1.6-winx64
        - vc_arch: amd64_x86  # cross-compile from amd64 to x86
          choco_arch: --x86
          nuget_arch: Win32
          target_arch: x86
          qt_ver: 5.5\msvc2013
          bintray_path: Win32
          MYSQL_DRIVER_URL: https://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-6.1.6-win32.zip
          MYSQL_DRIVER_ARCHIVE: mysql-connector-c-6.1.6-win32.zip
          MYSQL_DRIVER_NAME: mysql-connector-c-6.1.6-win32
install:
    - systeminfo
    # upgrade cmake in order to have c++11 support
    - choco install cmake -y
    # The chocolatey guys like to push broken packages; better stick with a working one
    - choco install nsis -y --version 2.50.0.20160103
    - ps: |
        if (Test-Path c:\protoc) {
            echo "using protoc from cache"
        } else {
            Invoke-WebRequest "https://github.com/google/protobuf/releases/download/v2.6.1/protoc-2.6.1-win32.zip" -OutFile c:\protoc-2.6.1-win32.zip
            c:\cygwin\bin\bash -lc "cd /cygdrive/c; 7z x -y protoc-2.6.1-win32.zip -oc:\protoc"
        }
    - ps: |
        if (Test-Path c:\protobuf) {
            echo "using protobuf from cache"
        } else {
           nuget install protobuf-v120 -OutputDirectory c:\protobuf
        }
    - ps: |
        if (Test-Path c:\zlib) {
            echo "using zlib from cache"
        } else {
           nuget install zlib -OutputDirectory c:\zlib
        }
    # install mysql connector
    - curl -kLO %MYSQL_DRIVER_URL%
    - 7z x %MYSQL_DRIVER_ARCHIVE% -oc:\ >nul
build_script:
    - mkdir build
    - cd build
    - '"c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/vcvarsall" %vc_arch%'
    - path
    - ps: |
        $zlibinc = c:\cygwin\bin\find /cygdrive/c/zlib/ -path '*v120*/zlib.h'
        $zlibinc = c:\cygwin\bin\dirname $zlibinc
        $zlibinc = c:\cygwin\bin\cygpath -m $zlibinc
        $zliblib = c:\cygwin\bin\find /cygdrive/c/zlib/ -path "*v120*/$env:nuget_arch/Release/zlib.lib"
        $zliblib = c:\cygwin\bin\cygpath -m $zliblib
        $protoinc = c:\cygwin\bin\find /cygdrive/c/protobuf/ -name 'google'
        $protoinc = c:\cygwin\bin\dirname $protoinc
        $protoinc = c:\cygwin\bin\cygpath -m $protoinc
        $protolib = c:\cygwin\bin\find /cygdrive/c/protobuf/ -path "*/lib/$env:nuget_arch/v120/Release/libprotobuf.lib"
        $protolib = c:\cygwin\bin\cygpath -m $protolib
        $protoc = c:\cygwin\bin\find /cygdrive/c/protoc/ -name "protoc.exe"
        $protoc = c:\cygwin\bin\cygpath -m $protoc
        $mysqldll = c:\cygwin\bin\find /cygdrive/c/$env:MYSQL_DRIVER_NAME -name "libmysql.dll"
        $mysqldll = c:\cygwin\bin\cygpath -m $mysqldll
        Write-Output "ZLIBINC  = $zlibinc"
        Write-Output "ZLIBLIB  = $zliblib"
        Write-Output "PROTOINC = $protoinc"
        Write-Output "PROTOLIB = $protolib"
        Write-Output "PROTOC   = $protoc"
        Write-Output "MYSQLDLL   = $mysqldll"
        cmake .. "-GNMake Makefiles" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_PREFIX_PATH=c:/Qt/$env:qt_ver" "-DWITH_SERVER=1" "-DZLIB_INCLUDE_DIR=$zlibinc" "-DZLIB_LIBRARY=$zliblib" "-DPROTOBUF_INCLUDE_DIR=$protoinc" "-DPROTOBUF_LIBRARIES=$protolib" "-DPROTOBUF_LIBRARIES=$protolib" "-DPROTOBUF_LIBRARY=$protolib" "-DPROTOBUF_PROTOC_EXECUTABLE=$protoc" "-DMYSQLCLIENT_LIBRARIES=$mysqldll"
    - nmake package
    - c:\cygwin\bin\ls -l
    - ps: |
        $exe = dir -name *.exe
        $new_name = $exe.Replace(".exe", "-${env:target_arch}_qt5.exe")
        Push-AppveyorArtifact $exe -FileName $new_name
        $cmake_name = $exe.Replace(".exe", "-${env:target_arch}_qt5.cmake.txt")
        Push-AppveyorArtifact CMakeCache.txt -FileName $cmake_name
        $json = New-Object PSObject
        (New-Object PSObject | Add-Member -PassThru NoteProperty bin $new_name | Add-Member -PassThru NoteProperty cmake $cmake_name | Add-Member -PassThru NoteProperty commit $env:APPVEYOR_REPO_COMMIT) | ConvertTo-JSON | Out-File -FilePath "latest-$env:target_arch" -Encoding ASCII
        Push-AppveyorArtifact "latest-$env:target_arch"
        $bt_password = ConvertTo-SecureString $Env:BINTRAY_APIKEY -AsPlainText -Force
        $bt_credentials = New-Object System.Management.Automation.PSCredential ($Env:BINTRAY_USER, $bt_password)
        $exe -match "Cockatrice-(?<content>.*)\.exe"
        $version = $matches['content']
        $bt_headers = @{
            "X-Bintray-Package" = "Cockatrice-git";
            "X-Bintray-Version" = $version;
            "X-Bintray-Publish" = 1;
            "X-Bintray-Override" = 1;
        }
        $url = "https://api.bintray.com/content/cockatrice/Cockatrice/$env:bintray_path/$new_name"
        $result = Invoke-WebRequest -Uri $url -Credential $bt_credentials -Method PUT -Headers $bt_headers -InFile "$exe"
        Write-Host $result
test: off
