stages:
    - build
    - view
    - test
    - dockerise

variables:
    NAV_BUILD_IMAGE_TAG:
        value: "26.03.02"
    PERC_BUILD_IMAGE_TAG:
        value: "24.12.05"
    AMD64_BUILD_NAV_IMAGE:
        value: $CI_REGISTRY/boston/amd64/ros2stack-nav-builder:26.03.02
    ARM64V8_BUILD_NAV_IMAGE:
        value: $CI_REGISTRY/boston/arm64v8/ros2stack-nav-builder:26.03.02
    AMD64_BUILD_PERC_IMAGE:
        value: $CI_REGISTRY/boston/amd64/ros2stack-perc-builder:24.12.05
    ARM64V8_BUILD_PERC_IMAGE:
        value: $CI_REGISTRY/boston/arm64v8/ros2stack-perc-builder:24.12.05
    AMD64_DOCKER_IMAGE:
        value: $CI_REGISTRY/devops/docker:23.0-klnx
        description: Docker image for `dind` pipeline jobs.
    ARM64V8_DOCKER_IMAGE:
        value: $CI_REGISTRY/devops/arm64v8/docker:23.0-klnx
        description: Docker image for `dind` pipeline jobs in ARM architecture.
    AMD64_DOCKER_SERVICE:
        value: $CI_REGISTRY/devops/docker:23.0-dind-klnx
        description: Service to start for `dind`.
    ARM64V8_DOCKER_SERVICE:
        value: $CI_REGISTRY/devops/arm64v8/docker:23.0-dind-klnx
        description: Service to start for `dind` in ARM architecture.
    DIND_ALIAS:
        value: "dind"
        description: Hostname alias used to reference DIND service.
    DOCKER_HOST:
        value: "tcp://$DIND_ALIAS:2375"
        description: Indicates not to use TLS.
    DOCKER_TLS_CERTDIR:
        value: ""
        description: Indicates not to use TLS.
    DOCKER_DRIVER:
        value: "overlay2"
        description: Driver specification

.block:build:
    stage: build
    before_script:
        - id
        - echo $CI_PROJECT_DIR
        - pwd
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - $CI_PROJECT_DIR/ci/version-info.sh $CI_PROJECT_DIR
        - ls -al
        - mkdir -p $CI_PROJECT_DIR/colcon_ws/src/third_party
        - source /opt/ros/$ROS_DISTRO/setup.bash
        - source /opt/ouster/install/setup.bash
        - ln -s $CI_PROJECT_DIR/pkgs colcon_ws/src/pkgs
        - cd $CI_PROJECT_DIR/colcon_ws
    after_script:
        - mv $CI_PROJECT_DIR/ros2stack.ver $CI_PROJECT_DIR/colcon_ws/install/ros2stack.ver

.block:dockerise:
    stage: dockerise
    before_script:
        - sleep 20s
        - docker info
        - docker login --username "$CI_REGISTRY_USER" --password "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
        - pwd

.block:dockerise:amd64:
    extends: .block:dockerise
    tags:
        - amd64
    inherit:
        variables:
            [
                AMD64_DOCKER_IMAGE,
                AMD64_DOCKER_SERVICE,
                DIND_ALIAS,
                DOCKER_HOST,
                DOCKER_TLS_CERTDIR,
                DOCKER_DRIVER,
                NAV_BUILD_IMAGE_TAG,
                PERC_BUILD_IMAGE_TAG,
            ]
    image: $AMD64_DOCKER_IMAGE
    services:
        - name: $AMD64_DOCKER_SERVICE
          alias: $DIND_ALIAS
    after_script:
        - docker image ls
        - docker system prune -af --volumes
        - docker rmi $(docker images -q) || true
        - docker image ls

