name: Windows portable

on:
  push:
    tags:
      - v*.*.*

env:
  FFMPEG_ARCHIVE_URL: https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-lgpl-shared.zip
  FFMPEG_ARCHIVE_NAME: ffmpeg-master-latest-win64-lgpl-shared.zip
  PYTHON_ARCHIVE_URL: https://www.python.org/ftp/python/3.11.1/python-3.11.1-embed-amd64.zip

jobs:

  build:
    name: Build Windows portable distributions
    runs-on: windows-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.5

      - name: Setup Python
        uses: actions/setup-python@v3
        with:
          python-version: "3.11.1"

      - name: Download ffmpeg archive
        run: Invoke-WebRequest -Uri $($env:FFMPEG_ARCHIVE_URL) -OutFile ffmpeg.zip

      - name: Download python archive
        run: Invoke-WebRequest -Uri $($env:PYTHON_ARCHIVE_URL) -OutFile python.zip

      - name: Create build directory and dist directory
        run: New-Item -Path @("build", "dist") -ItemType Directory

      - name: Unzip ffmpeg archive
        run: Expand-Archive -LiteralPath "ffmpeg.zip" -DestinationPath "build"

      - name: Unzip Python archive
        run: Expand-Archive -LiteralPath "python.zip" -DestinationPath "build\python"

      - name: Rename ffmpeg directory
        working-directory: build
        run: Rename-Item -Path $($env:FFMPEG_ARCHIVE_NAME).Substring(0, $($env:FFMPEG_ARCHIVE_NAME).Length - 4) "ffmpeg"

      - name: Sliming ffmpeg
        working-directory: build
        run: |
          Get-ChildItem -Path "ffmpeg" -Exclude @("LICENSE.txt", "bin") | Remove-Item -Recurse
          ls ffmpeg

      - name: Create venv
        working-directory: build
        run: python -m venv venv

      - name: Install packages
        working-directory: build
        run: |
          ls ${{ github.workspace }}
          .\venv\Scripts\activate
          pip install ${{ github.workspace }}
          ls venv\Lib\site-packages

      - name: Copy site-packages
        shell: cmd
        working-directory: build
        run: (robocopy venv\Lib\site-packages python\Lib\site-packages /mir /xd __pycache__* pip* setuptools*) ^& IF %ERRORLEVEL% LSS 8 SET ERRORLEVEL = 0
        # https://ss64.com/nt/robocopy-exit.html
        # https://superuser.com/questions/280425/getting-robocopy-to-return-a-proper-exit-code
        # https://social.msdn.microsoft.com/Forums/en-US/d599833c-dcea-46f5-85e9-b1f028a0fefe/robocopy-exits-with-error-code-1

      - name: Add search path
        working-directory: build
        run: Add-Content -Path "python\python310._pth" "Lib\site-packages"

      - name: Copy run.bat
        working-directory: build
        run: Copy-Item "${{ github.workspace }}\run.bat" -Destination ".\run.bat"

      - name: Copy run.ps1
        working-directory: build
        run: Copy-Item "${{ github.workspace }}\run.ps1" -Destination ".\run.ps1"

      - name: Zip files
        run: |
          ls build
          Compress-Archive -Path @("build\run.bat", "build\run.ps1", "build\python", "build\ffmpeg") -DestinationPath "dist\blrec-${{ github.ref_name }}-win64.zip"
          ls dist

      - name: Upload distributions to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: dist\*
          tag: ${{ github.ref }}
          overwrite: true
          file_glob: true