# Auto generate docs as a new commit for the docs repo
name: update readme to docs

on:
  workflow_dispatch:
  push:
    branches:
       - main

permissions:
  contents: read

jobs:
  readme-update-to-docs:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v4

    - name: Get README content
      id: get-readme
      run: |
        cp README.md ../new-src-index.md

    - name: see if info is there
      run: |
        echo "ll .."
        ls -al ..
        echo "ll ."
        ls -al .

    - name: add proper description to top of markdown file
      run: |
        FILE="../new-src-index.md"
        TEXT='hi.txt'
        echo '---' >> $TEXT
        echo 'description: "An extendable WebComponents library for the Spatial Web"' >> $TEXT
        echo '---' >> $TEXT
        cat $TEXT $FILE > temp && mv temp $FILE
        echo "<div class='centered'><a href='https://github.com/volumetrics-io/mrjs/edit/main/README.md' target='_blank'>Suggest an edit on GitHub for README.md</a></div>" >> "$FILE"
        cat $FILE

    - name: Checkout documentation Repository
      uses: actions/checkout@v4
      with:
        repository: Volumetrics-io/documentation
        ref: main
        token: ${{ secrets.MRJS_AND_DOCS_REPO_PAT }}

    - name: Update Docs
      run: |
        rm "./source/index.md"
        mv -f "../new-src-index.md" "./source/index.md"

    - name: Commit only if there are changes
      run: |
        if [[ -n $(git diff --exit-code) ]]; then
          echo "Changes detected. Committing and pushing."
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'
          commit_message=$'👷 MRjs - Auto Generated README to Index Update 👷\n\nChanges at '"${GITHUB_SHA}"
          git add .
          git commit -m "$commit_message"
          git push --quiet --set-upstream origin HEAD --force
        else
          echo "No changes detected. Exiting without committing."
        fi
      env:
          GITHUB_TOKEN: ${{ secrets.MRJS_AND_DOCS_REPO_PAT }}

