name: Publish release zip

on:
  push:
    tags:
      - 'OFDLV*'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Show .NET version
        run: dotnet --version

      - name: Extract version from tag
        id: version
        run: |
          FULL_REF="${{ gitea.ref }}"
          TAG="${FULL_REF##refs/tags/OFDLV}"
          echo "Version: $TAG"
          echo "version=$TAG" >> "$GITEA_OUTPUT"

      - name: Build for Windows and Linux
        run: |
          dotnet publish -p:Version=${{ steps.version.outputs.version }} \
                         -p:PackageVersion=${{ steps.version.outputs.version }} \
                         -p:WarningLevel=0 -c Release -r win-x86 \
                         --self-contained true -p:PublishSingleFile=true -o outwin

          dotnet publish -p:Version=${{ steps.version.outputs.version }} \
                         -p:PackageVersion=${{ steps.version.outputs.version }} \
                         -p:WarningLevel=0 -c Release -r linux-x64 \
                         --self-contained true -p:PublishSingleFile=true -o outlin

      - name: Copy and patch extra files
        run: |
          cp ./OF\ DL/rules.json outwin/
          chmod +x ./outlin/OF\ DL
          cd outwin

          echo "➤ Running OF DL binary (timeout)"
          timeout --preserve-status --kill-after=5s 30s ../outlin/OF\ DL --non-interactive || true
          echo "➤ Binary finished"

          echo "➤ Creating folder for CDM"
          mkdir -p cdm/devices/chrome_1610

          echo "➤ Copying ffmpeg from user folder"
          cp /home/rhys/ffmpeg/ffmpeg-7.1.1-essentials_build/bin/ffmpeg.exe .
          cp /home/rhys/ffmpeg/ffmpeg-7.1.1-essentials_build/LICENSE LICENSE.ffmpeg

          echo "➤ Creating release zip"
          zip ../OFDLV${{ steps.version.outputs.version }}.zip OF\ DL.exe e_sqlite3.dll rules.json config.conf cdm ffmpeg.exe LICENSE.ffmpeg
          cd ..

      - name: Create Gitea release
        uses: ./.gitea-actions/create-release
        with:
          tag_name: ${{ gitea.ref }}
          release_name: ${{ steps.version.outputs.version }}
          draft: true
          prerelease: false
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}

      - name: Upload release artifact
        uses: gitea/actions/gitea-upload-artifact@v1
        with:
          name: OFDLV${{ steps.version.outputs.version }}.zip
          path: ./OFDLV${{ steps.version.outputs.version }}.zip