version: 2
commands:
  setup:
    steps:
      - checkout
      - run:
          name: setup Qemu
          command: |
            docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
      - run:
          name: Create builder
          command: |
            export DOCKER_CLI_EXPERIMENTAL=enabled
            docker context create multi-arch-build
            docker buildx create --use multi-arch-build
jobs:
  checkstyle:
    docker:
      - image: ubuntu:focal

    environment:
      WM_NCOMPPROCS: 2
      TZ: Europe/Paris
      DEBIAN_FRONTEND: noninteractive

    working_directory: /root/sedfoam
    steps:
      - run:
          name: Make project and user dir
          command: mkdir -p /root/sedfoam

      - checkout:
          path: /root/sedfoam

      - run:
          name: Install vera++
          command: >
            apt-get update &&
            apt-get install -y ca-certificates git vera++

      - run:
          name: pull checkStyle submodule
          command: >
            shopt -s expand_aliases &&
            git submodule init &&
            git submodule update --remote

      - run:
          name: Check style
          command: >
            shopt -s expand_aliases &&
            ./foamStyleCheck/checkStyle

  docs-build:
    docker:
      - image: opencfd/openfoam-dev:2212

    environment:
      WM_NCOMPPROCS: 2
      TZ: Europe/Paris
      DEBIAN_FRONTEND: noninteractive

    working_directory: /root/sedfoam
    steps:
      - run:
          name: Install docs packages
          command: >
            update-ca-certificates &&
            apt-get update && apt-get -y install ca-certificates git python3-pip dvisvgm libclang-14-dev &&
            apt-get -y install ack-grep libclang-cpp14 wget graphviz tex-gyre texlive-base &&
            apt-get -y install texlive-latex-extra texlive-fonts-extra texlive-fonts-recommended

      - run:
          name: Install python packages
          command: >
            shopt -s expand_aliases && python3 --version &&
            pip3 --version &&
            pip3 install -U jinja2 Pygments

      - run:
          name: Make project and user dir
          command: mkdir -p /root/sedfoam

      - checkout:
          path: /root/sedfoam

      - run:
          name: install doxygen 1.9.3
          command: >
            shopt -s expand_aliases &&
            wget https://sourceforge.net/projects/doxygen/files/rel-1.9.3/doxygen-1.9.3.linux.bin.tar.gz &&
            tar -xvf doxygen-1.9.3.linux.bin.tar.gz

      - run:
          name: clone m.css and build doc
          command: >
            shopt -s expand_aliases &&
            export PATH=$PWD/doxygen-1.9.3/bin:$PATH &&
            cd doc &&
            git clone git@github.com:mosra/m.css &&
            cd m.css/documentation && cp ../../doxygen.py . &&
            source /openfoam/bash.rc &&
            python3 doxygen.py ../../conf.py &&
            cd ../.. && cp images/* mcssout/html/. &&
            ack "ource file" -l --print0 | xargs -0 -n 1 sed -i "s/\"\/root\/sedfoam/\"https:\/\/github.com\/sedfoam\/sedfoam\/blob\/master/g"

      - persist_to_workspace:
          root: /root/sedfoam/doc/mcssout
          paths: html

  docs-deploy:
    docker:
      - image: node:17.8.0
    steps:
      - checkout
      - attach_workspace:
          at: docs/_build
      - run:
          name: Install and configure dependencies
          command: |
            npm install -g --silent gh-pages@2.0.1
            git config user.email "cyrille.bonamy@univ-grenoble-alpes.fr"
            git config user.name "CyrilleBonamy"
      - add_ssh_keys:
          fingerprints:
          - $GITHUB_TOKEN
      - run:
          name: Deploy docs to gh-pages branch
          command: gh-pages --dotfiles --message "[skip ci] Updates" --dist docs/_build/html

  build:
    docker:
      - image: opencfd/openfoam-dev:2212

    environment:
      WM_NCOMPPROCS: 2
      TZ: Europe/Paris
      DEBIAN_FRONTEND: noninteractive

    working_directory: /root/sedfoam

    steps:
      - run:
          name: Make project and user dir
          command: mkdir -p /root/sedfoam

      - checkout:
          path: /root/sedfoam

      - run:
          name: pull submodules
          command: >
            update-ca-certificates &&
            apt-get update && apt-get -y install ca-certificates git &&
            shopt -s expand_aliases &&
            git submodule init &&
            git submodule update --remote

      - run:
          name: Build sedFoam
          command: >
            shopt -s expand_aliases && source /openfoam/bash.rc &&
            export FOAM_USER_LIBBIN=$FOAM_SITE_LIBBIN && ./Allwmake -prefix=group
      - persist_to_workspace:
          root: /usr/lib/openfoam/openfoam2212
          paths: site

  test:
    docker:
      - image: opencfd/openfoam-dev:2212
    working_directory: /root/sedfoam

    steps:
      - run:
          name: Install python3 packages
          command: >
            shopt -s expand_aliases &&
            update-ca-certificates &&
            apt-get update && apt-get -y install python3 &&
            apt-get -y install python3-pip &&
            pip3 install fluidfoam

      - checkout:
          path: /root/sedfoam

      - attach_workspace:
          at: /usr/lib/openfoam/openfoam2212

      - run:
          name: sedfoam test1D sedim
          command: >
            shopt -s expand_aliases && source /openfoam/bash.rc &&
            cd test-ci/1DSedim && cp -f constant/forceProperties.sedim constant/forceProperties &&
            cp -f system/controlDict.sedim system/controlDict  && chmod -R a+rwX /root &&
            runuser -l sudofoam -c 'source /openfoam/bash.rc && cd /root/sedfoam/test-ci/1DSedim && ./Allrun' &&
            python3 test_Sedimentation.py
      - run:
          name: sedfoam test1D bedload
          command: >
            shopt -s expand_aliases && source /openfoam/bash.rc &&
            cd /root/sedfoam/test-ci/1DSedim && cp -f constant/forceProperties.bedload constant/forceProperties &&
            cp -f system/controlDict.bedload system/controlDict && chmod -R a+rwX /root &&
            runuser -l sudofoam -c 'cd /root/sedfoam/test-ci/1DSedim &&
            source /openfoam/bash.rc && ./Allrun' &&
            python3 test_Bedload.py
          no_output_timeout: 30m

  advanced-test:
    docker:
      - image: opencfd/openfoam-dev:2212
    working_directory: /root/sedfoam

    steps:
      - run:
          name: Install python3 packages
          command: >
            shopt -s expand_aliases &&
            update-ca-certificates &&
            apt-get update &&
            apt-get -y install python3 python3-pip &&
            pip3 install fluidfoam

      - checkout:
          path: /root/sedfoam

      - attach_workspace:
          at: /usr/lib/openfoam/openfoam2212

      - run:
          name: sedfoam 1D AvalancheMuI
          command: >
            shopt -s expand_aliases && chmod -R a+rwX /root &&
            source /openfoam/bash.rc &&
            runuser -l sudofoam -c 'source /openfoam/bash.rc && cd /root/sedfoam/test-ci/1DAvalancheMuI && ./Allrun' &&
            cd /root/sedfoam/test-ci/1DAvalancheMuI && python3 test_1DAvalancheMuI.py
          no_output_timeout: 30m

  advanced-test1:
    docker:
      - image: opencfd/openfoam-dev:2212
    working_directory: /root/sedfoam

    steps:
      - run:
          name: Install python3 packages
          command: >
            shopt -s expand_aliases &&
            update-ca-certificates &&
            apt-get update &&
            apt-get -y install python3 python3-pip &&
            pip3 install fluidfoam

      - checkout:
          path: /root/sedfoam

      - attach_workspace:
          at: /usr/lib/openfoam/openfoam2212

      - run:
          name: sedfoam 1DBedLoadTurb
          command: >
            shopt -s expand_aliases && chmod -R a+rwX /root &&
            source /openfoam/bash.rc &&
            runuser -l sudofoam -c 'source /openfoam/bash.rc && cd /root/sedfoam/test-ci/1DBedLoadTurb && ./Allrun' &&
            cd /root/sedfoam/test-ci/1DBedLoadTurb && python3 test_1DBedLoadTurb.py
          no_output_timeout: 30m

      - run:
          name: sedfoam 1DBoundaryLayer
          command: >
            shopt -s expand_aliases && chmod -R a+rwX /root &&
            source /openfoam/bash.rc &&
            runuser -l sudofoam -c 'cd /root/sedfoam/test-ci/1DBoundaryLayer &&
            source /openfoam/bash.rc && ./Allrun' &&
            cd /root/sedfoam/test-ci/1DBoundaryLayer && python3 test_1DBoundaryLayer.py
          no_output_timeout: 30m

  advanced-test2:
    docker:
      - image: opencfd/openfoam-dev:2212
    working_directory: /root/sedfoam

    steps:
      - run:
          name: Install python3 packages
          command: >
            shopt -s expand_aliases &&
            update-ca-certificates &&
            apt-get update &&
            apt-get -y install python3 python3-pip &&
            pip3 install fluidfoam

      - checkout:
          path: /root/sedfoam

      - attach_workspace:
          at: /usr/lib/openfoam/openfoam2212

      - run:
          name: sedfoam 1DWetAvalanche
          command: >
            shopt -s expand_aliases && chmod -R a+rwX /root && source /openfoam/bash.rc &&
            runuser -l sudofoam -c 'source /openfoam/bash.rc && cd /root/sedfoam/test-ci/1DWetAvalanche && ./Allrun' &&
            cd /root/sedfoam/test-ci/1DWetAvalanche && python3 test_1DWetAvalanche.py
          no_output_timeout: 30m

# Orchestrate our job run sequence
workflows:
  version: 2
  build_and_test:
    jobs:
      - checkstyle
      - build
      - test:
          requires:
            - build
      - advanced-test1:
          requires:
            - build
      - advanced-test2:
          requires:
            - build
#      - docs-build
#      - docs-deploy:
#          requires:
#            - docs-build
#          filters:
#            branches:
#              only:
#                - master
#                - develop
  weekly:
    triggers:
      - schedule:
          cron: "0 2 * * 0"
          filters:
            branches:
              only:
                - develop
    jobs:
      - checkstyle
      - build
      - test:
          requires:
            - build
      - advanced-test:
          requires:
            - build
      - advanced-test1:
          requires:
            - build
      - advanced-test2:
          requires:
            - build
#      - docs-build
#      - docs-deploy:
#          requires:
#            - docs-build

