steps:
  - task: InstallAppleCertificate@2
    inputs:
      certSecureFile: 'AppleSigningCert.cer'
      certPwd: '$(AppleCertPass)'
      keychain: 'temp'

  - task: CmdLine@2
    inputs:
      script: | 
        python3 CI/build_version.py
    displayName: 'Update Version.cmake'

  - task: CmdLine@2
    inputs:
      script: | 
        python3 -m pip install dmgbuild
        python3 -m pip install aqtinstall==0.11.1
    displayName: 'Installing utilities'

  - task: CmdLine@2
    inputs:
      script: |
        python3 -m aqt install $(QT_VERSION) mac desktop -O $(QT_PATH)
    displayName: 'Installing QT'

  - task: CmdLine@2
    inputs:
      script: |
        export PATH="$(Qt5_DIR)/bin:$PATH"
        mkdir build
        cd build
        cmake \
          -DCMAKE_OSX_DEPLOYMENT_TARGET=$(version) \
          -DCMAKE_OSX_ARCHITECTURES=x86_64 \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_CONFIGURATION_TYPES=Release ..
    displayName: 'Cmake standard'
    condition: eq(variables['buildType'], 'standard')
    env:
      GIT_COMMIT: $(Build.SourceVersion)
      CMAKE_PREFIX_PATH: "$(Qt5_DIR);/usr/local/opt/openssl"

  - task: CmdLine@2
    inputs:
      script: |
        export PATH="$(Qt5_DIR)/bin:$PATH"
        export SYNERGY_ENTERPRISE=1
        mkdir build
        cd build
        cmake \
          -DCMAKE_OSX_DEPLOYMENT_TARGET=$(version) \
          -DCMAKE_OSX_ARCHITECTURES=x86_64 \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_CONFIGURATION_TYPES=Release \
          -DSYNERGY_ENTERPRISE=ON ..
    displayName: 'Cmake enterprise'
    condition: eq(variables['buildType'], 'enterprise')
    env:
      GIT_COMMIT: $(Build.SourceVersion)
      CMAKE_PREFIX_PATH: "$(Qt5_DIR);/usr/local/opt/openssl"

  - task: CmdLine@2
    inputs:
      script: |
        export PATH="$(Qt5_DIR)/bin:$PATH"
        export SYNERGY_BUSINESS=1
        mkdir build
        cd build
        cmake \
          -DCMAKE_OSX_DEPLOYMENT_TARGET=$(version) \
          -DCMAKE_OSX_ARCHITECTURES=x86_64 \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_CONFIGURATION_TYPES=Release \
          -DSYNERGY_BUSINESS=ON ..
    displayName: 'Cmake business'
    condition: eq(variables['buildType'], 'business')
    env:
      GIT_COMMIT: $(Build.SourceVersion)
      CMAKE_PREFIX_PATH: "$(Qt5_DIR);/usr/local/opt/openssl"

  - task: CmdLine@2
    inputs:
      script: |
        export PATH="$(Qt5_DIR)/bin:$PATH"
        cd build
        . ./version
        make -j
        make install/strip
    displayName: 'Compiling'
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - task: CmdLine@2
    inputs:
      script: |
        export PATH="$(Qt5_DIR)/bin:$PATH"
        macdeployqt $(Build.Repository.LocalPath)/build/bundle/Synergy.app -codesign="$CODESIGN_ID"
        codesign -f --options runtime --deep -s "$CODESIGN_ID" $(Build.Repository.LocalPath)/build/bundle/Synergy.app
        ln -s /Applications $(Build.Repository.LocalPath)/build/bundle/Applications
    displayName: 'Signing'
    env:
      CODESIGN_ID: "Developer ID Application: Symless Ltd (4HX897Y6GJ)"

  - script: |
      . ./build/version
      SYNERGY_VERSION="$SYNERGY_VERSION_MAJOR.$SYNERGY_VERSION_MINOR.$SYNERGY_VERSION_PATCH"
      SYNERGY_REVISION=`git rev-parse --short=8 HEAD`
      SYNERGY_DMG_VERSION="${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}"
      SYNERGY_DMG_FILENAME=$(prefix)_${SYNERGY_DMG_VERSION}_macos_$(platform).dmg
      echo "##vso[task.setvariable variable=SYNERGY_VERSION_STAGE]${SYNERGY_VERSION_STAGE}"
      echo "##vso[task.setvariable variable=SYNERGY_VERSION_BUILD]${SYNERGY_VERSION_BUILD}"
      echo "##vso[task.setvariable variable=SYNERGY_VERSION]${SYNERGY_VERSION}"
      echo "##vso[task.setvariable variable=SYNERGY_REVISION]${SYNERGY_REVISION}"
      echo "##vso[task.setvariable variable=SYNERGY_DMG_VERSION]${SYNERGY_DMG_VERSION}"
      echo "##vso[task.setvariable variable=SYNERGY_DMG_FILENAME]${SYNERGY_DMG_FILENAME}"
    displayName: "Setup variables"

  - task: CmdLine@2
    inputs:
      script: |
        dmgbuild \
          -s CI/MacOS/installator_settings.py \
          -D app=$(Build.Repository.LocalPath)/build/bundle/Synergy.app \
          -D background=$(Build.Repository.LocalPath)/build/bundle/Synergy.app/Contents/Resources/.installer_background.tiff \
          "Synergy" \
          $(SYNERGY_DMG_FILENAME)
        mkdir pkg
        mv $(SYNERGY_DMG_FILENAME) pkg/
        cd pkg
        md5 -r $(SYNERGY_DMG_FILENAME) >> $(SYNERGY_DMG_FILENAME).checksum.txt
        shasum $(SYNERGY_DMG_FILENAME) >> $(SYNERGY_DMG_FILENAME).checksum.txt
        shasum -a 256 $(SYNERGY_DMG_FILENAME) >> $(SYNERGY_DMG_FILENAME).checksum.txt
    displayName: 'Create Installer'

  - task: Bash@3
    inputs:
      filePath: 'CI/MacOS/notorize.sh'
      workingDirectory: 'pkg/'
    env:
      ASC_USERNAME: $(ASC_USERNAME)
      NOTORY_APP_PASSWORD: $(NOTORY_APP_PASSWORD)
    displayName: 'Submit for Notarization'

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