image: eicweb.phy.anl.gov:4567/containers/eic_container/jug_xl:3.1-unstable

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:
    - source .local/bin/env.sh
  artifacts:
    expire_in: 1 week 
    paths:
      - .local/detector
      - .local/lib
      - .local/bin
      - .local/include
      - .local/share
      - results
      - config
      - images
      - doc
      - juggler.env
      - geo
    reports:
      dotenv: juggler.env

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

common:setup:
  stage: config
  rules:
    - if: '$CI_SERVER_HOST == "eicweb.phy.anl.gov"' 
  before_script:
    - git clone https://eicweb.phy.anl.gov/EIC/benchmarks/common_bench.git setup 
  script:
    - export JUGGLER_DETECTOR="athena"
    - |
      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
    - source setup/bin/env.sh && ./setup/bin/install_common.sh

common:detector:
  stage: initialize
  needs: ["common:setup"]
  script:
    - source .local/bin/env.sh && build_detector.sh
    - mkdir_local_data_link sim_output
    - mkdir -p results
    - mkdir -p config
    - mkdir -p doc
    - mkdir -p geo
    - print_env.sh

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

.views:
  stage: docs
  needs:
    - ["common:detector"]

include:
  - local: 'views/view1.yml'
  - local: 'views/view2.yml'
  - local: 'views/view3.yml'
  - local: 'views/view6.yml'
  - local: 'views/view7.yml'
  - local: 'views/view11.yml'
  - local: 'views/view12.yml'
  - local: 'views/view13.yml'
  - local: 'views/view14.yml'
  - local: 'views/view15.yml'

dump_geometry:
  stage: test
  needs:
    - common:detector
  script:
   - echo "dumping geometry"
   - mkdir -p geo
   ## full geo with downstream beamline
   - dd_web_display --output geo/detector_geo_full.root ${DETECTOR_PATH}/athena.xml
   ## only central detector geo
   - sed -i '/forward_ion_beamline/d' ${DETECTOR_PATH}/athena.xml
   - sed -i '/far_forward/d' ${DETECTOR_PATH}/athena.xml
   - dd_web_display --output geo/detector_geo.root ${DETECTOR_PATH}/athena.xml

report:
  stage: finalize
  needs: 
    - view_01
    - view_02
    - view_03
    - view_06
    - view_07
    - view_11
    - view_12
    - view_13
    - view_14
    - view_15
    - dump_constants
    - dump_geometry
    - convert_to_gdml
  script:
    - pip3 install jinja2 &&  ls -lrth
    - ./bin/make_images > doc/dawn_views.md

dump_constants:
  stage: test
  needs: 
    - ["common:detector"]
  script:
    - npdet_info dump ${DETECTOR_PATH}/athena.xml  | tee doc/constants.out 

overlap_check_tgeo:
  stage: test
  needs: 
    - ["common:detector"]
  script:
    - checkOverlaps -c ${DETECTOR_PATH}/athena.xml  | tee doc/overlap_check_tgeo.out 
    - echo "$(cat doc/overlap_check_tgeo.out | grep ovlp | wc -l) overlaps..."
    - if [[ "$(cat doc/overlap_check_tgeo.out | grep ovlp | wc -l)" -gt "0" ]] ; then echo "Overlaps exist!" && false ; fi

overlap_check_geant4:
  stage: test
  needs: 
    - ["common:detector"]
  script:
    - python scripts/checkOverlaps.py -c ${DETECTOR_PATH}/athena.xml | tee doc/overlap_check_geant4.out
    - echo "$(cat doc/overlap_check_geant4.out | grep GeomVol1002 | wc -l) overlaps..."
    - if [[ "$(cat doc/overlap_check_geant4.out | grep GeomVol1002 | wc -l)" -gt "0" ]] ; then echo "Overlaps exist!" && false ; fi

convert_to_gdml:
  stage: test
  needs: 
    - ["common:detector"]
  script:
    - mkdir -p geo
    - python scripts/convert_to_gdml.py --compact ${DETECTOR_PATH}/athena.xml --output geo/athena.gdml

tracking_geometry_debug:
  stage: test
  allow_failure: true
  needs: 
    - ["common:detector"]
  script:
    - root -b -q "scripts/test_ACTS.cxx+(\"${DETECTOR_PATH}/athena.xml\")" | tee geo/tracking_geometry_debug.out 

detector:config_testing:
  stage: test
  needs: 
    - ["common:detector"]
  script:
    - checkOverlaps -o 's' -c ${DETECTOR_PATH}/athena.xml  | tee doc/overlap_check.out | wc -l
    - cat doc/overlap_check.out

benchmarks:detector:
  stage: deploy
  variables:
    JUGGLER_DETECTOR: "$JUGGLER_DETECTOR"
    JUGGLER_DETECTOR_VERSION: "$JUGGLER_DETECTOR_VERSION"
  trigger:
    project: EIC/benchmarks/detector_benchmarks
    strategy: depend
  needs: ["overlap_check_tgeo","overlap_check_geant4","report"]
  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_tgeo","overlap_check_geant4","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_tgeo","overlap_check_geant4","report"]