stages:
  - checkRepository
  - checkFormat
  - build
  - documentation

RebaseCheck:
  stage: checkRepository
  variables:
    GIT_DEPTH: 100
  tags:
    - CbmRoot
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    # Get the upstream repository manually. I did not find any other way to have it for
    # comparison
    # Check if a rebase is needed
    # If a rebase is needed stop immediately
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - hash1=$(git show-ref upstream/master | cut -f1 -d' ')
    - hash2=$(git merge-base upstream/master origin/$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME)
    - echo "${hash1}"
    - echo "${hash2}"
    - if [ "${hash1}" = "${hash2}" ]; then
    -   echo "No rebase required"
    -   exit 0
    - else
    -   echo "The Merge Request is not up-to-date"
    -   echo "Rebase is required"
    -   exit 1
    - fi

LinearHistCheck:
  stage: checkRepository
  variables:
    GIT_DEPTH: 100
  tags:
    - CbmRoot
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    # Get the upstream repository manually. I did not find any other way to have it for
    # comparison
    # Check if the history introduced by the MR is linear (no branch loop in fork)
    # If not linear stop immediately
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - count=$(git rev-list --min-parents=2 --count upstream/master..HEAD)
    - echo "${count}"
    - if [ 0 = "${count}" ]; then
    -   echo "History introduced in fork is linear"
    -   exit 0
    - else
    -   echo "History introduced in fork is not linear"
    -   echo "${count} merge commits must be removed or the other new commits have to be replayed in a new branch"
    -   exit 1
    - fi

FormatCheck:
  stage: checkFormat
  tags:
    - CbmRoot
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    # Get the upstream repository manually. I did not find any other way to have it for
    # comparison
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - echo "export FAIRSOFT_VERSION=jun19p1" > env.sh
    - echo "export FAIRROOT_VERSION=v18.2.0" >> env.sh
    - echo "export SIMPATH=/cvmfs/fairroot.gsi.de/fairsoft/\${FAIRSOFT_VERSION}" >> env.sh
    - echo "export FAIRROOTPATH=/cvmfs/fairroot.gsi.de/fairroot/\${FAIRROOT_VERSION}_fairsoft-\${FAIRSOFT_VERSION}" >> env.sh
    - echo "export BUILDDIR=$PWD/build" >> env.sh
    - echo "export SOURCEDIR=$PWD" >> env.sh
    - echo "export PATH=/cvmfs/fairroot.gsi.de/clang-format-8.0.1:\$SIMPATH/bin:$PATH:/cvmfs/it.gsi.de/compiler/llvm/6.0.1/bin/" >> env.sh
    - echo "export LABEL=format-check_MR-\${CI_MERGE_REQUEST_IID}" >> env.sh
    - echo "export FAIRROOT_FORMAT_BASE=upstream/\${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" >> env.sh
    - . ./env.sh && ctest -S cmake/scripts/checkformat.cmake -VV


CbmRoot_Continuous:
  stage: build
  tags:
    - CbmRoot
  only:
    refs:
      - master
    variables:
      - $CI_PROJECT_PATH == "computing/cbmroot"
  script:
    - set -xv
    - mkdir -p build
    - cd build
    - cd ..
    - echo "export LINUX_FLAVOUR=Debian8.11" >> Dart.cfg
    - echo "export FAIRSOFT_VERSION=jun19p1" >> Dart.cfg
    - echo "export FAIRROOT_VERSION=v18.2.0" >> Dart.cfg
    - echo "export SIMPATH=/cvmfs/fairroot.gsi.de/fairsoft/\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export FAIRROOTPATH=/cvmfs/fairroot.gsi.de/fairroot/\${FAIRROOT_VERSION}_fairsoft-\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export BUILDDIR=$PWD/build" >> Dart.cfg
    - echo "export SOURCEDIR=$PWD" >> Dart.cfg
    - echo "export NCPU=5" >> Dart.cfg
    - echo "export PATH=/cvmfs/it.gsi.de/cmake/3.19.1/bin:$PATH" >> Dart.cfg
    - ls
    - pwd
    - cat Dart.cfg
    - $PWD/Dart.sh Continuous Dart.cfg

