name: Test Build and Run

on:
#  push:
#    branches:
#      - '*'
#  pull_request:
#  merge_group:
  workflow_dispatch:

jobs:
  build-test:
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        arch: [amd64, arm64]

    runs-on: ${{ matrix.os }}
    continue-on-error: true
    env:
      GOARCH: ${{ matrix.arch }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'

      - name: Build Go binary
        run: go build -ldflags "-s -w" -trimpath -o ./go2rtc

      - name: Test Go binary on linux
        if: matrix.os == 'ubuntu-latest'
        run: |
            if [ "${{ matrix.arch }}" = "amd64" ]; then
              ./go2rtc -version
            else
              sudo apt-get update && sudo apt-get install -y qemu-user-static
              sudo cp /usr/bin/qemu-aarch64-static .
              sudo chown $USER:$USER ./qemu-aarch64-static
              qemu-aarch64-static ./go2rtc -version
            fi
      - name: Test Go binary on macos
        if: matrix.os == 'macos-latest'
        run: |
            if [ "${{ matrix.arch }}" = "amd64" ]; then
              ./go2rtc -version
            else
              echo "ARM64 architecture is not yet supported on macOS"
            fi
      - name: Test Go binary on windows
        if: matrix.os == 'windows-latest'
        run: |
            if ("${{ matrix.arch }}" -eq "amd64") {
                .\go2rtc* -version
            } else {
                Write-Host "ARM64 architecture is not yet supported on Windows"
            }
  docker-test:
    strategy:
      matrix:
        platform:
          - amd64
          - "386"
          - arm/v7
          - arm64/v8
    continue-on-error: true
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/Dockerfile
          platforms: linux/${{ matrix.platform }}
          push: false
          load: true
          tags: go2rtc-${{ matrix.platform }}
      - name: test run
        run: |
          docker run --platform=linux/${{ matrix.platform }} --rm go2rtc-${{ matrix.platform }} go2rtc -version

      - name: Build and push Hardware
        if: matrix.platform == 'amd64'
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/hardware.Dockerfile
          platforms: linux/amd64
          push: false
          load: true
          tags: go2rtc-${{ matrix.platform }}-hardware
      - name: test run
        if: matrix.platform == 'amd64'
        run: |
          docker run --platform=linux/${{ matrix.platform }} --rm go2rtc-${{ matrix.platform }}-hardware go2rtc -version