# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

strategy:
  matrix:
    linux-perl530:
        PERL_VERSION: '5.30.0'
        IMAGE: 'ubuntu-latest'
    linux-perl528:
        PERL_VERSION: '5.28.2'
        IMAGE: 'ubuntu-latest'
    linux-perl526:
        PERL_VERSION: '5.26.3'
        IMAGE: 'ubuntu-latest'
    linux-perl524:
        PERL_VERSION: '5.24.4'
        IMAGE: 'ubuntu-latest'
    linux-perl522:
        PERL_VERSION: '5.22.4'
        IMAGE: 'ubuntu-latest'
    macos-perl530:
        PERL_VERSION: '5.30.0'
        IMAGE: 'macos-latest'
    macos-perl528:
        PERL_VERSION: '5.28.2'
        IMAGE: 'macos-latest'
    macos-perl526:
        PERL_VERSION: '5.26.3'
        IMAGE: 'macos-latest'
    macos-perl524:
        PERL_VERSION: '5.24.4'
        IMAGE: 'macos-latest'
    macos-perl522:
        PERL_VERSION: '5.22.4'
        IMAGE: 'macos-latest'

pool:
  vmImage: $(IMAGE)

steps:
- script: |
    sudo apt-get -qq install libssl-dev libssh2-1-dev openssh-client openssh-server
    sudo mkdir -p -m0755 /var/run/sshd
  condition: startsWith(variables['IMAGE'], 'ubuntu')
  displayName: 'Install dependencies'

- script: |
    # Setup an SSH server
    TMPDIR=${TMPDIR:-/tmp}
    SSHD_DIR=`mktemp -d ${TMPDIR}/sshd.XXXXXXXX`

    cat >"${SSHD_DIR}/sshd_config" <<-EOF
    Port 2222
    ListenAddress 0.0.0.0
    Protocol 2
    HostKey ${SSHD_DIR}/id_rsa
    PidFile ${SSHD_DIR}/pid
    AuthorizedKeysFile ${HOME}/.ssh/authorized_keys
    LogLevel DEBUG
    RSAAuthentication yes
    PasswordAuthentication yes
    PubkeyAuthentication yes
    ChallengeResponseAuthentication yes
    StrictModes no
    UsePAM no
    EOF

    ssh-keygen -t rsa -f "${SSHD_DIR}/id_rsa" -N "" -q
    /usr/sbin/sshd -f "${SSHD_DIR}/sshd_config" -E "${SSHD_DIR}/log"

    # Set up keys
    mkdir -p "${HOME}/.ssh"
    ssh-keygen -t rsa -m PEM -b 2048 -f "${HOME}/.ssh/id_rsa" -N "" -q
    cat "${HOME}/.ssh/id_rsa.pub" >>"${HOME}/.ssh/authorized_keys"
    while read algorithm key comment; do
        echo "[localhost]:2222 $algorithm $key" >>"${HOME}/.ssh/known_hosts"
    done <"${SSHD_DIR}/id_rsa.pub"
  condition: or(startsWith(variables['IMAGE'], 'ubuntu'), startsWith(variables['IMAGE'], 'macos'))
  displayName: 'Setup SSH server'

- script: |
    PATH=~/perl5/bin:~/perl5/perlbrew/bin:~/perl5/perlbrew/perls/perl-$(PERL_VERSION)/bin:$PATH
    echo "##vso[task.setvariable variable=PATH]$PATH"
    wget -O - https://install.perlbrew.pl | bash
    perlbrew install --notest perl-$(PERL_VERSION)
    perl -V
    curl -L https://cpanmin.us | perl - App::cpanminus
  condition: or(startsWith(variables['IMAGE'], 'ubuntu'), startsWith(variables['IMAGE'], 'macos'))
  displayName: 'Install perl (Unix)'

- script: |
    git config --global init.defaultBranch master
    git config --global user.name "p5-Git-Raw"
    git config --global user.email "jacquesg@cpan.org"
    cpanm --quiet --notest Dist::Zilla Dist::Zilla::PluginBundle::Author::JACQUESG App::Ack
    dzil authordeps --missing | ack -v "inc::" | cpanm --quiet --notest
    dzil listdeps --missing | cpanm --quiet --notest
  condition: or(startsWith(variables['IMAGE'], 'ubuntu'), startsWith(variables['IMAGE'], 'macos'))
  displayName: 'Install CPAN dependencies (Unix)'

- script: |
    dzil cover -ignore_re ^deps -ignore_re CORE -ignore_re ^const -ignore_re usr -test -report coveralls
  condition: startsWith(variables['IMAGE'], 'ubuntu')
  env:
    COVERALLS_REPO_TOKEN: $(COVERALLS_REPO_TOKEN)
    RELEASE_TESTING: 1
    NETWORK_TESTING: 1
    AUTOMATED_TESTING: 1
    SSH_TESTING: 1
  displayName: 'Build/Test (Linux)'

- script: |
    dzil test
  condition: startsWith(variables['IMAGE'], 'macos')
  env:
    RELEASE_TESTING: 1
    NETWORK_TESTING: 1
    AUTOMATED_TESTING: 1
    SSH_TESTING: 0
  displayName: 'Build/Test (macOS)'