name: Documentation

on:
  pull_request:
  push:
    paths:
      - ".github/workflows/docs.yml"
      - "docs/book/**"
      - "**/src/*"
      - "libs/**/README.md"
      - "book.toml"

jobs:
  build:
    name: Build Docs
    runs-on: ubuntu-latest

    permissions:
      pages: write
      id-token: write

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

    steps:
      - uses: actions/checkout@v3

      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v1
        with:
          mdbook-version: "latest"

      - name: Build Book
        run: mdbook build

      - name: Build rustdoc
        uses: actions-rs/cargo@v1
        with:
          command: doc
          args: --no-deps --workspace --document-private-items

      - name: Combine Book and rustdoc
        run: mkdir -p /tmp/combined && cp -rv target/book/ /tmp/combined/book && cp -rv target/doc/ /tmp/combined/rustdoc-master

      - name: Write CNAME file
        run: echo "docs.protomask.ewpratten.com" > /tmp/combined/CNAME

      - name: Fix permissions
        run: |
          chmod -c -R +rX "/tmp/combined/" | while read line; do
            echo "::warning title=Invalid file permissions automatically fixed::$line"
          done

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v2
        with:
          path: /tmp/combined

      - name: Deploy
        uses: actions/deploy-pages@v2
        id: deployment
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/ewpratten/reorg2' }}