image: eicweb.phy.anl.gov:4567/eic/juggler/juggler:latest

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "webide"'
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_TAG'

default:
  before_script:
    - mkdir -p images && mkdir -p doc/
    - git clone https://eicweb.phy.anl.gov/EIC/detectors/accelerator.git && ln -s accelerator/eic 
    - git clone https://eicweb.phy.anl.gov/EIC/detectors/ip6.git  eic_ip6 && mkdir ip6_build && cd ip6_build && cmake ../eic_ip6/. -DCMAKE_INSTALL_PREFIX=/usr/local && make -j20 && make install  && cd .. && ln -s eic_ip6/ip6 
    - mkdir build && cd build && cmake ../. -DCMAKE_INSTALL_PREFIX=/usr/local && make -j20 && make install  && cd ..
  artifacts:
    paths:
      - images/
      - doc/
    reports:
      dotenv: juggler.env

stages:
  - config
  - build
  - docs
  - test 
  - finalize 
  - deploy

env:
  stage: config 
  script:
    - export JUGGLER_DETECTOR="reference_detector"
    - |
      if [[ "x${CI_PIPELINE_SOURCE}" == "xmerge_request_event"  || "$CI_COMMIT_BRANCH" == "master" ]]; then
        export JUGGLER_DETECTOR_VERSION="${CI_COMMIT_REF_NAME}"
        echo "BEAMLINE_CONFIG          = ${BEAMLINE_CONFIG}"
        echo "BEAMLINE_CONFIG_VERSION  = ${BEAMLINE_CONFIG_VERSION}"
        echo "JUGGLER_DETECTOR         = ${JUGGLER_DETECTOR}"
        echo "JUGGLER_DETECTOR_VERSION = ${CI_COMMIT_REF_NAME}"
        echo "JUGGLER_DETECTOR_VERSION=$CI_COMMIT_REF_NAME" >> juggler.env
        echo "JUGGLER_DETECTOR=$JUGGLER_DETECTOR" >> juggler.env
      fi
  artifacts:
    reports:
      dotenv: juggler.env


compile:
  stage: build
  needs:
    - ["env"]
  script:
    - mkdir -p build && cd build && cmake ../. -DCMAKE_INSTALL_PREFIX=/usr/local && make -j20 && make install  && cd ..
    - echo "Build successful."

.views:
  stage: docs
  needs:
    - ["compile"]
  artifacts:
    paths:
      - images/
      - doc/

include:
  - local: 'views/view1.yml'
  - local: 'views/view2.yml'
  - local: 'views/view3.yml'
  - local: 'views/view6.yml'
  - local: 'views/view7.yml'
  - local: 'views/config.yml'

report:
  stage: finalize
  needs: 
    - view_01
    - dawn_view_02:detector
    - dawn_view_03:detector
    - dump_constants
  artifacts:
    paths:
      - images/
      - doc/
  script:
    - pip3 install jinja2 &&  ls -lrth
    - ./bin/make_images > doc/dawn_views.md

dump_constants:
  stage: test
  needs: 
    - ["compile"]
  script:
    - npdet_info dump reference_detector.xml  | tee doc/constants.out 

overlap_check:
  stage: test
  needs: 
    - ["compile"]
  script:
    - checkOverlaps -c reference_detector.xml  | tee doc/overlap_check.out 
    - echo "$(cat doc/overlap_check.out | grep ovlp | wc -l) overlaps..."
    - if [[ "$(cat doc/overlap_check.out | grep ovlp | wc -l)" -gt "1" ]] ; then echo "Overlaps exist!" && false ; fi

detector:config_testing:
  stage: test
  needs: 
    - ["compile"]
  script:
    - checkOverlaps -c reference_detector.xml  | tee doc/overlap_check.out | wc -l
    - cat doc/overlap_check.out
  allow_failure: true

    #benchmarks:reconstruction:
    #  stage: deploy
    #  variables:
    #    JUGGLER_DETECTOR: "$JUGGLER_DETECTOR"
    #    JUGGLER_DETECTOR_VERSION: "$JUGGLER_DETECTOR_VERSION"
    #  trigger:
    #    project: EIC/benchmarks/reconstruction_benchmarks
    #    strategy: depend
    #  needs: ["env","overlap_check","report"]
    #  
    #
    #benchmarks:physics:
    #  stage: deploy
    #  variables:
    #    JUGGLER_DETECTOR: "$JUGGLER_DETECTOR"
    #    JUGGLER_DETECTOR_VERSION: "$JUGGLER_DETECTOR_VERSION"
    #  trigger:
    #    project: EIC/benchmarks/physics_benchmarks
    #    strategy: depend
    #  needs: ["env","overlap_check","report"]

