###############################################################################
# (c) Copyright 2018-2023 CERN for the benefit of the LHCb Collaboration      #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################
stages:
  - check
  - build
  - docs
  - deploy

variables:
  TARGET_BRANCH: master
  # Parameters for downloading nightlies and building DaVinci
  BINARY_TAG: x86_64_v2-centos7-gcc11-opt
  LCG_VERSION: 101
  NO_LBLOGIN: "1" # prevent lbdocker containers to start LbLogin/LbEnv
  LB_NIGHTLY_SLOT: lhcb-master

check-copyright:
  stage: check
  image: gitlab-registry.cern.ch/ci-tools/ci-worker:cc7
  script:
    - curl -o lb-check-copyright "https://gitlab.cern.ch/lhcb-core/LbDevTools/raw/master/LbDevTools/SourceTools.py?inline=false"
    - python lb-check-copyright origin/${TARGET_BRANCH} --exclude lhcbproject.yml

pre-commit-checks:
  stage: check
  image: registry.cern.ch/docker.io/library/python:3.9
  variables:
    PRE_COMMIT_HOME: ${CI_PROJECT_DIR}/.cache/pre-commit
  cache:
    paths:
      - ${PRE_COMMIT_HOME}
  before_script:
    - |
      python -m venv ${CI_PROJECT_DIR}/.cache/pre-commit-venv
      . ${CI_PROJECT_DIR}/.cache/pre-commit-venv/bin/activate
      pip install pre-commit
      git fetch origin $TARGET_BRANCH
      git config user.name "Gitlab CI"
      git config user.email "noreply@cern.ch"
  script:
    - |
      if ! pre-commit run --show-diff-on-failure --from-ref FETCH_HEAD --to-ref HEAD ; then
        echo ""
        echo "Generating patch file..."
        git commit -a -m "pre-commit fixes

      patch generated by ${CI_JOB_URL}" > /dev/null
        git format-patch HEAD~
        cat <<EOF
      =======================================
       You can apply these changes with:

          curl ${CI_JOB_URL}/artifacts/raw/0001-pre-commit-fixes.patch | git am

      =======================================
      EOF
        exit 1
      fi
  artifacts:
    paths:
      - 0001-pre-commit-fixes.patch
    when: on_failure
    expire_in: 1 week

python-linting:
  stage: check
  tags:
    - cvmfs
  script:
    - . /cvmfs/lhcb.cern.ch/lib/LbEnv.sh
    # Only run the pyflakes linter and a few select pycodestyle errors
    - flake8 $(find . -name '*.py')

build:
  stage: build
  image: gitlab-registry.cern.ch/lhcb-core/lbdocker/centos7-build:latest
  tags:
    - cvmfs
  script:
    - . /cvmfs/lhcb.cern.ch/lib/LbEnv.sh
    - . /cvmfs/lhcbdev.cern.ch/nightlies/${LB_NIGHTLY_SLOT}/latest/setupSearchPath.sh
    # setupSearchPath.sh only sets CMTPROJECTPATH but we need CMAKE_PREFIX_PATH
    - export CMAKE_PREFIX_PATH=${CMTPROJECTPATH}:${CMAKE_PREFIX_PATH}
    - lb-project-init
    - make CMAKEFLAGS=-DLOKI_BUILD_FUNCTOR_CACHE=OFF
    - ln -s ./build.$BINARY_TAG/run run
    - sudo chown -R root:root $CI_PROJECT_DIR
  # Allow failure as the nightly slot we depend on may have itself failed
  allow_failure: true
  artifacts:
    untracked: true
    expire_in: 1 hr

build-docs:
  stage: docs
  image: gitlab-registry.cern.ch/lhcb-core/lbdocker/centos7-build:latest
  tags:
    - cvmfs
  # Change pip's cache directory so we can cache it
  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  before_script:
    # As the previous stage is allowed to fail, skip this stage if the previous
    # did not complete successfully (i.e. did not create the `run` symlink)
    - test -L run || { echo "WARNING build job failed, stopping"; exit 77; }
  script:
    # First try producing just the graphs (gaudirun.py). If that fails, Moore
    # is likely incompatible with the last nightly, so fail with a warning.
    - ./run make -C doc graphs || { echo "WARNING build is incompatible with last nightly, stopping"; exit 77; }
    #- reasons=
    #- ./run python doc/make_functor_docs.py "${CI_COMMIT_SHA:0:8}" "master" > doc/selection/thor_functors_reference.generated.rst || reasons+='ERROR failed functor docs build\n'
    - ./run make -C doc linkcheck || reasons+='ERROR failed link check\n'
    - ./run make -C doc html || reasons+='ERROR failed html generation\n'
    - if [ -n "$reasons" ]; then echo -e $reasons; exit 1; fi
  allow_failure:
    exit_codes: 77
  artifacts:
    untracked: false
    expire_in: 30 days
    paths:
     - doc/_build/html/
  cache:
    key: "$CI_JOB_NAME"
    paths:
      - .cache/pip

pages:
  stage: deploy
  needs: [build-docs]
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
   - mv doc/_build/html public
  artifacts:
    untracked: false
    expire_in: 30 days
    paths:
     - public

  # image: gitlab-registry.cern.ch/ci-tools/ci-web-deployer
  # rules:
  #   - if: '$CI_COMMIT_REF_PROTECTED == "true"'
  # variables:
  #   # Need to ensure that the path up to $CI_COMMIT_REF_NAME exists (the
  #   # deployment script does `mkdir`, not `mkdir -p`)
  #   EOS_PATH: "/eos/user/m/mamartin/www/davincidocs/$CI_COMMIT_REF_NAME"
  #   #EOS_PATH: "/eos/project/l/lhcbwebsites/www/projects/moore/$CI_COMMIT_REF_NAME"
  #   CI_OUTPUT_DIR: "doc/_build/html"
  #   # Makes sure files on the remote are deleted if missing from the source
  #   METHOD: "rsync"
  # before_script:
  #   # As the previous stage is allowed to fail, skip this stage if the previous
  #   # did not complete successfully (i.e. did not create the html directory)
  #   - test -d $CI_OUTPUT_DIR || { echo "WARNING docs not built, stopping"; exit 77; }
  # script:
  #   #- deploy-eos && echo -e "Deployed at\n\n\thttps://lhcbdoc.web.cern.ch/lhcbdoc/moore/$CI_COMMIT_REF_NAME/index.html"
  #   - deploy-eos && echo -e "Deployed at\n\n\thttps://mamartin.web.cern.ch/davincidocs/$CI_COMMIT_REF_NAME/index.html"
  # allow_failure:
  #   exit_codes: 77


include:
  - project: 'lhcb-rta/reference-update-bot'
    file: 'templates/ref-bot-ci-trigger-template.yml'
