name: Documentation

on:
  release:
    types: [published]

  push:
    branches:
      - main
    paths:
      - 'src/**'
      - 'include/**'
      - 'css/**'
      - 'resources/**'
      - 'Makefile'
      - 'Doxyfile'
      - '*.md'
      - '.github/workflows/dox.yml'

  pull_request:
    paths:
      - 'src/**'
      - 'include/**'
      - 'css/**'
      - 'resources/**'
      - 'Makefile'
      - 'Doxyfile'
      - '*.md'
      - '.github/workflows/dox.yml'
jobs:   

  apidocs:
    name: Generate API documentation

    runs-on: ubuntu-latest
    env:
      CC: gcc
    steps:

    - name: install doxygen
      run: sudo apt-get install doxygen
   
    - uses: actions/checkout@v4

    - name: Generate docs
      run: make dox
 
  site-update:
    name: Update github pages
    needs: apidocs
    if: github.repository_owner == 'Smithsonian' && (github.event_name == 'release' || github.event.pull_request.merged || contains(github.event.head_commit.message, 'site update'))
 
    runs-on: ubuntu-latest
    steps:
    
    - name: Checkout source
      uses: actions/checkout@v4
        
    - name: Generate headless README
      run: make README-orig.md
        
    - uses: mattnotmitt/doxygen-action@v1.9.8
      with:
        additional-packages: font-roboto
       
    - name: Checkout gh-pages
      uses: actions/checkout@v4
      with:
        ref: 'gh-pages'
        path: site

    - name: Assert site/doc/
      run: mkdir -p site/doc 

    - name: Copy README
      run: |
        echo '<img src="/SuperNOVAS/resources/CfA-logo.png" alt="CfA logo" width="400" height="67" align="right">' > site/doc/README.md
        echo '<br clear="all">' >> site/doc/README.md
        cat README-orig.md >> site/doc/README.md
        
    - name: Copy CHANGELOG
      run: cp CHANGELOG.md site/doc/
      
    - name: Copy API documentation
      run: cp -a apidoc site/
        
    - name: Assert site/resources/
      run: mkdir -p site/resources 
        
    - name: Copy Figures
      run: cp -a resources/SuperNOVAS-*.png site/resources/
        
    - name: Push to pages
      run: |
        cd site
        git config --global user.email "$GITHUB_JOB+github-actions[bot]@users.noreply.github.com"
        git config --global user.name "github-actions"
        git add -A
        git commit -m "[automated site update]" && git push || true
        
        
  changelog-update:
    name: Update CHANGELOG on github pages
    if: github.repository_owner == 'Smithsonian' && (github.event.pull_request.merged || contains(github.event.head_commit.message, 'changelog update'))
   
    runs-on: ubuntu-latest
    steps:
    
    - name: Checkout source
      uses: actions/checkout@v4
       
    - name: Checkout gh-pages
      uses: actions/checkout@v4
      with:
        ref: 'gh-pages'
        path: site

    - name: Assert site/doc/
      run: mkdir -p site/doc 
        
    - name: Copy CHANGELOG
      run: cp CHANGELOG.md site/doc/
        
    - name: Push to pages
      run: |
        cd site
        git config --global user.email "$GITHUB_JOB+github-actions[bot]@users.noreply.github.com"
        git config --global user.name "github-actions"
        git add -A
        git commit -m "[automated site update]" && git push || true
    
 
        
