on:
  push:
    branches: ["mca2html"]
  pull_request:
  schedule:
    - cron: 0 0 * * * # every morning

jobs:
  generate-artifact:
    runs-on: ubuntu-latest
    continue-on-error: true  # ← If a job fails, do not make it a total failure
    strategy:
      matrix:
        infotheo:
          - '0.9.4'
        image:
          - 'yoshihiro503/mathcomp-analysis:1.12.0-coq-8.20-ocaml-4.14-flambda'
      fail-fast: false
    steps:
      - uses: actions/checkout@v4
      - name: set the variables for push event
        if: ${{ github.event_name == 'push' }}
        run: |
          branch_name=${{ github.ref_name }}
          short_sha=$(git rev-parse --short ${{ github.sha }})
          echo "destination_dir=sample_"$branch_name_$short_sha >> $GITHUB_ENV

      - name: set the variables for PR event
        if: ${{ github.event_name == 'pull_request' }}
        run: |
          branch_name=${{ github.head_ref }}
          echo "destination_dir=sample_$branch_name" >> $GITHUB_ENV

      - name: set the variables for scheduled event
        if: ${{ github.event_name == 'schedule' }}
        run: |
          name=${{ github.action }}
          echo "destination_dir=nightly_$name" >> $GITHUB_ENV

      - name: Checkout Infotheowith a specific tag
        uses: actions/checkout@v4
        with:
          repository: affeldt-aist/infotheo
          path: infotheo
          ref: ${{ matrix.infotheo }}
      - uses: coq-community/docker-coq-action@v1
        with:
          opam_file: 'rocq-navi.opam'
          custom_image: ${{ matrix.image }}
          before_script: |
            startGroup "Build Rocqnavi"
              sudo apt-get install -y graphviz
              sudo chown -R 1000:1000 .
              make
              cd infotheo/
              opam install -y coq-interval coq-mathcomp-algebra-tactics coq-mathcomp-reals-stdlib
              make all
              cd ..
            endGroup
          script: |
            startGroup "Do Rocqnavi"
              export SOURCE=infotheo
              export REVISION=${{ matrix.infoetheo }}-rocqnavi-${{ env.destination_dir }}
              export DIR=$(pwd `dirname .`)
              export OUTDIR=$DIR/html/infotheo-$REVISION
              ./tools/generate-infotheo.sh
            endGroup

      - name: Upload htmldoc as artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.destination_dir }}
          path: |
            html

