# Provide docker in container for installing dependencies as root.
# https://github.com/Microsoft/azure-pipelines-agent/issues/2043#issuecomment-687983301
resources:
  containers:
  # This no longer works for Fedora
  # - container: fedora_latest
  #   image: fedora:latest
  #   options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'
  - container: debian_testing
    image: debian:testing
    options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'
  - container: ubuntu_rolling
    image: ubuntu:rolling
    options: '--name runner -v /usr/bin/docker:/usr/bin/docker:ro'

jobs:
- job: BuildTest_dnf
  pool:
    vmImage: ubuntu-latest
  steps:
  - task: PythonScript@0
    displayName: Set environment variables
    inputs:
      scriptSource: inline
      script: |
        import os

        value = os.getenv('BASE_IMAGE', 'registry.fedoraproject.org/fedora:latest')
        print('BASE_IMAGE: {value}'.format(value=value))
        print('##vso[task.setvariable variable=BASE_IMAGE]{value}'.format(value=value))

        value = os.getenv('COPR_REPO')
        print('COPR_REPO: {value}'.format(value=value))
        print('##vso[task.setvariable variable=COPR_REPO]{value}'.format(value=value))

  - script: |
      docker build \
          --build-arg BASE_IMAGE=$BASE_IMAGE \
          --build-arg COPR_REPO=$COPR_REPO \
          --target jss-base \
          --tag jss-base \
          .

      docker run \
          --name runner \
          --privileged \
          --detach \
          jss-base

      while :
      do
          docker exec runner echo "Runner is ready" && break
          echo "Waiting for runner..."
          sleep 1
          [ $((++i)) -ge 10 ] && exit 1
      done
    displayName: Create runner container

  - script: |
      docker exec runner dnf install -y dnf-plugins-core rpm-build
      if [ -n "$COPR_REPO" ]; then docker exec runner dnf copr enable -y $COPR_REPO; fi

      docker cp . runner:/root/src
      docker exec -w /root/src runner dnf builddep -y --spec jss.spec
    displayName: Install build dependencies

  - script: |
      docker exec -w /root/src runner ./build.sh --cmake='/usr/bin/cmake -DENABLE_NSS_VERSION_PQC_DEF=ON'
    displayName: Build JSS binaries, Javadoc, and run tests

- job: BuildTest_apt
  pool:
    vmImage: ubuntu-latest
  strategy:
    matrix:
      debian_testing:
        image: debian_testing
      ubuntu_rolling:
        image: ubuntu_rolling
  container: $[variables['image']]
  steps:
  - script: |
      docker exec -u 0 runner apt-get update

      # https://salsa.debian.org/freeipa-team/jss/-/blob/master/debian/control
      docker exec -u 0 runner apt-get install -y \
          cmake \
          g++ \
          junit5 \
          libcommons-lang3-java \
          libnss3-dev \
          libnss3-tools \
          libopentest4j-java \
          libslf4j-java \
          openjdk-21-jdk \
          unzip \
          zip
    displayName: Install build dependencies

  - script: ./build.sh
    displayName: Build JSS binaries, Javadoc, and run tests
    env:
      JAVA_HOME: /usr/lib/jvm/java-21-openjdk-amd64

- job: SymbolTest
  pool:
    vmImage: ubuntu-latest
  steps:
  - script: |
      grep -iroh '^Java_org_mozilla[^(;]*' native/src/main/native/ | sort -u > /tmp/functions.txt
      cat /tmp/functions.txt
    displayName: Get JNI symbols in the code

  - script: |
      grep -iroh '^Java_org_mozilla[^(;]*' lib/ | sort -u > /tmp/version.txt
      cat /tmp/version.txt
    displayName: Get JNI symbols in the version script

  - script: |
      diff /tmp/functions.txt /tmp/version.txt || true
      comm -23 --check-order /tmp/functions.txt /tmp/version.txt > /tmp/diff.txt
      test ! -s /tmp/diff.txt
    displayName: Compare JNI symbols
