# This is a basic workflow to help you get started with Actions

name: Links

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  #push:
  #  branches: [ main ]
  #pull_request:
  #  branches: [ main ]
  #schedule:
  #  - cron: "00 18 * * *"

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

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  Splitting_files_into_batches:
    runs-on: ubuntu-latest
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - name: Creating artifacts directory
        id: create_artifacts_dir
        run: mkdir -pv "$MD_ARTIFACTS_DIR"

      - name: Find all markdown files and create batch
        id: find_md_files
        run: |
          export md_files_arr=($(find . -name '*.md' -type f | sed 's|^./||'))
          echo "${md_files_arr}" > "${MD_ARTIFACTS_DIR}/MD_FILES.txt"
          echo "${md_files_arr[@]:0:50}" | tr ' ' '\n' > "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_1.txt"
          echo "${md_files_arr[@]:50:50}" | tr ' ' '\n' > "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_2.txt"
          echo "${md_files_arr[@]:100:50}" | tr ' ' '\n' > "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_3.txt"
          echo "${md_files_arr[@]:150:50}" | tr ' ' '\n' > "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_4.txt"
          echo "${md_files_arr[@]:200}" | tr ' ' '\n' > "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_5.txt"
          echo "First batch has $(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_1.txt" | wc -l) file."
          echo "Second batch has $(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_2.txt" | wc -l) file."
          echo "Third batch has $(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_3.txt" | wc -l) file."
          echo "Fourth batch has $(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_4.txt" | wc -l) file."
          echo "Fifth batch has $(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_5.txt" | wc -l) file."

      - uses: actions/upload-artifact@v3
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

# Link Checker Jobs
  Link_checking_batch_one:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_1.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose

  Link_checking_batch_two:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_2.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose

  Link_checking_batch_three:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_3.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose

  Link_checking_batch_four:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_4.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose

  Link_checking_batch_five:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_5.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose

# Link Checker (Excluded) Jobs
  Link_checking_excluded_batch_one:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_1.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Creating lychee include file
        run: cp -vf .lycheeignore /tmp/lychee_include_file

      - name: Removing .lycheeignore
        run: rm -vf .lycheeignore

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose --include $(cat /tmp/lychee_include_file | tr '\n' ' ')

  Link_checking_excluded_batch_two:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_2.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Creating lychee include file
        run: cp -vf .lycheeignore /tmp/lychee_include_file

      - name: Removing .lycheeignore
        run: rm -vf .lycheeignore

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose --include $(cat /tmp/lychee_include_file | tr '\n' ' ')

  Link_checking_excluded_batch_three:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_3.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Creating lychee include file
        run: cp -vf .lycheeignore /tmp/lychee_include_file

      - name: Removing .lycheeignore
        run: rm -vf .lycheeignore

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose --include $(cat /tmp/lychee_include_file | tr '\n' ' ')

  Link_checking_excluded_batch_four:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_4.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Creating lychee include file
        run: cp -vf .lycheeignore /tmp/lychee_include_file

      - name: Removing .lycheeignore
        run: rm -vf .lycheeignore

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose --include $(cat /tmp/lychee_include_file | tr '\n' ' ')

  Link_checking_excluded_batch_five:
    runs-on: ubuntu-latest
    needs: [Splitting_files_into_batches]
    env:
      MD_ARTIFACTS_DIR: /tmp/md_artifacts
    steps:
      - uses: actions/checkout@v3

      - uses: actions/download-artifact@v4.1.7
        with:
          name: artifacts-markdown
          path: ${{ env.MD_ARTIFACTS_DIR }}

      - name: Creating MD_FILES env variable
        run: echo "MD_FILES=$(cat "${MD_ARTIFACTS_DIR}/MD_FILES_BATCH_5.txt" | tr '\n' ' ')" >> $GITHUB_ENV

      - name: Creating lychee include file
        run: cp -vf .lycheeignore /tmp/lychee_include_file

      - name: Removing .lycheeignore
        run: rm -vf .lycheeignore

      - name: Link Checker
        id: lychee
        # if: ${{ false }}  # disable for now
        uses: lycheeverse/lychee-action@v1.5.4
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          # Fail action on broken links
          fail: true
          # Use json as output format (instead of markdown)
          # format: json
          args: ${{ env.MD_FILES }} --max-concurrency 1 --no-progress --verbose --include $(cat /tmp/lychee_include_file | tr '\n' ' ')