# spell-checker:ignore dtolnay libsystemd libattr libcap gsub

name: Check uudoc Documentation Generation

on:
  pull_request:
    paths:
      - 'src/bin/uudoc.rs'
      - 'src/uu/*/locales/en-US.ftl'
      - 'Cargo.toml'
      - '.github/workflows/documentation.yml'
  push:
    branches:
      - main
    paths:
      - 'src/bin/uudoc.rs'
      - 'src/uu/*/locales/en-US.ftl'
      - 'Cargo.toml'

jobs:
  check-doc:
    name: Verify uudoc generates correct documentation
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install/setup prerequisites
        shell: bash
        run: sudo apt-get -y update ; sudo apt-get -y install libselinux1-dev libsystemd-dev libacl1-dev libattr1-dev libcap-dev

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Download tldr
        run: curl -L https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip -o docs/tldr.zip

      - name: Generate documentation
        run: cargo run --bin uudoc --all-features

      - name: Get current version from Cargo.toml
        id: version
        run: |
          VERSION=$(awk '/\[workspace\.package\]/{flag=1; next} flag && /^version = /{gsub(/.*= "/, ""); gsub(/".*/, ""); print; exit}' Cargo.toml)
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Detected version: $VERSION"

      - name: Check for --repeated option in uniq.md
        run: |
          if [ ! -f "docs/src/utils/uniq.md" ]; then
            echo "docs/src/utils/uniq.md does not exist"
            exit 1
          fi

          if ! grep -q -- "--repeated" docs/src/utils/uniq.md; then
            echo "'--repeated' option not found in docs/src/utils/uniq.md"
            echo "Content of uniq.md:"
            head -50 docs/src/utils/uniq.md
            exit 1
          fi

      - name: Check for correct version in ls.md
        run: |
          VERSION="${{ steps.version.outputs.version }}"

          if [ ! -f "docs/src/utils/ls.md" ]; then
            echo "docs/src/utils/ls.md does not exist"
            exit 1
          fi

          if ! grep -q "v(uutils coreutils) $VERSION" docs/src/utils/ls.md; then
            echo "Version '$VERSION' not found in docs/src/utils/ls.md"
            echo "Found version info:"
            grep "v(uutils coreutils)" docs/src/utils/ls.md || echo "No version info found"
            echo "Full version section:"
            grep -A2 -B2 "version" docs/src/utils/ls.md || echo "No version section found"
            exit 1
          fi

      - name: Verify usage information is present
        run: |
          if [ ! -f "docs/src/utils/cat.md" ]; then
            echo "docs/src/utils/cat.md does not exist"
            exit 1
          fi

          if ! grep -q "cat \[OPTION\].*\[FILE\]" docs/src/utils/cat.md; then
            echo "Usage information missing from cat.md"
            echo "Content around usage:"
            grep -A5 -B5 "cat" docs/src/utils/cat.md | head -20
            exit 1
          fi

      - name: Verify help text is properly resolved
        run: |
          if [ ! -f "docs/src/utils/cat.md" ]; then
            echo "docs/src/utils/cat.md does not exist"
            exit 1
          fi

          if grep -q "cat-help-" docs/src/utils/cat.md; then
            echo "Found unresolved Fluent keys in cat.md - help text not properly translated"
            echo "Unresolved Fluent keys found:"
            grep "cat-help-" docs/src/utils/cat.md
            exit 1
          fi

      - name: Verify about text is present
        run: |
          if [ ! -f "docs/src/utils/cat.md" ]; then
            echo "docs/src/utils/cat.md does not exist"
            exit 1
          fi

          if ! grep -q "Concatenate FILE(s)" docs/src/utils/cat.md; then
            echo "About text missing from cat.md"
            echo "Content of cat.md:"
            head -30 docs/src/utils/cat.md
            exit 1
          fi

      - name: Verify tldr examples integration
        run: |
          if [ ! -f "docs/src/utils/cp.md" ]; then
            echo "docs/src/utils/cp.md does not exist"
            exit 1
          fi

          if ! grep -q "The examples are provided by" docs/src/utils/cp.md; then
            echo "tldr examples integration missing from cp.md"
            echo "Expected to find 'The examples are provided by' text"
            echo "Content of cp.md:"
            tail -20 docs/src/utils/cp.md
            exit 1
          fi
