stages:
  - checkRepository
  - checkFormat
  - build
  - finalise
  - documentation

RebaseCheck:
  stage: checkRepository
  variables:
    GIT_DEPTH: 200
  image: alpine
  tags:
    - docker
  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
    - apk update && apk add git bash
    - 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 HEAD)
    - 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: 200
  image: alpine
  tags:
    - docker
  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
    - apk update && apk add git bash
    - 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

CodeFormatCheck:
  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=apr21p2" > env.sh
    - echo "export FAIRROOT_VERSION=v18.6.7" >> env.sh
    - echo "export SIMPATH=/cvmfs/fairsoft.gsi.de/debian10/fairsoft/\${FAIRSOFT_VERSION}" >> env.sh
    - echo "export FAIRROOTPATH=/cvmfs/fairsoft.gsi.de/debian10/fairroot/\${FAIRROOT_VERSION}_fs_\${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-11.0.0/bin:\$SIMPATH/bin:$PATH" >> 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
FileFormatCheck:
  stage: checkFormat
  image: alpine
  tags:
    - docker
  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
    - apk update && apk add git bash file
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - scripts/check-file-format.sh upstream

FileEndCheck:
  stage: checkFormat
  image: alpine
  tags:
    - docker
  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
    - apk update && apk add git bash file
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - scripts/check-file-ending.sh upstream


FileLicenceCheck:
  stage: checkFormat
  image: alpine
  tags:
    - docker
  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
    - apk update && apk add git bash file
    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
    - git fetch upstream
    - scripts/check-licence-header.sh upstream


#
# job template
#
.build_and_test: &build_and_test
  before_script:
    - if [ "$CI_MERGE_REQUEST_PROJECT_PATH" != "computing/cbmroot" ]; then
    -   if [ "$CI_PROJECT_PATH" != "computing/cbmroot" ]; then
    -     echo "Should not come here"
    -     env
    -     exit 1
    -   fi
    - fi
  script:
    - echo "export LINUX_FLAVOUR=$OS" >> Dart.cfg
    - echo "export SIMPATH=$SIMPATH" >> Dart.cfg
    - echo "export FAIRROOTPATH=$FAIRROOTPATH" >> Dart.cfg
    - if [[ -n $RAW_DATA_PATH ]]; then
    -   echo "export RAW_DATA_PATH=$RAW_DATA_PATH" >> Dart.cfg
    - fi
    - echo "export BUILDDIR=$PWD/build" >> Dart.cfg
    - echo "export SOURCEDIR=$PWD" >> Dart.cfg
    - if [[ -n $EXTRA_PATH ]]; then
    -   echo "export PATH=$EXTRA_PATH:$PATH" >> Dart.cfg
    - fi
    - if [[ -n $EXTRA_FLAGS ]]; then
    -   echo "export EXTRA_FLAGS=$EXTRA_FLAGS" >> Dart.cfg
    - fi
    - if [[ -n $INSTALL_PROJECT ]]; then
    -   echo "export INSTALL_PROJECT=TRUE" >> Dart.cfg
    - fi
    - if [[ -n $CONFIGFILE ]]; then
    -   source $CONFIGFILE $FAIRSOFT_VERSION
    - fi
    - $PWD/Dart.sh $BUILD_TYPE Dart.cfg
  after_script:
    - rm -rf build

#
# job element definitions
#
.realData_tag: &realData_tag
  stage: build
  tags:
    - CbmRoot_realData

.realData_variables: &realData_variables
    OS: "Debian10"
    EXTRA_PATH: /opt/cmake/3.20.1/bin
    NCPU: 16
    RAW_DATA_PATH: "/opt/cbmsoft/beamtime-test-data/reduced/"
    FAIRSOFT_VERSION: "apr21p2_root6"
    FAIRROOT_VERSION: "v18.6.7"
    BASE_PATH: "/opt/cbmsoft"
    SIMPATH: "$BASE_PATH/fairsoft_$FAIRSOFT_VERSION/installation"
    FAIRROOTPATH: "$BASE_PATH/fairroot_${FAIRROOT_VERSION}-fairsoft_${FAIRSOFT_VERSION}"

