trigger:
  branches:
    include:
      - "*"
  tags:
    include:
      - "v*"

pr:
  - "*"

jobs:
  - job: build_and_test_base
    displayName: "macOS-10.14"
    timeoutInMinutes: 0
    pool:
      vmImage: 'macOS-10.14'
    steps:
      - checkout: self
        displayName: "Checkout repository"
        fetchDepth: 64
      - bash: |
          sudo mkdir /opt/local.old
          sudo mv /usr/local/* /opt/local.old
        displayName: "Cleanup /usr/local"
      - bash: |
          set -eu
          ./configure
        displayName: "Configure MacPorts Base"
      - bash: |
          set -eu
          make
        displayName: "Build MacPorts Base"
      - bash: |
          set -eu
          sudo make install
        displayName: "Install MacPorts Base"
      - bash: |
          set -eu
          make test
        displayName: "Test MacPorts Base"
      - bash: |
          set -eu

          sed -i "" "s/-v selfupdate/version/" portmgr/dmg/postflight
          sudo install -d -m755 /opt/local/libexec/macports/postflight
          sudo install -m755 \
            portmgr/dmg/postflight \
            /opt/local/libexec/macports/postflight/
          sudo install -m644 \
            src/*.tcl \
            /opt/local/libexec/macports/postflight/
          tar \
            --exclude '/opt/local/var/macports/sip-workaround/?*' \
            -cjf "MacPorts.tar.bz2" \
            /opt/local
        condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/travis-ci'), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')))
        displayName: "Prepare Deployment"
      - bash: |
          set -eu

          # printf is a built-in, so this won't show up in ps(1)
          printf \
            "machine api.bintray.com\n\tlogin %s\n\tpassword %s\n" \
            "$user" \
            "$pass" > ~/.netrc
          chmod 600 ~/.netrc

          os_major=$(uname -r | cut -f 1 -d .)
          macports_version=$(cat config/macports_version)
          sha256=$(openssl dgst -sha256 -binary "MacPorts.tar.bz2" \
              | xxd -ps -c256)
          curl \
            --upload-file "MacPorts.tar.bz2" \
            --netrc \
            --header "X-Checksum-Sha2: ${sha256}" \
            --header "X-Bintray-Package: MacPorts" \
            --header "X-Bintray-Version: ${macports_version}" \
            --header "X-Bintray-Publish: 1" \
            --header "X-Bintray-Override: 1" \
            "https://api.bintray.com/content/macports-ci-env/macports-base/MacPorts-${os_major}.tar.bz2"
        env:
          user: "macports-ci-env"
          pass: $(BINTRAY_API_TOKEN)
        condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/travis-ci'), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')))
        displayName: "Deploy to bintray"
