name: CEF Runtime

on:
  workflow_dispatch:
    inputs:
      cef_version:
        description: "CEF version to package"
        required: true
        default: "144.0.6+g5f7e671+chromium-144.0.7559.59"
      source:
        description: "Build from official archive or CEF source"
        required: true
        type: choice
        default: "official"
        options:
          - official
          - source
      cef_branch:
        description: "Optional CEF branch for source builds"
        required: false
        default: ""

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.platform }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: macosarm64
            runner: macos-14
          - platform: macosx64
            runner: macos-15
          - platform: linux64
            runner: ubuntu-latest
          - platform: linuxarm64
            runner: ubuntu-24.04-arm
          - platform: windows64
            runner: windows-2022
          # windowsarm64 disabled: Zig 0.16.0 aarch64-windows binary is broken
          # https://codeberg.org/ziglang/zig/issues/31865
          # - platform: windowsarm64
          #   runner: windows-11-arm

    steps:
      - uses: actions/checkout@v4

      - uses: mlugg/setup-zig@v2
        with:
          version: 0.16.0

      - name: Download official CEF
        if: ${{ inputs.source == 'official' }}
        shell: bash
        run: |
          set -euo pipefail
          version="${{ inputs.cef_version }}"
          platform="${{ matrix.platform }}"
          archive="cef_binary_${version}_${platform}.tar.bz2"
          base="https://cef-builds.spotifycdn.com"
          mkdir -p zig-out/cef-download
          curl --fail --location --output "zig-out/cef-download/${archive}" "${base}/${archive}"
          curl --fail --location --output "zig-out/cef-download/${archive}.sha256" "${base}/${archive}.sha256"
          expected="$(tr -d '[:space:]' < "zig-out/cef-download/${archive}.sha256")"
          if command -v sha256sum &>/dev/null; then
            actual="$(sha256sum "zig-out/cef-download/${archive}" | awk '{print $1}')"
          elif command -v shasum &>/dev/null; then
            actual="$(shasum -a 256 "zig-out/cef-download/${archive}" | awk '{print $1}')"
          else
            echo "::error::No SHA-256 hashing utility found"; exit 1
          fi
          test "$expected" = "$actual"
          tar -xjf "zig-out/cef-download/${archive}" -C zig-out/cef-download
          echo "CEF_ROOT=zig-out/cef-download/${archive%.tar.bz2}" >> "$GITHUB_ENV"

      - name: Build CEF wrapper
        if: ${{ inputs.source == 'official' }}
        shell: bash
        run: |
          set -euo pipefail
          cmake_args=(-S "$CEF_ROOT" -B "$CEF_ROOT/build/libcef_dll_wrapper")
          case "${{ matrix.platform }}" in
            *arm64) cmake_args+=(-DPROJECT_ARCH=arm64) ;;
            macosx64) cmake_args+=(-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPROJECT_ARCH=x86_64) ;;
          esac
          cmake "${cmake_args[@]}"
          cmake --build "$CEF_ROOT/build/libcef_dll_wrapper" --target libcef_dll_wrapper --config Release
          mkdir -p "$CEF_ROOT/libcef_dll_wrapper"
          case "${{ matrix.platform }}" in
            windows*) wrapper="libcef_dll_wrapper.lib" ;;
            *) wrapper="libcef_dll_wrapper.a" ;;
          esac
          find "$CEF_ROOT/build/libcef_dll_wrapper" -name "$wrapper" -print -quit | xargs -I{} cp "{}" "$CEF_ROOT/libcef_dll_wrapper/$wrapper"

      - name: Prepare zero-native runtime archive
        if: ${{ inputs.source == 'official' }}
        shell: bash
        run: |
          zig build
          cli="zig-out/bin/zero-native"
          if [[ "${{ runner.os }}" == "Windows" ]]; then cli="zig-out/bin/zero-native.exe"; fi
          "$cli" cef prepare-release --dir "$CEF_ROOT" --output zig-out/cef --version "${{ inputs.cef_version }}"

      - name: Build CEF from source and prepare runtime
        if: ${{ inputs.source == 'source' }}
        shell: bash
        run: |
          zig build
          chmod +x tools/cef/build-from-source.sh
          args=(
            tools/cef/build-from-source.sh
            --platform "${{ matrix.platform }}"
            --version "${{ inputs.cef_version }}"
            --output zig-out/cef
            --zero-native-bin zig-out/bin/zero-native
          )
          if [ -n "${{ inputs.cef_branch }}" ]; then
            args+=(--cef-branch "${{ inputs.cef_branch }}")
          fi
          "${args[@]}"

      - name: Publish release asset
        uses: softprops/action-gh-release@v2
        with:
          tag_name: cef-${{ inputs.cef_version }}
          name: CEF ${{ inputs.cef_version }}
          files: |
            zig-out/cef/zero-native-cef-${{ inputs.cef_version }}-${{ matrix.platform }}.tar.gz
            zig-out/cef/zero-native-cef-${{ inputs.cef_version }}-${{ matrix.platform }}.tar.gz.sha256
