name: release

# Builds standalone PodTui binaries for each supported OS/arch and attaches
# them to a GitHub Release. One runner per platform because Bun cannot
# cross-compile — each runner runs `make dist`, which emits a
# podtui-<platform>-<arch>.tar.gz (binary + native libs side by side).
#
# Trigger: push a tag like v0.1.0. Bump VERSION in src/index.tsx in the same
# commit as the tag so the released binary reports the tagged version.

on: # intentional: YAML `on` key
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build:
    name: build (${{ matrix.os }} / ${{ matrix.arch }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            arch: x64
            plat: linux
          - os: ubuntu-24.04-arm
            arch: arm64
            plat: linux
          - os: macos-15-intel
            arch: x64
            plat: darwin
          - os: macos-14
            arch: arm64
            plat: darwin
    steps:
      - name: Check out repo
        uses: actions/checkout@v5

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install

      - name: Install fftw (cavacore build dependency)
        run: |
          if uname -s | grep -qi darwin; then
            # mpv is required for the release bundle: build.ts copies it into
            # PodTui.app (signed with the podtui bundle identifier) so macOS
            # Now Playing shows the PodTui icon instead of a blank placeholder.
            brew install fftw mpv
          else
            sudo apt-get update
            sudo apt-get install -y libfftw3-dev
          fi

      - name: Build native cavacore library
        run: scripts/build-cavacore.sh

      - name: Build standalone binary + tarball
        run: make dist

      - name: Smoke-test binary boot
        env:
          DIST_TAR: podtui-${{ matrix.plat }}-${{ matrix.arch }}.tar.gz
        run: |
          # The binary is compiled with bunfig autoload disabled
          # (autoloadBunfig: false in build.ts), so it must boot even from a
          # directory holding a bunfig.toml with a top-level preload the
          # standalone can't resolve. Plant one to make this a real regression
          # test for "preload not found".
          SMOKE_DIR=$(mktemp -d)
          tar -xzf "dist/$DIST_TAR" -C "$SMOKE_DIR"
          printf 'preload = ["./definitely-missing.ts"]\n' > "$SMOKE_DIR/bunfig.toml"
          cd "$SMOKE_DIR"
          ./podtui-*/podtui --version
          # macOS tarballs must ship PodTui.app with a working bundled mpv
          # carrying the podtui bundle identifier — otherwise Now Playing
          # attribution silently regresses to a blank icon.
          if [ "${{ matrix.plat }}" = "darwin" ]; then
            MPV=./podtui-*/PodTui.app/Contents/MacOS/mpv
            test -x $MPV || { echo "PodTui.app missing bundled mpv"; exit 1; }
            $MPV --version >/dev/null || { echo "bundled mpv does not launch"; exit 1; }
            codesign -dvv $MPV 2>&1 | grep -q "Identifier=com.mikefreno.podtui" \
              || { echo "bundled mpv lacks podtui signing identifier"; exit 1; }
          fi

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: podtui-${{ matrix.plat }}-${{ matrix.arch }}
          path: dist/podtui-*.tar.gz

  upload:
    name: Attach to GitHub Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download all binaries
        uses: actions/download-artifact@v7
        with:
          path: artifacts

      - name: Publish release
        uses: softprops/action-gh-release@v3
        with:
          generate_release_notes: true
          files: |
            artifacts/**/*.tar.gz
            LICENSE
