name: Deploy website

on:
  workflow_dispatch:
    inputs:
      source_branch:
        description: 'Branch to deploy the website from (last release/* branch for current version)'
        required: true
        default: 'release/0.80.x'
        type: string

      destination_prefix:
        description: 'Deploy to this path prefix (leave empty for the current version)'
        type: string

jobs:
  build_and_deploy_website:
    runs-on: ubuntu-22.04
    steps:
      - name: Check out MkDocs sources
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.source_branch }}
          submodules: false
          path: dosbox-staging

      - name: Check out organisation GitHub Pages repo
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.ACCESS_TOKEN_REPO }}
          repository: dosbox-staging/dosbox-staging.github.io
          ref: master
          submodules: false
          path: dosbox-staging.github.io

      - name: Set up variables
        run: |
          if [[ -z "${{ inputs.destination_prefix }}" ]]; then
            echo "dest_path_prefix=." >> "$GITHUB_ENV"
          else
            echo "dest_path_prefix=preview/${{ inputs.destination_prefix }}" >> "$GITHUB_ENV"
          fi

      - name: Install mkdocs
        run: |
          cd dosbox-staging
          pip install -r contrib/documentation/mkdocs-package-requirements.txt

      - name: Get current documentation Git hash
        run: |
          cd dosbox-staging
          REV=$(git rev-parse --short HEAD)
          echo "source_git_hash=$REV" >> "$GITHUB_ENV"

      - name: Delete existing documentation
        run: |
          cd dosbox-staging.github.io
          if [[ -d ${{ env.dest_path_prefix }} ]]; then
              cd ${{ env.dest_path_prefix }}
              FILES=$(find . \
                  -not -path '*/\.*'        `# ignore hidden files` \
                  -not -path "."            `# ignore current dir` \
                  -not -path "./static"     `# ignore manually managed 'static' dir` \
                  -not -path "./static/*"   `# ignore manually managed 'static' dir contents` \
                  -not -path "./preview"    `# ignore 'preview' dir` \
                  -not -path "./preview/*"  `# ignore 'preview' dir contents` \
                  -not -name "README.md"    `# ignore 'README.md'` \
                  -not -name "CNAME"        `# ignore CNAME`
              )
              echo $FILES | xargs git rm -rf --
          fi

      - name: Build documentation
        run: |
          cd dosbox-staging.github.io
          DEST_PATH=$(pwd)/${{ env.dest_path_prefix }}
          MKDOCS_SRC_PATH=../dosbox-staging/website/
          mkdocs build --config-file $MKDOCS_SRC_PATH/mkdocs.yml --site-dir $DEST_PATH --dirty
          cp -R $MKDOCS_SRC_PATH/docs/.well-known $DEST_PATH
          touch $DEST_PATH/.nojekyll

      - name: Setup Git config
        run: |
          cd dosbox-staging.github.io
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"

      - name: Commit & publish new release
        run: |
          cd dosbox-staging.github.io
          git add .
          git add .well-known/*
          git add .nojekyll
          git commit -m "Deployed current website (${{ env.source_git_hash }})"
          git push