.block:dockerise:arm64v8:
    extends: .block:dockerise
    tags:
        - arm64
    inherit:
        variables:
            [
                ARM64V8_DOCKER_IMAGE,
                ARM64V8_DOCKER_SERVICE,
                NAV_BUILD_IMAGE_TAG,
                PERC_BUILD_IMAGE_TAG,
            ]
    image: $ARM64V8_DOCKER_IMAGE
    services:
        - $ARM64V8_DOCKER_SERVICE
    after_script:
        - docker image ls

.block:view:
    stage: view
    script:
        - pwd
        - tree colcon_ws/install/
        - cat colcon_ws/install/ros2stack.ver
        - du --human-readable --max-depth 1 colcon_ws/install/

.block:linter:
    stage: test
    tags:
        - amd64
    inherit:
        variables: [AMD64_BUILD_NAV_IMAGE]
    image: $AMD64_BUILD_NAV_IMAGE
    before_script:
        - echo $CI_PROJECT_DIR
        - pwd
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - ls -al
        - cd $CI_PROJECT_DIR
    needs: []

.block:build:nav:
    extends: .block:build
    script:
        - $CI_PROJECT_DIR/ci/clone-third-party.py
          --input $CI_PROJECT_DIR/nav-third-party.yml --path src/third_party
        - rosdep install --default-yes --ignore-src --rosdistro $ROS_DISTRO
          --skip-keys="Eigen GTSAM OpenCV PCL"
          --from-paths src/third_party
          --from-paths src/pkgs/common
          --from-paths src/pkgs/nav
        - touch src/pkgs/perc/COLCON_IGNORE
        - colcon build --executor sequential

.block:build:perc:
    extends: .block:build
    script:
        - $CI_PROJECT_DIR/ci/clone-third-party.py
          --input $CI_PROJECT_DIR/perc-third-party.yml --path src/third_party
        - rosdep install --default-yes --ignore-src --rosdistro $ROS_DISTRO
          --from-paths src/third_party
          --from-paths src/pkgs/common
          --from-paths src/pkgs/perc
        - touch src/pkgs/nav/COLCON_IGNORE
        - colcon build --executor sequential

amd64:build:nav:
    extends: .block:build:nav
    tags:
        - amd64
    inherit:
        variables: [AMD64_BUILD_NAV_IMAGE]
    image: $AMD64_BUILD_NAV_IMAGE
    artifacts:
        name: "amd64-nav-colcon-output"
        paths:
            - $CI_PROJECT_DIR/colcon_ws/install
            - $CI_PROJECT_DIR/colcon_ws/log
            - $CI_PROJECT_DIR/cyclonedds_configs

amd64:build:perc:
    extends: .block:build:perc
    tags:
        - amd64
    inherit:
        variables: [AMD64_BUILD_PERC_IMAGE]
    image: $AMD64_BUILD_PERC_IMAGE
    artifacts:
        name: "amd64-perc-colcon-output"
        paths:
            - $CI_PROJECT_DIR/colcon_ws/install
            - $CI_PROJECT_DIR/colcon_ws/log
            - $CI_PROJECT_DIR/cyclonedds_configs

arm64v8:build:nav:
    extends: .block:build:nav
    tags:
        - arm64
    inherit:
        variables: [ARM64V8_BUILD_NAV_IMAGE]
    image: $ARM64V8_BUILD_NAV_IMAGE
    artifacts:
        name: "arm64v8-nav-colcon-output"
        paths:
            - $CI_PROJECT_DIR/colcon_ws/install
            - $CI_PROJECT_DIR/colcon_ws/log
            - $CI_PROJECT_DIR/cyclonedds_configs

arm64v8:build:perc:
    extends: .block:build:perc
    tags:
        - arm64
    inherit:
        variables: [ARM64V8_BUILD_PERC_IMAGE]
    image: $ARM64V8_BUILD_PERC_IMAGE
    artifacts:
        name: "arm64v8-perc-colcon-output"
        paths:
            - $CI_PROJECT_DIR/colcon_ws/install
            - $CI_PROJECT_DIR/colcon_ws/log
            - $CI_PROJECT_DIR/cyclonedds_configs