.apptainer_tag: &apptainer_tag
  stage: build
  tags:
    - CbmRoot_apptainer

.apptainer_debian10_variables_apr21: &apptainer_debian10_variables_apr21
    OS: debian10
    FAIRSOFT_VERSION: apr21p2
    FAIRROOT_VERSION: v18.6.7
    BASE_PATH: "/cvmfs/fairsoft.gsi.de/$OS"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_fs_${FAIRSOFT_VERSION}"
    CONFIGFILE: "/cvmfs/cbm.gsi.de/$OS/bin/setup_development_environment.sh"
    INSTALL_PROJECT: "TRUE"
    EXTRA_FLAGS: '"-DCBM_TEST_INSTALL=TRUE"'

.apptainer_debian10_variables_nov22p1: &apptainer_debian10_variables_nov22p1
    OS: debian10
    FAIRSOFT_VERSION: nov22p1
    FAIRROOT_VERSION: v18.8.0
    BASE_PATH: "/cvmfs/fairsoft.gsi.de/$OS/"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_fs_${FAIRSOFT_VERSION}"
    CONFIGFILE: "/cvmfs/cbm.gsi.de/$OS/bin/setup_development_environment.sh"
    INSTALL_PROJECT: "TRUE"
    EXTRA_FLAGS: '"-DCBM_TEST_INSTALL=TRUE"'

.apptainer_centos7_variables_apr21: &apptainer_centos7_variables_apr21
    OS: centos7
    FAIRSOFT_VERSION: apr21p2
    FAIRROOT_VERSION: v18.6.7
    BASE_PATH: "/cvmfs/fairsoft.gsi.de/$OS/gcc8"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_fs_${FAIRSOFT_VERSION}"
    CONFIGFILE: "/cvmfs/cbm.gsi.de/$OS/bin/setup_development_environment_test.sh"

.apptainer_centos7_variables_nov22p1: &apptainer_centos7_variables_nov22p1
    OS: centos7
    FAIRSOFT_VERSION: nov22p1
    FAIRROOT_VERSION: v18.8.0
    BASE_PATH: "/cvmfs/fairsoft.gsi.de/$OS/gcc8"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    CONFIGFILE: "/cvmfs/fairsoft.gsi.de/$OS/bin/setup_development_environment.sh"

.apptainer_debian11_variables_apr21: &apptainer_debian11_variables_apr21
    CONTAINER: "/cvmfs/cbm.gsi.de/containers/debian11_v18.6.7_apr21p2.sif"
    OS: debian11
    FAIRSOFT_VERSION: apr21p2
    FAIRROOT_VERSION: v18.6.7
    BASE_PATH: "/opt/"
    SIMPATH: "$BASE_PATH/fairsoft/${FAIRSOFT_VERSION}"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    INSTALL_PROJECT: "TRUE"
    EXTRA_FLAGS: '"-DCBM_TEST_INSTALL=TRUE"'

.apptainer_debian12_variables_nov22p1: &apptainer_debian12_variables_nov22p1
    CONTAINER: "/cvmfs/cbm.gsi.de/containers/debian12_v18.8.0_nov22p1.sif"
    OS: debian12
    FAIRSOFT_VERSION: nov22p1
    FAIRROOT_VERSION: v18.8.0
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"

.linux_tag: &linux_tag
  stage: build
  tags:
    - CbmRoot

.linux_variables: &linux_variables
    OS: "Debian10.11"
    NCPU: 5
    FAIRSOFT_VERSION: "apr21p2"
    FAIRROOT_VERSION: "v18.6.7"
    BASE_PATH: "/cvmfs/fairsoft.gsi.de/debian10"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_fs_${FAIRSOFT_VERSION}"
    EXTRA_PATH: "${SIMPATH}/bin"

