name: book
on:
  push:
    branches: [master]
    paths:
      - 'book/**'
      - 'book.toml'

  pull_request:
    branches: [master]
    paths:
      - 'book/**'
      - 'book.toml'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Install mdbook
        run: |
          mkdir mdbook
          curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.21/mdbook-v0.4.21-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
          curl -sSL https://github.com/tommilligan/mdbook-admonish/releases/download/v1.8.0/mdbook-admonish-v1.8.0-x86_64-unknown-linux-gnu.tar.gz| tar -xz --directory=./mdbook
          curl -sSL https://github.com/badboy/mdbook-mermaid/releases/download/v0.12.6/mdbook-mermaid-v0.12.6-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
          echo `pwd`/mdbook >> $GITHUB_PATH

      - name: Install mdbook-template
        run: |
          mkdir mdbook-template
          curl -sSL https://github.com/sgoudham/mdbook-template/releases/latest/download/mdbook-template-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook-template
          echo `pwd`/mdbook-template >> $GITHUB_PATH

      - name: Build
        run: |
          mdbook-admonish install 
          mdbook-mermaid install
          mdbook build

      - name: Save pages artifact
        uses: actions/upload-pages-artifact@v1
        with:
          path: target/book

  deploy:
    # Only deploy if a push to master
    if: github.ref_name == 'master' && github.event_name == 'push'
    runs-on: ubuntu-latest
    needs: [build]

    # Grant GITHUB_TOKEN the permissions required to make a Pages deployment
    permissions:
      pages: write
      id-token: write

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

    steps:
      # reference: https://github.com/actions/deploy-pages
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1
