image: eicweb.phy.anl.gov:4567/eic/juggler/juggler:$JUGGLER_TAG

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:
  artifacts:
    expire_in: 72 hours 
    paths:
      - .local/detector
      - .local/lib
      - .local/bin
      - .local/include
      - .local/share
      - results
      - config
      - juggler.env
    reports:
      dotenv: juggler.env

stages:
  - config
  - initialize
  - data_init
  - simulate
  - calibrate
  - benchmarks
  - collect
  - deploy
  - trigger

common:setup:
  stage: config
  before_script:
    - |
      if [[ "${COMMON_BENCH_VERSION}" == "" ]] ; then
        export COMMON_BENCH_VERSION="master" 
      fi
      echo "COMMON_BENCH_VERSION = ${COMMON_BENCH_VERSION}" 
      git clone -b "${COMMON_BENCH_VERSION}" https://eicweb.phy.anl.gov/EIC/benchmarks/common_bench.git setup 
  script:
    - |
      if [[ "x${CI_PIPELINE_SOURCE}" == "xmerge_request_event"  || "$CI_COMMIT_BRANCH" == "master" ]]; then
        echo "JUGGLER_DETECTOR         = ${JUGGLER_DETECTOR}"
        echo "JUGGLER_DETECTOR_VERSION = ${JUGGLER_DETECTOR_VERSION}"
        echo "JUGGLER_DETECTOR=$JUGGLER_DETECTOR" >> juggler.env
        echo "JUGGLER_DETECTOR_VERSION=$JUGGLER_DETECTOR_VERSION" >> juggler.env
        echo "COMMON_BENCH_VERSION=$COMMON_BENCH_VERSION" >> 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
    - print_env.sh

get_data:
  stage: data_init
  needs: ["common:detector"]
  script:
    - source .local/bin/env.sh
    - ln -s "${LOCAL_DATA_PATH}/sim_output" sim_output
    - ln -s "${LOCAL_DATA_PATH}/datasets/data" data
    - cd ${LOCAL_DATA_PATH} && git clone --depth=1 https://eicweb.phy.anl.gov/EIC/datasets.git datasets

.det_benchmark:
  image: eicweb.phy.anl.gov:4567/eic/juggler/juggler:$JUGGLER_TAG
  needs:
    - ["get_data","common:detector"]
  before_script:
    - source .local/bin/env.sh
    - ls -lrtha 
    - ln -s "${LOCAL_DATA_PATH}/sim_output" sim_output
    - ln -s "${LOCAL_DATA_PATH}/datasets/data" data
    - ls -lrtha

include: 
  - local: 'benchmarks/barrel_ecal/config.yml'
  - local: 'benchmarks/barrel_hcal/config.yml'
  - local: 'benchmarks/roman_pots/config.yml'
  - local: 'benchmarks/zdc/config.yml'
  - local: 'benchmarks/crystal_calorimeter/config.yml'
  - local: 'benchmarks/pid/config.yml'
  - local: 'benchmarks/timing/config.yml'

deploy_results:
  stage: deploy
  needs:
    - ["collect_results:zdc","collect_results:barrel_ecal","collect_results:barrel_hcal","collect_results:crystal_calorimeter"]
  script:
    - echo "deploy results!"

benchmarks:reconstruction:
  stage: trigger
  variables:
    JUGGLER_DETECTOR: "$JUGGLER_DETECTOR"
    JUGGLER_DETECTOR_VERSION: "$JUGGLER_DETECTOR_VERSION"
  trigger:
    project: EIC/benchmarks/reconstruction_benchmarks
    strategy: depend
  needs: ["deploy_results"]

benchmarks:physics:
  stage: trigger
  variables:
    JUGGLER_DETECTOR: "$JUGGLER_DETECTOR"
    JUGGLER_DETECTOR_VERSION: "$JUGGLER_DETECTOR_VERSION"
  trigger:
    project: EIC/benchmarks/physics_benchmarks
    strategy: depend
  needs: ["deploy_results"]

pages:
  stage: deploy
  rules:
    - if: '$CI_COMMIT_BRANCH == "master" && $CI_SERVER_HOST != "eicweb.phy.anl.gov"'
  cache:
    paths:
      - node_modules/
  image: node:latest
  script:
    - mkdir public && cp doc/main.html public/index.html
  artifacts:
    paths:
      - public