CbmRoot_Merge:
  stage: build
  tags:
    - CbmRoot
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    - echo $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
    - echo $CI_MERGE_REQUEST_PROJECT_PATH
    - if [ "$CI_MERGE_REQUEST_PROJECT_PATH" != "computing/cbmroot" ]; then
    -   echo "Should not come here"
    -   exit 1
    - fi
    - echo $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
    - echo $CI_MERGE_REQUEST_ID
    - echo $CI_MERGE_REQUEST_IID
    - echo "export LINUX_FLAVOUR=Debian8.11" >> Dart.cfg
    - echo "export FAIRSOFT_VERSION=jun19p1" >> Dart.cfg
    - echo "export FAIRROOT_VERSION=v18.2.0" >> Dart.cfg
    - echo "export SIMPATH=/cvmfs/fairroot.gsi.de/fairsoft/\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export FAIRROOTPATH=/cvmfs/fairroot.gsi.de/fairroot/\${FAIRROOT_VERSION}_fairsoft-\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export BUILDDIR=$PWD/build" >> Dart.cfg
    - echo "export SOURCEDIR=$PWD" >> Dart.cfg
    - echo "export NCPU=5" >> Dart.cfg
    - echo "export PATH=/cvmfs/it.gsi.de/cmake/3.19.1/bin:$PATH" >> Dart.cfg
    - ls
    - pwd
    - cat Dart.cfg
    - $PWD/Dart.sh MergeRequest Dart.cfg

CbmRoot_Merge_macosx:
  stage: build
  tags:
    - CbmRoot_macosx
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    - echo $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
    - echo $CI_MERGE_REQUEST_PROJECT_PATH
    - if [ "$CI_MERGE_REQUEST_PROJECT_PATH" != "computing/cbmroot" ]; then
    -   echo "Should not come here"
    -   exit 1
    - fi
    - echo $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
    - echo $CI_MERGE_REQUEST_ID
    - echo $CI_MERGE_REQUEST_IID
    - echo "export LINUX_FLAVOUR=macOS_10.13" >> Dart.cfg
    - echo "export FAIRSOFT_VERSION=jun19p1" >> Dart.cfg
    - echo "export FAIRROOT_VERSION=v18.2.0" >> Dart.cfg
    - echo "export SIMPATH=/opt/fairsoft/\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export FAIRROOTPATH=/opt/fairroot/\${FAIRROOT_VERSION}_fairsoft-\${FAIRSOFT_VERSION}" >> Dart.cfg
    - echo "export BUILDDIR=$PWD/build" >> Dart.cfg
    - echo "export SOURCEDIR=$PWD" >> Dart.cfg
    - echo "export NCPU=4" >> Dart.cfg
    - echo "export PATH=\$SIMPATH/bin:$PATH" >> Dart.cfg
    - ls
    - pwd
    - cat Dart.cfg
    - $PWD/Dart.sh MergeRequest Dart.cfg

CbmRoot_Merge_realData:
  stage: build
  tags:
    - CbmRoot_realData
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
  script:
    - echo $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
    - echo $CI_MERGE_REQUEST_PROJECT_PATH
    - if [ "$CI_MERGE_REQUEST_PROJECT_PATH" != "computing/cbmroot" ]; then
    -   echo "Should not come here"
    -   exit 1
    - fi
    - echo $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
    - echo $CI_MERGE_REQUEST_ID
    - echo $CI_MERGE_REQUEST_IID
    - echo "export LINUX_FLAVOUR=Debian10" >> Dart.cfg
    - echo "export FAIRSOFT_VERSION=jun19p2" >> Dart.cfg
    - echo "export FAIRROOT_VERSION=v18.2.1" >> Dart.cfg
    - echo "export SIMPATH=/opt/cbmsoft/fairsoft_\${FAIRSOFT_VERSION}_root6/installation" >> Dart.cfg
    - echo "export FAIRROOTPATH=/opt/cbmsoft/fairroot_\${FAIRROOT_VERSION}-fairsoft_\${FAIRSOFT_VERSION}_root6" >> Dart.cfg
    - echo "export RAW_DATA_PATH=/opt/cbmsoft/beamtime-test-data/reduced/" >> Dart.cfg
    - echo "export BUILDDIR=$PWD/build" >> Dart.cfg
    - echo "export SOURCEDIR=$PWD" >> Dart.cfg
    - echo "export NCPU=16" >> Dart.cfg
    - echo "export PATH=\$SIMPATH/bin:$PATH" >> Dart.cfg
    - ls
    - pwd
    - cat Dart.cfg
    - $PWD/Dart.sh MergeRequest Dart.cfg

pages:
  stage: documentation
  image: alpine
  tags:
    - docker
  script:
    - apk update && apk add doxygen graphviz
    - doxygen doxygen/cbmDoxyfile.conf
    - mv html-doc/html public/
  artifacts:
    expire_in: 1 day
    paths:
      - public
  only:
    - nightly_master