default: 
    image: debian-bookworm:latest 
    tags: 
      - netvs
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip

before_script:
  - apt-get -y update
  - command -v git || apt-get -y install git
  - command -v python3 || apt-get -y install python3
  - command -v pip3 || apt-get -y install python3-pip twine python3-build
  - command -v curl || apt-get -y install curl
  - command -v bsdtar || apt-get -y install libarchive-tools
  - python3 -V  # Print out python version for debugging
  - python3 -m venv || apt-get -y install python3-venv
  - if [[ "$CI_COMMIT_BRANCH" != "main" ]]; then sed -i "s%git+https://gitlab.kit.edu/scc-net/netvs/api-generator.git@master%git+https://gitlab.kit.edu/scc-net/netvs/api-generator.git@${CI_COMMIT_BRANCH}%g" pyproject.toml; fi
  - if [[ "$CI_COMMIT_BRANCH" == "devel" ]]; then export NETDB_ENDPOINT=${NETDB_DEVEL_ENDPOINT}; fi


stages:
  - build

build:
  stage: build
  resource_group: ${CI_COMMIT_BRANCH} # this prevents parallel (potentially racy) pipeline runs
  script:
    - mkdir -p latest
    - "(curl -LsS \"${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/jobs/artifacts/${CI_COMMIT_BRANCH}/download?job=build&job_token=${CI_JOB_TOKEN}\" | bsdtar -xf - -C latest) || true"
    - test -d latest/dist && export LATEST_UTIL_VERSION=$(cd latest/dist; ls *.whl | sed -r 's/.*\+([a-z0-9.]+)-.*/\1/g')
    - test -d latest/dist && export LATEST_API_VERSION=$(cd latest/dist; ls *.whl | sed -r 's/netdb_client-([0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
    - test -d latest/dist && export POST_NUM=$(cd latest/dist; ls *.whl | sed -r 's/.*\.post([0-9]+).*/\1/g')
    - python3 -m build
    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python3 -m twine upload --verbose --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi --skip-existing dist/*
  artifacts:
    paths:
      - dist/*
    expire_in: 7 days
  only:
    - main
    - devel

debian:
  stage: build
  resource_group: deb-${CI_COMMIT_BRANCH} # this prevents parallel (potentially racy) pipeline runs
  script:
    - apt-get install -y --no-install-recommends build-essential cdbs devscripts equivs fakeroot debhelper curl unzip
    - curl https://gitlab.kit.edu/api/v4/projects/170104/jobs/artifacts/debian/download?job=build | funzip /dev/stdin > api-generator.deb
    - apt-get install -yf ./api-generator.deb
    - rm ./api-generator.deb
    - export PROJECT_ROOT=$(pwd)
    - mkdir /tmp/build/
    - cp -r . /tmp/build/netdb_client
    - cd /tmp/build/netdb_client/
    - mk-build-deps -ir -t "apt-get -o Debug::pkgProblemResolver=yes -y --no-install-recommends"    
    - debuild -b -uc -us    
    - cp /tmp/build/*.deb $PROJECT_ROOT    
  artifacts:
    paths:                                                                                                                                                                    
      - ./*.deb    
    expire_in: 7 days
  only:
    - debian
