name: Deploy artifact

on:
  push:
    branches:

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  generate-deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    strategy:
      matrix:
        analysis_tag:
          - '1.16.0'
        analysis_image:
          - 'yoshihiro503/mathcomp-analysis:1.12.0-coq-8.20-ocaml-4.14-flambda'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set Short Commit Hash
        run: |
          echo "COMMIT_SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Setup Graphviz
        uses: ts-graphviz/setup-graphviz@v2

      - name: Set-up OCaml
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: 4.14
          opam-repositories: |
            coq-released: https://coq.inria.fr/opam/released
            default: https://github.com/ocaml/opam-repository.git

      - name: Build rocqnavi
        run: opam install -y . --deps-only --with-test && opam exec -- make

      - name: Setup Coq temporary fix for the Dependency Graph
        run: opam install -y coq.8.20.1

      - name: top site
        run: mkdir html && cp -r .github/workflows/resources/* html/

      ## Amidakuji -------------------
      - name: Clone amidakuji
        uses: actions/checkout@v4
        with:
          repository: yoshihiro503/rocq-amidakuji
          path: rocq-amidakuji

      - name: Build Amidakuji
        run: cd rocq-amidakuji && opam exec -- ./configure.sh && opam exec -- make
      - run: |
             opam exec -- tools/generate-amidakuji.sh "(rocqnavi-${{ env.COMMIT_SHORT_SHA }})"
             sed -i '/<!-- ADD_ITEM -->/ i <li><a href="rocq-amidakuji">rocq-amidakuji</a></li>' html/index.html
      ## ----------------------------

      ## Mathcomp Analysis ----------
      - uses: coq-community/docker-coq-action@v1
        with:
          opam_file: 'rocq-navi.opam'
          custom_image: ${{ matrix.analysis_image }}
          before_script: |
            startGroup "Build Rocqnavi"
              pwd
              ls -la
              ls -la ocamldot/
              sudo apt-get install -y graphviz
              sudo chown -R 1000:1000 .
              make clean
              make
            endGroup
          script: |
            startGroup "Do Rocqnavi"
              export MATHCOMP_ANALYSIS=$OPAM_SWITCH_PREFIX/lib/coq/user-contrib/mathcomp/
              export REVISION=${{ matrix.analysis_tag }}-rocqnavi-${{ env.COMMIT_SHORT_SHA }}
              export DIR=$(pwd `dirname .`)
              export OUTDIR=$DIR/html/analysis-$REVISION
              export ANALYSIS_SRC=$OPAM_SWITCH_PREFIX/.opam-switch/sources/coq-mathcomp-analysis
              ./tools/generate-mathcomp-analysis.sh
              sed -i "/<!-- ADD_ITEM -->/ i <li><a href=\"analysis-$REVISION\">Math-Comp Analysis</a></li>" html/index.html
            endGroup
          export: 'OPAMWITHTEST'  # space-separated list of variables
        env:
          OPAMWITHTEST: 'true'
      ## ----------------------------

      - name: Setup Pages
        uses: actions/configure-pages@v6

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v5
        with:
          path: 'html/'

  deploy:
    if: github.ref == 'refs/heads/mca2html'
    needs: generate-deploy
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5