.macosx_tag: &macosx_tag
  stage: build
  tags:
    - qemu_macosx

.macosx_13_variables_nov22p1: &macosx_13_variables_nov22p1
    OS: "macOS13"
    FAIRSOFT_VERSION: "nov22p1"
    FAIRROOT_VERSION: "v18.8.0"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.macosx_12_variables_nov22p1: &macosx_12_variables_nov22p1
    OS: "macOS12"
    FAIRSOFT_VERSION: "nov22p1"
    FAIRROOT_VERSION: "v18.8.0"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.macosx_11_variables_nov22p1: &macosx_11_variables_nov22p1
    OS: "macOS11"
    FAIRSOFT_VERSION: "nov22p1"
    FAIRROOT_VERSION: "v18.8.0"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.macosx_1015_variables: &macosx_1015_variables
    OS: "macOS1015"
    FAIRSOFT_VERSION: "apr21p2"
    FAIRROOT_VERSION: "v18.6.7"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.macosx_12_variables: &macosx_12_variables
    OS: "macOS12"
    FAIRSOFT_VERSION: "apr21p2"
    FAIRROOT_VERSION: "v18.6.7"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.macosx_11_variables: &macosx_11_variables
    OS: "macOS11"
    FAIRSOFT_VERSION: "apr21p2"
    FAIRROOT_VERSION: "v18.6.7"
    BASE_PATH: "/opt"
    SIMPATH: "$BASE_PATH/fairsoft/$FAIRSOFT_VERSION"
    FAIRROOTPATH: "$BASE_PATH/fairroot/${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    IMAGE_NAME: "${OS}_${FAIRROOT_VERSION}_${FAIRSOFT_VERSION}"
    EXTRA_PATH: /usr/local/bin

.mergeVariables: &mergeVariables
    BUILD_TYPE: "Continuous"

.mergeRequestVariables: &mergeRequestVariables
    BUILD_TYPE: "MergeRequest"

.check_merge_request: &check_merge_request
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"

.check_merge: &check_merge
  only:
    refs:
      - master
    variables:
      - $CI_PROJECT_PATH == "computing/cbmroot"

#
# job matrix for build jobs
#

CbmRoot_Merge_realData:
  <<: [*realData_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *realData_variables]

CbmRoot_Merge_Apptainer_debian12_nov22p1:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_debian12_variables_nov22p1]

CbmRoot_Merge_Apptainer_debian10_apr21:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_debian10_variables_apr21]

CbmRoot_Merge_Apptainer_debian10_nov22p1:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_debian10_variables_nov22p1]

CbmRoot_Merge_Apptainer_debian11_apr21:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_debian11_variables_apr21]

CbmRoot_Merge_Apptainer_centos7_apr21:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_centos7_variables_apr21]

CbmRoot_Merge_Apptainer_centos7_nov22p1:
  <<: [*apptainer_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *apptainer_centos7_variables_nov22p1]

CbmRoot_Merge_macosx_13_nov22p1:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_13_variables_nov22p1]

CbmRoot_Merge_macosx_12_nov22p1:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_12_variables_nov22p1]

CbmRoot_Merge_macosx_11_nov22p1:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_11_variables_nov22p1]

CbmRoot_Merge_macosx_1015:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_1015_variables]

CbmRoot_Merge_macosx_11:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_11_variables]

CbmRoot_Merge_macosx_12:
  <<: [*macosx_tag, *check_merge_request, *build_and_test]
  variables:
    <<: [*mergeRequestVariables, *macosx_12_variables]

CbmRoot_Continuous:
  <<: [*linux_tag, *check_merge, *build_and_test]
  variables:
    <<: [*mergeVariables, *linux_variables]

InformCodeOwners:
  stage: finalise
  tags:
    - Code_Owners
  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
    - scripts/inform_codeowners.sh upstream

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
