# This file is part of ssh2-python.
# Copyright (C) 2017-2022 Panos Kittenis and contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
version: 2.1


jobs:
  python_test:
    parameters:
      python_ver:
        type: string
        default: "3.12"
    docker:
      - image: cimg/python:<< parameters.python_ver >>
    steps:
      - checkout
      - run:
          name: Deps
          command: |
            sudo apt-get update
            sudo apt-get install cmake openssh-server
            pip install -r requirements_dev.txt
      - run:
          command: |
            pip uninstall -y cython
            python setup.py build_ext --inplace
            eval "$(ssh-agent -s)"
          name: Build
      - run:
          command: |
            set -x
            ls -lhtr ssh2/
            pwd
            python -m pytest --junit-xml=results.xml tests
          name: Test
      - run:
          command: |
            python -m pytest ci/integration_tests
          name: Integration
          max_auto_reruns: 3
          auto_rerun_delay: 5s
      - run:
          command: |
            flake8 ssh2
          name: Flake
      - run:
          command: |
            python setup.py sdist
            cd dist; pip install *; python -c 'from ssh2.session import Session; Session()'; cd ..
          name: Sdist Install
      - run:
          command: |
            cd doc
            make html
            cd ..
          name: Docs
      - store_test_results:
          path: results.xml

  osx:
    parameters:
      xcode_ver:
        type: string
    macos:
      xcode: << parameters.xcode_ver >>
    environment:
      HOMEBREW_NO_AUTO_UPDATE: 1
      SYSTEM_LIBSSH2: 1
    steps:
      - checkout
      - run:
          name: deps
          command: |
            set -x
            export HOMEBREW_NO_INSTALL_CLEANUP=1
            brew install cmake python libssh2
            brew link --force openssl
            brew link --force libssh2
            pip3 install twine
            which twine
      - run:
          name: Build Wheel
          command: |
            ./ci/osx-wheel.sh
      - store_artifacts:
          path: wheels
      - run:
          name: Upload Wheel
          command: |
            if [[ ! -z "$CIRCLE_TAG" ]]; then
              twine upload --skip-existing -u $PYPI_USER -p $PYPI_PASSWORD wheels/*
            fi

  manylinux-x86_64:
    machine:
      image: ubuntu-2004:current
    steps: &manylinux-steps
      - checkout
      - run:
          name: Deps
          command: |
            sudo apt-get install python3-pip
            pip install -U pip setuptools
            pip install twine
            which twine
            which python3
      - run:
          name: Build Wheels
          command: |
            export LIBSSH2_VERSION=1.11.1
            if [[ -z "${CIRCLE_PULL_REQUEST}" ]]; then
              echo "$DOCKER_PASSWORD" | docker login -u="$DOCKER_USERNAME" --password-stdin;
            fi
            ./ci/build-manylinux.sh
      - run:
          name: sdist
          command: |
            sudo chown circleci -R *
            python3 setup.py sdist
      - run:
          name: Upload Wheels
          command: |
            if [[ ! -z "$CIRCLE_TAG" ]]; then
              twine upload --skip-existing -u $PYPI_USER -p $PYPI_PASSWORD dist/* wheelhouse/*
            fi

  manylinux2014-aarch64:
    machine:
      image: ubuntu-2004:current
    resource_class: arm.medium
    steps: *manylinux-steps

workflows:
  version: 2
  main:
    jobs:
      - python_test:
          matrix:
            parameters:
              python_ver:
                - "3.8"
                - "3.11"
                - "3.12"
                - "3.13"
          filters:
            tags:
              ignore: /.*/
      - manylinux-x86_64: &manylinux-wf
          context: Docker
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
      - manylinux2014-aarch64: *manylinux-wf
      - osx:
          matrix:
            parameters:
              xcode_ver:
                - "14.0.0"
                - "15.0.0"
                - "16.0.0"
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
