name: Build Application and Make Release

on:
  push:
    tags:
      - "*"

permissions: {}

jobs:
  build_release:
    name: Build Release
    uses: ./.github/workflows/build.yml
    permissions:
      contents: read
      # Required for Azure Trusted Signing
      id-token: write
      # Required for vcpkg binary cache
      packages: write
    with:
      build-type: Release
      environment: Release
    secrets: inherit

  create_release:
    needs: build_release
    permissions:
      contents: write
    runs-on: ubuntu-slim
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: "true"
          path: "PineconeMC-source"
      - name: Download artifacts
        uses: actions/download-artifact@v8
      - name: Grab and store version
        run: |
          tag_name=$(echo ${{ github.ref }} | grep -oE "[^/]+$")
          echo "VERSION=$tag_name" >> $GITHUB_ENV
      - name: Package artifacts properly
        run: |
          mv ${{ github.workspace }}/PineconeMC-source PineconeMC-${{ env.VERSION }}
          mv PineconeMC-Linux-Qt6-Portable*/PineconeMC-portable.tar.gz PineconeMC-Linux-Qt6-Portable-${{ env.VERSION }}.tar.gz
          mv PineconeMC-Linux-aarch64-Qt6-Portable*/PineconeMC-portable.tar.gz PineconeMC-Linux-aarch64-Qt6-Portable-${{ env.VERSION }}.tar.gz
          mv PineconeMC-*.AppImage/PineconeMC-*-x86_64.AppImage PineconeMC-Linux-x86_64.AppImage
          mv PineconeMC-*.AppImage.zsync/PineconeMC-*-x86_64.AppImage.zsync PineconeMC-Linux-x86_64.AppImage.zsync
          mv PineconeMC-*.AppImage/PineconeMC-*-aarch64.AppImage PineconeMC-Linux-aarch64.AppImage
          mv PineconeMC-*.AppImage.zsync/PineconeMC-*-aarch64.AppImage.zsync PineconeMC-Linux-aarch64.AppImage.zsync
          mv PineconeMC-macOS*/PineconeMC.zip PineconeMC-macOS-${{ env.VERSION }}.zip
          mv PineconeMC-macOS*/PineconeMC.dmg PineconeMC-macOS-${{ env.VERSION }}.dmg

          tar --exclude='.git' -czf PineconeMC-${{ env.VERSION }}.tar.gz PineconeMC-${{ env.VERSION }}

          for d in PineconeMC-Windows-MSVC*; do
            cd "${d}" || continue
            LEGACY="$(echo -n ${d} | grep -o Legacy || true)"
            ARM64="$(echo -n ${d} | grep -o arm64 || true)"
            INST="$(echo -n ${d} | grep -o Setup || true)"
            PORT="$(echo -n ${d} | grep -o Portable || true)"
            NAME="PineconeMC-Windows-MSVC"
            test -z "${LEGACY}" || NAME="${NAME}-Legacy"
            test -z "${ARM64}" || NAME="${NAME}-arm64"
            test -z "${PORT}" || NAME="${NAME}-Portable"
            test -z "${INST}" || mv PineconeMC-*.exe ../${NAME}-Setup-${{ env.VERSION }}.exe
            test -n "${INST}" || zip -r -9 "../${NAME}-${{ env.VERSION }}.zip" *
            cd ..
          done

          for d in PineconeMC-Windows-MinGW-w64*; do
            cd "${d}" || continue
            INST="$(echo -n ${d} | grep -o Setup || true)"
            PORT="$(echo -n ${d} | grep -o Portable || true)"
            NAME="PineconeMC-Windows-MinGW-w64"
            test -z "${PORT}" || NAME="${NAME}-Portable"
            test -z "${INST}" || mv PineconeMC-*.exe ../${NAME}-Setup-${{ env.VERSION }}.exe
            test -n "${INST}" || zip -r -9 "../${NAME}-${{ env.VERSION }}.zip" *
            cd ..
          done

          for d in PineconeMC-Windows-MinGW-arm64*; do
            cd "${d}" || continue
            INST="$(echo -n ${d} | grep -o Setup || true)"
            PORT="$(echo -n ${d} | grep -o Portable || true)"
            NAME="PineconeMC-Windows-MinGW-arm64"
            test -z "${PORT}" || NAME="${NAME}-Portable"
            test -z "${INST}" || mv PineconeMC-*.exe ../${NAME}-Setup-${{ env.VERSION }}.exe
            test -n "${INST}" || zip -r -9 "../${NAME}-${{ env.VERSION }}.zip" *
            cd ..
          done

      - name: Create release
        id: create_release
        uses: softprops/action-gh-release@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag_name: ${{ github.ref }}
          name: PineconeMC ${{ env.VERSION }}
          draft: true
          prerelease: false
          fail_on_unmatched_files: true
          files: |
            PineconeMC-Linux-x86_64.AppImage
            PineconeMC-Linux-x86_64.AppImage.zsync
            PineconeMC-Linux-aarch64.AppImage
            PineconeMC-Linux-aarch64.AppImage.zsync
            PineconeMC-Linux-Qt6-Portable-${{ env.VERSION }}.tar.gz
            PineconeMC-Linux-aarch64-Qt6-Portable-${{ env.VERSION }}.tar.gz
            PineconeMC-Windows-MinGW-w64-${{ env.VERSION }}.zip
            PineconeMC-Windows-MinGW-w64-Portable-${{ env.VERSION }}.zip
            PineconeMC-Windows-MinGW-w64-Setup-${{ env.VERSION }}.exe
            PineconeMC-Windows-MinGW-arm64-${{ env.VERSION }}.zip
            PineconeMC-Windows-MinGW-arm64-Portable-${{ env.VERSION }}.zip
            PineconeMC-Windows-MinGW-arm64-Setup-${{ env.VERSION }}.exe
            PineconeMC-Windows-MSVC-arm64-${{ env.VERSION }}.zip
            PineconeMC-Windows-MSVC-arm64-Portable-${{ env.VERSION }}.zip
            PineconeMC-Windows-MSVC-arm64-Setup-${{ env.VERSION }}.exe
            PineconeMC-Windows-MSVC-${{ env.VERSION }}.zip
            PineconeMC-Windows-MSVC-Portable-${{ env.VERSION }}.zip
            PineconeMC-Windows-MSVC-Setup-${{ env.VERSION }}.exe
            PineconeMC-macOS-${{ env.VERSION }}.zip
            PineconeMC-macOS-${{ env.VERSION }}.dmg
            PineconeMC-${{ env.VERSION }}.tar.gz
