name: 🏭 Release Pipeline

on:
  push:
    tags:
      - v*

jobs:
  # Build Everything
  # Linux
  build_linux_qt:
    if: github.repository == 'PCSX2/pcsx2'
    name: "Linux"
    uses: ./.github/workflows/linux_build_qt.yml
    with:
      jobName: "Qt"
      compiler: clang
      cmakeflags: ""
      buildAppImage: true
    secrets: inherit

  # Windows
  build_windows_qt:
    if: github.repository == 'PCSX2/pcsx2'
    name: "Windows"
    uses: ./.github/workflows/windows_build_qt.yml
    with:
      jobName: Qt
      configuration: CMake
      buildSystem: cmake
    secrets: inherit

  build_qt_sse4:
    if: github.repository == 'PCSX2/pcsx2'
    name: "Windows - SSE4"
    uses: ./.github/workflows/windows_build_qt.yml
    with:
      jobName: Qt
      configuration: Release
      simd: "SSE4"
    secrets: inherit

  build_qt_avx2:
    if: github.repository == 'PCSX2/pcsx2'
    name: "Windows - AVX2"
    uses: ./.github/workflows/windows_build_qt.yml
    with:
      jobName: Qt
      configuration: Release AVX2
    secrets: inherit

  # MacOS
  build_macos_qt:
    if: github.repository == 'PCSX2/pcsx2'
    name: "MacOS"
    uses: ./.github/workflows/macos_build.yml
    with:
      jobName: "Qt"
      gui: "Qt"
    secrets: inherit

  # Upload the Artifacts
  upload_artifacts:
    if: github.repository == 'PCSX2/pcsx2'
    needs:
      - build_linux_qt
      - build_windows_qt
      - build_qt_sse4
      - build_qt_avx2
      - build_macos_qt
    name: "Upload Artifacts"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Prepare Artifact Folder
        run: mkdir ./ci-artifacts/

      - uses: actions/download-artifact@v3
        name: Download all Artifacts
        with:
          path: ./ci-artifacts/

      - name: Display structure of downloaded files
        run: ls ./ci-artifacts/

      # Prepare artifacts, they are all zips from github!
      - name: Prepare Artifacts
        working-directory: ./ci-artifacts/
        run: for d in *windows*/; do 7z a "${d}asset.7z" ./$d/*; done

      # Artifact Naming:
      # MacOS: PCSX2-<tag>-macOS-[additional hyphen seperated tags]
      # Windows|Linux: PCSX2-<tag>-<windows|linux>-<32bit|64bit>--[additional hyphen seperated tags]
      - name: Name and Upload the Release Assets
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAG: ${{ github.REF }}
          SCAN_DIR: ${{ github.WORKSPACE }}/ci-artifacts
          OUT_DIR: ${{ github.WORKSPACE }}/ci-artifacts/out
        run: |
          gh release list --repo PCSX2/pcsx2
          mkdir -p ${{ github.WORKSPACE }}/ci-artifacts/out
          python ./.github/workflows/scripts/releases/rename-release-assets.py
          ls ${{ github.WORKSPACE }}/ci-artifacts/out
          TAG_VAL=$(echo ${{ github.REF }} | awk -F'refs/tags/' '{print $2}')
          gh release upload "${TAG_VAL}" ${{ github.WORKSPACE }}/ci-artifacts/out/*  --repo PCSX2/pcsx2 --clobber

      - name: Publish Release
        env:
          GITHUB_TOKEN: ${{ secrets.BOT_PAT }}
        run: |
          TAG_VAL=$(echo ${{ github.REF }} | awk -F'refs/tags/' '{print $2}')
          gh release edit ${TAG_VAL} --draft=false --repo PCSX2/pcsx2

