name: "Docs: Copy"

on:
  workflow_dispatch:
  push:
    branches:
      - master
    tags-ignore:
      - '**'
    paths:
      - 'library/**/Chart.yaml'
      - '.tools/gen-helm-docs.sh'
      - '.tools/templates/docs/**'
      - '.tools/templates/app/*.gotmpl'
      - '**.gotmpl'
      - '**/LICENSE'
      - '**LICENSE'
      - '**.md'
      - '.github/README.md'
      - '.github/CODE_OF_CONDUCT'
      - '.github/CONTRIBUTING'
      - './LICENSE'
      - './NOTICE'
      - '.github/workflows/docs.copy.yaml'
      - 'index.yaml'

jobs:
  copy:
    runs-on: ubuntu-latest
    name: "Copy"
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.BOT_TOKEN }}

      - name: install helm-docs
        run: |
          brew install norwoodj/tap/helm-docs

      - name: (re)generate docs
        run: |
          .tools/gen-helm-docs.sh

      - name: Copy general readme to website
        run: |
          yes | cp -rf index.yaml docs/index.yaml || echo "chart-index copy failed, continuing..."
          yes | cp -rf .github/README.md docs/index.md || echo "readme copy failed, continuing..."
          sed -i '1s/^/---\nhide:\n  - navigation\n  - toc\n---\n/' docs/index.md
          sed -i 's~<!-- INSERT-DISCORD-WIDGET -->~<iframe src="https://discord.com/widget?id=830763548678291466\&theme=dark" width="350" height="500" allowtransparency="true" frameborder="0" sandbox="allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts"></iframe>~g' docs/index.md
          yes | cp -rf .github/CODE_OF_CONDUCT docs/about/code_of_conduct.md || echo "CODE_OF_CONDUCT copy failed, continuing..."
          yes | cp -rf .github/CONTRIBUTING docs/development/contributing.md || echo "CONTRIBUTING copy failed, continuing..."
          yes | cp -rf .github/SUPPORT.md docs/manual/SUPPORT.md || echo "support policy copy failed, continuing..."
          yes | cp -rf LICENSE docs/about/legal/LICENSE.md || echo "license copy failed, continuing..."
          sed -i '1s/^/# License<br>\n\n/' docs/about/legal/LICENSE.md
          yes | cp -rf NOTICE docs/about/legal/NOTICE.md || echo "license copy failed, continuing..."
          sed -i '1s/^/# NOTICE<br>\n\n/' docs/about/legal/NOTICE.md
          ls docs/

      - name: Copy Apps readme to website
        run: |
          for train in stable incubator develop non-free deprecated; do
            for chart in ${train}/*; do
              if [ -d "${chart}" ]; then
                  chartname=$(basename ${chart})
                  echo "Processing: ${chart}"
                  mkdir -p docs/apps/${train}/${chartname} || echo "app path already exists, continuing..."
                  yes | cp -rf ${chart}/README.md docs/apps/${train}/${chartname}/index.md || echo "readme copy failed, continuing..."
                  yes | cp -rf ${chart}/CONFIG.md docs/apps/${train}/${chartname}/CONFIG.md || echo "config copy failed, continuing..."
                  yes | cp -rf ${chart}/LICENSE docs/apps/${train}/${chartname}/LICENSE.md || echo "license copy failed, continuing..."
                  sed -i '1s/^/# License<br>\n\n/' docs/apps/${train}/${chartname}/LICENSE.md || echo "license edit failed, continuing..."
              fi
            done
          echo "Processed charts for ${train}:"
          ls docs/apps/${train}/ || echo "no Apps in train"
          done

      - name: Commit and Push updated docs
        run: |
          git config user.name "TrueCharts-Bot"
          git config user.email "bot@truecharts.org"
          git add --all
          git commit -sm "Commit docs updates" || exit 0
          git push
