---
version: 2.1

orbs:
  capture-tag: rvla/capture-tag@0.0.2
jobs:
  make_docs:
    docker:
      - image: mydumper/mydumper-builder-focal
    steps:
    - run:
        command: |
          git config --global user.name "David Ducos"
          git config --global user.email "david.ducos@gmail.com"
          git clone https://github.com/mydumper/mydumper_docs.git mydumper_docs
          git clone https://github.com/mydumper/mydumper.git mydumper
          sudo percona-release setup ps80
          sudo apt-get update
          sudo apt install libperconaserverclient21-dev pip sphinx-common python3.9
          cd mydumper/
          cmake .
          make
          ./mydumper --help | while read line; do if [[ ${line:0:1} == '-' ]]; then echo -e ".. option:: $line" | sed 's/  /\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n /\n/g;s/\n/\n\n  /g' ; echo ; fi; if [[ ${line:0:1} != '-' ]] && [[ ${line} == *'Options'* ]] ; then echo "${line}"; echo "$line" | sed 's/./-/g';  fi; done > ../mydumper_docs/mydumper_usage_autogenerated.rst
          ./myloader --help | while read line; do if [[ ${line:0:1} == '-' ]]; then echo ; echo -e ".. option:: $line" | sed 's/  /\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n /\n/g;s/\n/\n\n  /g' ; fi; if [[ ${line:0:1} != '-' ]] && [[ ${line} == *'Options'* ]] ; then if [[ ${line} != *'AFTER_IMPORT_PER_TABLE'* ]]; then echo; echo "${line}"; echo "$line" | sed 's/./-/g'; else echo "  ${line}"; fi; fi; done > ../mydumper_docs/myloader_usage_autogenerated.rst
          cd ../mydumper_docs/
          /usr/bin/python3.9 -m pip install furo sphinx_copybutton sphinx-inline-tabs
          cmake .
          make clean
          make
          git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/mydumper/mydumper_docs.git
          git add .
          git commit -am "[skip ci] Building html" && git push || true 

  publish_docs:
    docker:
      - image: mydumper/mydumper-builder-noble
    steps:
    - run:
        command: |
          git config --global user.name "David Ducos"
          git config --global user.email "david.ducos@gmail.com"
          git clone https://github.com/mydumper/mydumper.git mydumper
          cd mydumper
          git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/mydumper/mydumper.git
          curl https://github.com/mydumper/mydumper_docs/info/refs?service=git-upload-pack --output ../latest_commit
          git update-index --cacheinfo 160000,$(head -2 ../latest_commit | tail -1 | cut -b9-48),docs
          git commit -am "[skip ci] Auto updated submodule references" && git push
          #          cmake . && make
          #    - run:
          #        command: |
          #          git clone https://github.com/mydumper/mydumper_doc.git mydumper_doc
          #          mydumper/mydumper --help | while read line; do if [[ ${line:0:1} == '-' ]]; then echo -e ".. option:: $line" | sed 's/  /\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n /\n/g;s/\n/\n\n  /g' ; echo ; fi; if [[ ${line:0:1} != '-' ]] && [[ ${line} == *'Options'* ]] ; then echo "${line}"; echo "$line" | sed 's/./-/g';  fi; done > mydumper_doc/mydumper_usage_autogenerated.rst
          #          mydumper/myloader --help | while read line; do if [[ ${line:0:1} == '-' ]]; then echo ; echo -e ".. option:: $line" | sed 's/  /\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n\n/\n/g;s/\n /\n/g;s/\n/\n\n  /g' ; fi; if [[ ${line:0:1} != '-' ]] && [[ ${line} == *'Options'* ]] ; then if [[ ${line} != *'AFTER_IMPORT_PER_TABLE'* ]]; then echo; echo "${line}"; echo "$line" | sed 's/./-/g'; else echo "  ${line}"; fi; fi; done > mydumper_doc/myloader_usage_autogenerated.rst
          #          cd mydumper_doc
          #          cmake . && make
          #          git commit -am "[skip ci] Auto updated  references" && git push




workflows:
  version: 2
  mydumper_docs:
    jobs:
    - make_docs
    - publish_docs:
        requires:
          - make_docs
