steps:
  - script: |
      command -v python3 >/dev/null 2>&1 && python3 CI/build_version.py || python CI/build_version.py
    displayName: 'Update Version.cmake'

  - script: |
      mkdir build-release
      cd build-release
      if [ -z $(which cmake) ]; then cmake3 -DCMAKE_BUILD_TYPE=Release ..; else cmake -DCMAKE_BUILD_TYPE=Release ..; fi
      . ./version
      make -j$(makeJobsLimit)
    condition: eq(variables['packager'],'deb')
    displayName: 'Build Release deb'
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      mkdir build-release
      cd build-release
      if [ -z $(which cmake) ]; then
      cmake3 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      else cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      fi
      . ./version
      make -j$(makeJobsLimit)
    condition: eq(variables['packager'],'rpm')
    displayName: 'Build Release rpm'
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      mkdir build-ent
      cd build-ent
      export SYNERGY_ENTERPRISE=1
      if [ -z $(which cmake) ]; then cmake3 -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON ..; else cmake -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON ..; fi
      . ./version
      make -j$(makeJobsLimit)
    displayName: 'Build enterprise'
    condition: eq(variables['packager'],'deb')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      mkdir build-ent
      cd build-ent
      if [ -z $(which cmake) ]; then
      cmake3 -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      else cmake -DCMAKE_BUILD_TYPE=Release -DSYNERGY_ENTERPRISE=ON -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      fi
      . ./version
      make -j$(makeJobsLimit)
    condition: eq(variables['packager'],'rpm')
    displayName: 'Build enterprise rpm'
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      mkdir build-business
      cd build-business
      export SYNERGY_BUSINESS=1
      if [ -z $(which cmake) ]; then cmake3 -DCMAKE_BUILD_TYPE=Release -DSYNERGY_BUSINESS=ON ..; else cmake -DCMAKE_BUILD_TYPE=Release -DSYNERGY_BUSINESS=ON ..; fi
      . ./version
      make -j$(makeJobsLimit)
    displayName: 'Build business'
    condition: eq(variables['packager'],'deb')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      mkdir build-business
      cd build-business
      if [ -z $(which cmake) ]; then
      cmake3 -DCMAKE_BUILD_TYPE=Release -DSYNERGY_BUSINESS=ON -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      else cmake -DCMAKE_BUILD_TYPE=Release -DSYNERGY_BUSINESS=ON -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/rpm/BUILDROOT/usr ..;
      fi
      . ./version
      make -j$(makeJobsLimit)
    condition: eq(variables['packager'],'rpm')
    displayName: 'Build business rpm'
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      . ./build-release/version
      SYNERGY_VERSION="$SYNERGY_VERSION_MAJOR.$SYNERGY_VERSION_MINOR.$SYNERGY_VERSION_PATCH"
      SYNERGY_REVISION=`git rev-parse --short=8 HEAD`
      SYNERGY_DEB_VERSION="${SYNERGY_VERSION}.${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}"
      SYNERGY_RPM_VERSION="${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}"
      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}"
      echo "##vso[task.setvariable variable=SYNERGY_DEB_VERSION]${SYNERGY_DEB_VERSION}"
      echo "##vso[task.setvariable variable=SYNERGY_RPM_VERSION]${SYNERGY_RPM_VERSION}"
    displayName: "Setup variables"

  - script: |
      python3 CI/deb_changelog.py synergy "${SYNERGY_DEB_VERSION}"
      export GPG_TTY=$(tty)
      debuild --preserve-envvar SYNERGY_* --preserve-envvar GIT_COMMIT -us -uc
      mkdir standard_package
      cd ..
      filename=$(ls synergy_*.deb)
      filename_new="synergy_${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}_$(name)${filename##*${SYNERGY_REVISION}}"
      mv $filename $(Build.Repository.LocalPath)/standard_package/$filename_new
      cd $(Build.Repository.LocalPath)/standard_package
      md5sum $filename_new >> ${filename_new}.checksum.txt
      sha1sum $filename_new >> ${filename_new}.checksum.txt
      sha256sum $filename_new >> ${filename_new}.checksum.txt
      ls -la
    displayName: "Package Binary DEB(Standard)"
    condition: eq(variables['packager'],'deb')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      export SYNERGY_ENTERPRISE=1
      python3 CI/deb_changelog.py synergy "${SYNERGY_DEB_VERSION}"
      export GPG_TTY=$(tty)
      debuild --preserve-envvar SYNERGY_* --preserve-envvar GIT_COMMIT -us -uc
      mkdir enterprise_package
      cd ..
      filename=$(ls synergy_*.deb)
      filename_new="synergy-enterprise_${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}_$(name)${filename##*$(SYNERGY_REVISION)}"
      mv $filename $(Build.Repository.LocalPath)/enterprise_package/$filename_new
      cd $(Build.Repository.LocalPath)/enterprise_package
      md5sum $filename_new >> ${filename_new}.checksum.txt
      sha1sum $filename_new >> ${filename_new}.checksum.txt
      sha256sum $filename_new >> ${filename_new}.checksum.txt
      ls -la
    displayName: "Package Binary DEB(Enterprise)"
    condition: eq(variables['packager'],'deb')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      export SYNERGY_BUSINESS=1
      python3 CI/deb_changelog.py synergy "${SYNERGY_DEB_VERSION}"
      export GPG_TTY=$(tty)
      debuild --preserve-envvar SYNERGY_* --preserve-envvar GIT_COMMIT -us -uc
      mkdir business_package
      cd ..
      filename=$(ls synergy_*.deb)
      filename_new="synergy-business_${SYNERGY_VERSION}-${SYNERGY_VERSION_STAGE}.${SYNERGY_REVISION}_$(name)${filename##*$(SYNERGY_REVISION)}"
      mv $filename $(Build.Repository.LocalPath)/business_package/$filename_new
      cd $(Build.Repository.LocalPath)/business_package
      md5sum $filename_new >> ${filename_new}.checksum.txt
      sha1sum $filename_new >> ${filename_new}.checksum.txt
      sha256sum $filename_new >> ${filename_new}.checksum.txt
      ls -la
    displayName: "Package Binary DEB(Business)"
    condition: eq(variables['packager'],'deb')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      cd build-release
      make install/strip
      cd rpm
      rpmbuild -bb --define "_topdir $(pwd)" --buildroot $(pwd)/BUILDROOT synergy.spec
      rpmlint --verbose RPMS/*.rpm
      cd RPMS
      filename=$(ls *.rpm)
      md5sum $filename >> ${filename}.checksum.txt
      sha1sum $filename >> ${filename}.checksum.txt
      sha256sum $filename >> ${filename}.checksum.txt
      cd ..
      mv RPMS $(Build.Repository.LocalPath)/standard_package
    displayName: "Package Binary RPM(standard)"
    condition: eq(variables['packager'],'rpm')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      cd build-ent
      make install/strip
      cd rpm
      rpmbuild -bb --define "_topdir $(pwd)" --buildroot $(pwd)/BUILDROOT synergy-enterprise.spec
      rpmlint --verbose RPMS/*.rpm
      cd RPMS
      filename=$(ls *.rpm)
      md5sum $filename >> ${filename}.checksum.txt
      sha1sum $filename >> ${filename}.checksum.txt
      sha256sum $filename >> ${filename}.checksum.txt
      cd ..
      mv RPMS $(Build.Repository.LocalPath)/enterprise_package
      ls -la
    displayName: "Package Binary RPM(enterprise)"
    condition: eq(variables['packager'],'rpm')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

  - script: |
      cd build-business
      make install/strip
      cd rpm
      rpmbuild -bb --define "_topdir $(pwd)" --buildroot $(pwd)/BUILDROOT synergy-business.spec
      rpmlint --verbose RPMS/*.rpm
      cd RPMS
      filename=$(ls *.rpm)
      md5sum $filename >> ${filename}.checksum.txt
      sha1sum $filename >> ${filename}.checksum.txt
      sha256sum $filename >> ${filename}.checksum.txt
      cd ..
      mv RPMS $(Build.Repository.LocalPath)/business_package
      ls -la
    displayName: "Package Binary RPM(business)"
    condition: eq(variables['packager'],'rpm')
    env:
      GIT_COMMIT: $(Build.SourceVersion)

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

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

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