name: deploy-docs
on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    container:
      image: hugomods/hugo:latest
      # Mount deployment target and the framework directly from the host
      options: --user root -v /srv/www:/srv/www -v /srv/dev/hugo/wiki:/srv/dev/hugo/wiki:ro -v /srv/configs:/srv/configs:ro
      
    steps:
      - name: Checkout Docs Source
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Prepare, Build, and Deploy
        run: |
          # install asciidoctor
          apk add --no-cache asciidoctor

          # Save the path to the checked-out docs
          DOCS_DIR=$(pwd)
          
          # Copy the read-only framework to a writable build directory
          cp -r /srv/dev/hugo/wiki /tmp/hugo-build
          cd /tmp/hugo-build
          
          # Remove the symlinks that were copied from the host
          rm -rf content static
          
          # Create real folders for the build
          mkdir -p content static config/_default
          
          # Copy the new content from the docs repository
          cp -r $DOCS_DIR/* content/
          
          # Move static assets if they exist in the docs
          if [ -d "content/static" ]; then
            cp -r content/static/* static/
            rm -rf content/static
          fi
          
          # Move dynamic config overrides (e.g. menus.en.toml, params.toml) if they exist
          if [ -d "content/config/_default" ]; then
            cp -r content/config/_default/* config/_default/
            rm -rf content/config
          fi
          
          # Clean git metadata
          rm -rf content/.git content/.gitea
          
          # Build the site using dynamic environment variable overrides
          HUGO_PARAMS_ARTICLE_EDITURL="https://git.wompmacho.com/wompmacho/docs-public/src/branch/main" hugo --minify --destination public
          
          # Deploy to Nginx Volume
          mkdir -p /srv/www/docs-public
          rm -rf /srv/www/docs-public/*
          cp -r public/* /srv/www/docs-public/
