version: 0.16.0.{build}

#init:
#  # Exclude combinations allowed to fail
#  - if "%Environment%" == "VSVER=14" (if "%Configuration%" == "Release" (exit /b 1))
#  - if "%Environment%" == "VSVER=14" (if "%Configuration%" == "Debug" (exit /b 1))
#  - if "%Environment%" == "VSVER=10" (if "%Platform%" == "x64" (exit /b 1))

platform:
  - x86
  - x64

configuration:
  - Release
  - Debug
  - Light-Release
  - Light-Debug

environment:
  matrix:
    # - VSVER: 14
    - VSVER: 12
    # - VSVER: 10

matrix:
  fast_finish: true     # set this flag to immediately finish build once one of the jobs fails.
  allow_failures:
    # not included in AppVeyor right now
    - platform: x64
      VSVER: 10
    # does currently not build zlib out of the box
    - configuration: Release
      VSVER: 14
    # does currently not build zlib out of the box
    - configuration: Debug
      VSVER: 14

install:
  - ps: if ($env:APPVEYOR_PULL_REQUEST_NUMBER -and $env:APPVEYOR_BUILD_NUMBER -ne ((Invoke-RestMethod `
        https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/history?recordsNumber=50).builds | `
        Where-Object pullRequestId -eq $env:APPVEYOR_PULL_REQUEST_NUMBER)[0].buildNumber) { `
        throw "There are newer queued builds for this pull request, failing early." }
  - date /T & time /T
  - set PATH=C:\cygwin\bin;%PATH%
  - set OPENSSL_VER=1_0_2e
  - set ZLIB_VER=128
  - set ZLIB_VER_DOT=1.2.8
  - ps: $env:PACKAGE_NAME=(git describe --tags)
  - ps: >-
      If ($env:Platform -Match "x86") {
        $env:VCVARS_PLATFORM="x86"
        $env:ENV_PLATFORM="x86"
        $env:OPENSSL_PF="Win32"
        $env:NMAKE_ARCH=""
        $env:ARTIFACT="OpenSC-${env:PACKAGE_NAME}-win32_vs${env:VSVER}-${env:CONFIGURATION}.msi"
      } Else {
        $env:VCVARS_PLATFORM="amd64"
        $env:ENV_PLATFORM="x64"
        $env:OPENSSL_PF="Win64"
        $env:NMAKE_ARCH="BUILD_ON=WIN64 BUILD_FOR=WIN64"
        $env:ARTIFACT="OpenSC-${env:PACKAGE_NAME}-win64_vs${env:VSVER}-${env:CONFIGURATION}.msi"
      }
  - ps: >-
      If ($env:Configuration -Like "*Debug*") {
        $env:NMAKE_EXTRA="DEBUG_DEF=/DDEBUG ${env:NMAKE_EXTRA}"
      }
  - ps: >-
      If (!($env:Configuration -Like "*Light*")) {
        If (!(Test-Path -Path "C:\OpenSSL-${env:OPENSSL_PF}" )) {
          Start-FileDownload https://slproweb.com/download/${env:OPENSSL_PF}OpenSSL-${env:OPENSSL_VER}.exe -FileName C:\WinOpenSSL.exe
          C:\WinOpenSSL.exe /SILENT /VERYSILENT /SP- /SUPPRESSMSGBOXES /NORESTART
        }
        $env:NMAKE_EXTRA="OPENSSL_DEF=/DENABLE_OPENSSL ${env:NMAKE_EXTRA}"
        If (!(Test-Path zlib.zip )) {
           appveyor DownloadFile "https://prdownloads.sourceforge.net/libpng/zlib${env:ZLIB_VER}.zip" -FileName zlib.zip
        }
        7z x zlib.zip -oC:\
        Rename-Item -path "c:\zlib-${env:ZLIB_VER_DOT}" -newName "zlib"
      }
  - ps: $env:VSCOMNTOOLS=(Get-Content ("env:VS" + "$env:VSVER" + "0COMNTOOLS"))
  - echo "Using Visual Studio %VSVER%.0 at %VSCOMNTOOLS%"
  - call "%VSCOMNTOOLS%\..\..\VC\vcvarsall.bat" %VCVARS_PLATFORM%
  - appveyor DownloadFile "http://download.microsoft.com/download/2/C/9/2C93059C-0532-42DF-8C24-9AEAFF00768E/cngsdk.msi"
  - cngsdk.msi /quiet
  - uname -a
  - set

build_script:
  # build zlib.lib as a static library
  - ps: >-
      if (!($env:Configuration -Like "*Light*")) {
          cd C:\zlib
          If ($env:Platform -Match "x86") {
             nmake -f win32/Makefile.msc LOC="-DASMV -DASMINF" OBJA="inffas32.obj match686.obj" zlib.lib
          } Else {
             nmake -f win32/Makefile.msc AS=ml64 LOC="-DASMV -DASMINF -I." OBJA="inffasx64.obj gvmat64.obj inffas8664.obj" zlib.lib
          }
          $env:NMAKE_EXTRA="ZLIBSTATIC_DEF=/DENABLE_ZLIB_STATIC ${env:NMAKE_EXTRA}"
          cd c:\projects\Opensc
      }
  - bash -c "exec 0</dev/null && ./bootstrap"
  # disable features to speed up the script
  - bash -c "exec 0</dev/null && ./configure --disable-openssl --disable-readline --disable-zlib || cat config.log"
  - bash -c "make -C etc opensc.conf"
  - cp win32/winconfig.h config.h
  - nmake /f Makefile.mak %NMAKE_ARCH% %NMAKE_EXTRA%
  - cd win32 && nmake /f Makefile.mak %NMAKE_ARCH% %NMAKE_EXTRA% VSVER=%VSVER% OpenSC.msi
  - move OpenSC.msi %ARTIFACT%
  - appveyor PushArtifact %ARTIFACT%
  # put all pdb files for dump analysis, but this consume approx 100 MB per build
  - ps: >-
      If ($env:Configuration -Like "*Debug*") {
        Get-ChildItem -recurse c:\projects\OpenSC -exclude vc*.pdb *.pdb | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
      }

cache:
  - C:\OpenSSL-Win32 -> appveyor.yml
  - C:\OpenSSL-Win64 -> appveyor.yml
  - zlib.zip -> appveyor.yml

deploy:
  - provider: GitHub
    tag: $(APPVEYOR_REPO_TAG_NAME)
    release: OpenSC-$(APPVEYOR_REPO_TAG_NAME)
    description: 'release OpenSC $(APPVEYOR_REPO_TAG_NAME)'
    auth_token:
      secure: NGaTqWohBQa7fgE62rEm2sp9jkv6S9FRc3YEi3T5CpaoyIY6K89FJjqzaoPLr8vj
    artifact: /OpenSC-.*\.msi/
    draft: false
    prerelease: true
    on:
      branch: /0.16.0-rc.*/		# here branch is release tag
      appveyor_repo_tag: true  		# deploy on tag push only
