steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        New-Item -Force -ItemType Directory -Path ".\deps\"
        $client = new-object System.Net.WebClient
        $client.DownloadFile("https://binaries.symless.com/bonjour/BonjourSDK.zip",".\deps\bonjoursdk.zip")
        $client.DownloadFile("https://binaries.symless.com/bonjour/Bonjour64.msi",".\deps\Bonjour64.msi")
    displayName: 'Downloading Bonjour SKD'
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.x'
      addToPath: true
      architecture: 'x64'
    displayName: 'Set python version'

  - script: |
      python CI/build_version.py
    displayName: 'Update Version.cmake'

  - script: |
      cd $(Build.SourcesDirectory)
      python -m pip install aqtinstall
    displayName: 'Install aqtinstall'

  - script: |
      cd $(Build.SourcesDirectory)
      python -m aqt install -b $(QLI_BASE_URL) --outputdir $(Build.Repository.LocalPath)\\deps\\Qt  $(QT_VERSION) windows desktop win64_msvc2017_64
      python -m aqt install -b $(QLI_BASE_URL) --outputdir $(Build.Repository.LocalPath)\\deps\\Qt  $(QT_VERSION) windows desktop win32_msvc2017
    displayName: 'Install Qt $(QT_VERSION)'

  - task: ExtractFiles@1
    inputs:
      archiveFilePatterns: './deps/*.zip'
      destinationFolder: './deps/'
      cleanDestinationFolder: false
    displayName: 'Extracting zips'

  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        Get-ChildItem '.\deps'
        msiexec.exe /i ${pwd}\deps\Bonjour64.msi INSTALLDIR="${pwd}\deps\Bonjour" /qb
    displayName: 'Installing bonjour SDK'

  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        choco install openssl -y --forcex64 --no-progress
        Copy-Item -Path "C:\\Program Files\\OpenSSL" -Destination "$(Build.Repository.LocalPath)\\ext\\openssl\\windows\\x64" -Recurse
    displayName: 'Installing OpenSSL (x64)'

  - script: |
      call "%ENV_BAT%" -arch=x64
      set BONJOUR_SDK_HOME=%cd%\%BONJOUR_SDK_DIR%
      mkdir build64
      cd build64
      IF "$(buildType)"=="enterprise" (
          cmake -G "Visual Studio 16 2019" -A %MSARCH% -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON ..
      ) else IF "$(buildType)"=="business" (
          cmake -G "Visual Studio 16 2019" -A %MSARCH% -DCMAKE_BUILD_TYPE=Release -DSYNERGY_BUSINESS=ON ..
      ) else (
        cmake -G "Visual Studio 16 2019" -A %MSARCH% -DCMAKE_BUILD_TYPE=Release ..
      )

    displayName: 'Cmake x64 Standard'
    env:
      ENV_BAT: $(ENV_BAT)
      BONJOUR_SDK_DIR: $(BONJOUR_SDK_DIR)
      MSARCH: 'x64'
      GIT_COMMIT: $(Build.SourceVersion)
      CMAKE_PREFIX_PATH: "$(QT_BIN_PATH);$(Build.Repository.LocalPath)\\ext\\openssl\\windows\\x64"

  - script: |
      call "%ENV_BAT%" -arch=x64
      cd build64
      call version
      msbuild synergy-core.sln /p:Platform="x64" /p:Configuration=Release
    displayName: 'Compiling x64'
    env:
      ENV_BAT: $(ENV_BAT)
      GIT_COMMIT: $(Build.SourceVersion)

  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        New-Item -Force -ItemType Directory -Path "C:\\Program Files (x86)\\OpenSSL-Win32"
        choco uninstall openssl -y --ignore-autouninstaller-failure --no-progress
        choco install openssl -y --forcex86 --no-progress
        Copy-Item -Path "C:\\Program Files (x86)\\OpenSSL-Win32" -Destination "$(Build.Repository.LocalPath)\\ext\\openssl\\windows\\x86" -Recurse
    displayName: 'Installing OpenSSL (x86)'

  - script: |
      call "%ENV_BAT%" -arch=x86
      set BONJOUR_SDK_HOME=%cd%\%BONJOUR_SDK_DIR%
      mkdir build32
      cd build32
      IF "$(buildType)"=="enterprise" (
         cmake -G "Visual Studio 16 2019" -A %MSARCH% -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON ..
      ) else (
         cmake -G "Visual Studio 16 2019" -A %MSARCH% -DCMAKE_BUILD_TYPE=Release ..
      )
    displayName: 'Cmake x32 Standard'
    env:
      ENV_BAT: $(ENV_BAT)
      BONJOUR_SDK_DIR: $(BONJOUR_SDK_DIR)
      MSARCH: 'Win32'
      GIT_COMMIT: $(Build.SourceVersion)
      CMAKE_PREFIX_PATH: "$(QT_BIN_PATH_32);$(Build.Repository.LocalPath)\\ext\\openssl\\windows\\x86"

  - script: |
      call "%ENV_BAT%" -arch=x86
      cd build32
      call version
      msbuild synergy-core.sln /p:Platform="Win32" /p:Configuration=Release
    displayName: 'Compiling x32'
    env:
      ENV_BAT: $(ENV_BAT)
      GIT_COMMIT: $(Build.SourceVersion)

  - task: codesigning@2
    inputs:
      secureFileId: 'WindowsSigningCert.pfx'
      signCertPassword: '$(CODESIGN_PWD)'
      files: '*\bin\Release\*.exe'
      timeServer: 'http://timestamp.digicert.com'
      hashingAlgorithm: 'SHA256'
    displayName: "Signing Synergy binaries"

  - task: MSBuild@1
    inputs:
      solution: 'build64/installer/Synergy.sln'
      msbuildVersion: '16.0'
      msbuildArchitecture: 'x64'
      platform: 'x64'
      configuration: 'Release'
      msbuildArguments: '/m'
      maximumCpuCount: true
    displayName: 'Making installer x64'
    env:
      QT_PATH: $(QT_BIN_PATH)

  - task: MSBuild@1
    inputs:
      solution: 'build32/installer/Synergy.sln'
      msbuildVersion: '16.0'
      msbuildArchitecture: 'x86'
      platform: 'x86'
      configuration: 'Release'
      msbuildArguments: '/m'
      maximumCpuCount: true
    displayName: 'Making installer x86'
    env:
      QT_PATH: $(QT_BIN_PATH_32)

  - task: codesigning@2
    inputs:
      secureFileId: 'WindowsSigningCert.pfx'
      signCertPassword: '$(CODESIGN_PWD)'
      files: '*\installer\bin\Release\Synergy.msi'
      timeServer: 'http://timestamp.digicert.com'
      hashingAlgorithm: 'SHA256'
    displayName: "Signing installers"

  - task: ArchiveFiles@2
    inputs:
      rootFolderOrFile: 'build32\bin\Release'
      includeRootFolder: false
      archiveType: 'zip'
      archiveFile: '$(Build.ArtifactStagingDirectory)/Synergy_win_x86.zip'
      replaceExistingArchive: true
      verbose: true
    displayName: "Archive 32-bit binaries"
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

  - task: ArchiveFiles@2
    inputs:
      rootFolderOrFile: 'build64\bin\Release'
      includeRootFolder: false
      archiveType: 'zip'
      archiveFile: '$(Build.ArtifactStagingDirectory)/Synergy_win_x64.zip'
      replaceExistingArchive: true
      verbose: true
    displayName: "Archive 64-bit binaries"
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

  - script: |
      call ./build64/version
      set SYNERGY_VERSION=%SYNERGY_VERSION_MAJOR%.%SYNERGY_VERSION_MINOR%.%SYNERGY_VERSION_PATCH%
      git rev-parse --short=8 HEAD > gitversion
      set /p SYNERGY_REVISION= < gitversion
      echo ##vso[task.setvariable variable=SYNERGY_VERSION_BUILD]%SYNERGY_VERSION_BUILD%
      echo ##vso[task.setvariable variable=SYNERGY_VERSION_STAGE]%SYNERGY_VERSION_STAGE%
      echo ##vso[task.setvariable variable=SYNERGY_VERSION]%SYNERGY_VERSION%
      echo ##vso[task.setvariable variable=SYNERGY_REVISION]%SYNERGY_REVISION%
    displayName: "Set Variables"

  - script: |
      cd $(Build.Repository.LocalPath)\build32\installer\bin\Release\
      set FILENAME=$(prefix)_$(SYNERGY_VERSION)-$(SYNERGY_VERSION_STAGE).$(SYNERGY_REVISION)_windows_x86.msi
      ren "Synergy.msi" "%FILENAME%"
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe md5 %FILENAME% > %FILENAME%.checksum.txt
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe sha1 %FILENAME% >> %FILENAME%.checksum.txt
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe sha256 %FILENAME% >> %FILENAME%.checksum.txt
      cd $(Build.Repository.LocalPath)\build64\installer\bin\Release\
      set FILENAME=$(prefix)_$(SYNERGY_VERSION)-$(SYNERGY_VERSION_STAGE).$(SYNERGY_REVISION)_windows_x64.msi
      ren "Synergy.msi" "%FILENAME%"
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe md5 %FILENAME% > %FILENAME%.checksum.txt
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe sha1 %FILENAME% >> %FILENAME%.checksum.txt
      $(Build.Repository.LocalPath)\ext\openssl\windows\x64\bin\openssl.exe sha256 %FILENAME% >> %FILENAME%.checksum.txt
    displayName: "Rename files"

  - task: CopyFilesOverSSH@0
    inputs:
      sshEndpoint: 'Binary Storage'
      sourceFolder: '$(Build.ArtifactStagingDirectory)/'
      contents: '*.zip'
      targetFolder: '$(BINARIES_DIR)/v1-core-$(buildType)/$(SYNERGY_VERSION)/$(SYNERGY_VERSION_STAGE)/b$(SYNERGY_VERSION_BUILD)-$(SYNERGY_REVISION)/'
      readyTimeout: '20000'
    displayName: 'Send archives to Binary Storage'
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

  - task: CopyFilesOverSSH@0
    inputs:
      sshEndpoint: 'Binary Storage'
      sourceFolder: '$(Build.Repository.LocalPath)\build32\installer\bin\Release\'
      contents: '*'
      targetFolder: '$(BINARIES_DIR)/v1-core-$(buildType)/$(SYNERGY_VERSION)/$(SYNERGY_VERSION_STAGE)/b$(SYNERGY_VERSION_BUILD)-$(SYNERGY_REVISION)/'
      readyTimeout: '20000'
    displayName: 'Send x86 package to Binary Storage'


  - task: CopyFilesOverSSH@0
    inputs:
      sshEndpoint: 'Binary Storage'
      sourceFolder: '$(Build.Repository.LocalPath)\build64\installer\bin\Release\'
      contents: '*'
      targetFolder: '$(BINARIES_DIR)/v1-core-$(buildType)/$(SYNERGY_VERSION)/$(SYNERGY_VERSION_STAGE)/b$(SYNERGY_VERSION_BUILD)-$(SYNERGY_REVISION)/'
      readyTimeout: '20000'
    displayName: 'Send x64 package to Binary Storage'