amd64:linter:cmake-format:
    extends: .block:linter
    script:
        - ci/check-cmake-format.sh

amd64:linter:cpp:clang-format:
    extends: .block:linter
    script:
        - ci/check-clang-format.sh

amd64:linter:python:flake8:
    extends: .block:linter
    script:
        - ci/check-flake8.sh

amd64:linter:python:black:
    extends: .block:linter
    script:
        - ci/check-black.sh

amd64:linter:xml:
    extends: .block:linter
    script:
        - ci/check-xmllint.sh

.amd64:linter:yaml:
    extends: .block:linter
    script:
        - ci/check-yamllint.sh

amd64:view:nav:colcon-install:
    extends: .block:view
    tags:
        - amd64
    inherit:
        variables: [AMD64_BUILD_NAV_IMAGE]
    image: $AMD64_BUILD_NAV_IMAGE
    needs: ["amd64:build:nav"]

amd64:view:perc:colcon-install:
    extends: .block:view
    tags:
        - amd64
    inherit:
        variables: [AMD64_BUILD_PERC_IMAGE]
    image: $AMD64_BUILD_PERC_IMAGE
    needs: ["amd64:build:perc"]

arm64v8:view:nav:colcon-install:
    extends: .block:view
    tags:
        - arm64
    inherit:
        variables: [ARM64V8_BUILD_NAV_IMAGE]
    image: $ARM64V8_BUILD_NAV_IMAGE
    needs: ["arm64v8:build:nav"]

arm64v8:view:perc:colcon-install:
    extends: .block:view
    tags:
        - arm64
    inherit:
        variables: [ARM64V8_BUILD_PERC_IMAGE]
    image: $ARM64V8_BUILD_PERC_IMAGE
    needs: ["arm64v8:build:perc"]

amd64:dockerise:nav:build-only:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -r $CI_COMMIT_REF_SLUG
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["amd64:build:nav"]

amd64:dockerise:perc:build-only:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -r $CI_COMMIT_REF_SLUG
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["amd64:build:perc"]

arm64v8:dockerise:nav:build-only:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -r $CI_COMMIT_REF_SLUG
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:nav"]

arm64v8:dockerise:perc:build-only:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -r $CI_COMMIT_REF_SLUG
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:perc"]

amd64:dockerise:nav:main:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -p -r $CI_DEFAULT_BRANCH
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["amd64:build:nav"]
    rules:
        - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

amd64:dockerise:perc:main:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -p -r $CI_DEFAULT_BRANCH
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["amd64:build:perc"]
    rules:
        - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

arm64v8:dockerise:nav:main:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -p -r $CI_COMMIT_REF_SLUG
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:nav"]
    rules:
        - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

arm64v8:dockerise:perc:main-r $CI_COMMIT_REF_SLUG:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -p -r $CI_COMMIT_REF_SLUG
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:perc"]
    rules:
        - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

amd64:dockerise:manual:nav:build+push:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -p -r $CI_COMMIT_REF_SLUG
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["amd64:build:nav"]
    when: manual

amd64:dockerise:manual:perc:build+push:
    extends: .block:dockerise:amd64
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -p -r $CI_COMMIT_REF_SLUG
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["amd64:build:perc"]
    when: manual

arm64v8:dockerise:manual:nav:build+push:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -p -r $CI_COMMIT_REF_SLUG
          -s nav -t $NAV_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:nav"]
    when: manual

arm64v8:dockerise:manual:perc:build+push:
    extends: .block:dockerise:arm64v8
    script:
        - $CI_PROJECT_DIR/docker/ci.build.sh -a -p -r $CI_COMMIT_REF_SLUG
          -s perc -t $PERC_BUILD_IMAGE_TAG
    needs: ["arm64v8:build:perc"]
    when: manual
