# This is a basic workflow to help you get started with Actions

name: Publish Documentation

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    #paths:
    #  - 'docs/**.adoc'
    #  - '!docs/release-documentation.adoc'
    branches:
        - main
    tags:
        # normal versions
        - "v[0-9]+.[0-9]+.[0-9]+"
        # pre-releases
        - "v[0-9]+.[0-9]+.[0-9]+-**"

  # 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:
  # This workflow contains a single job called "build"
  doc_build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      
      # TODO: Check if tag is latest and create symlink to release
      - name: Prepare tag
        id: prepare_tag
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          TAG_NAME="${GITHUB_REF##refs/tags/}"
          if [ "$TAG_NAME" = $(git describe --tags --abbrev=0) ]; then
              echo "::set-output name=is_latest::true"
          fi
          echo "::set-output name=tag_name::${TAG_NAME}"
          echo "::set-output name=deploy_tag_name::doc-${TAG_NAME}"
          sed -i "s/___VERSION___/$TAG_NAME/g" doc/index.adoc
      
      # Build HTML Asciidoc
      - name: Build Asciidoc
        id: adocbuild
        uses: avattathil/asciidoctor-action@master
        with:
            program: "asciidoctor -D doc --backend=html5 -o index.html doc/index.adoc"
      
      #- name: CHECK index.html
      #  run: cat doc/index.html
          
      # Runs a single command using the runners shell
      - name: Deploy documentation to GH Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./doc
          exclude_assets: '**.adoc,*/**.adoc,*/**.sh,*/**.gif'
          keep_files: true
          destination_dir: ${{ steps.prepare_tag.outputs.deploy_tag_name }}

      # Change latest link in case there is a new documentation available
      - name : Modify gh-pages branch
        uses: actions/checkout@v2
        with:
          ref: gh-pages

      - name: Update latest symlink
        if: ${{ steps.prepare_tag.outputs.is_latest}}
        run: |
          echo "tag is latest. need to update latest-symlink in gh-pages branch"
          ln -sfn ${{ steps.prepare_tag.outputs.deploy_tag_name }} latest
      
      - name: Commit changes
        uses: EndBug/add-and-commit@v7
        with:
          add: latest
          author_name: Excodibur
          branch: gh-pages
          message: updated symlink latest to ${{ steps.prepare_tag.outputs.deploy_tag_name }}
          tag: ${{ steps.prepare_tag.outputs.deploy_tag_name }}



      
