name: Test and build
on:
  push:

permissions:
  contents: read

jobs:
  test:
    name: Test and lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version-file: 'go.mod'
          check-latest: false
      - name: Test
        run:  go test -v ./...
      - name: Lint
        uses: golangci/golangci-lint-action@v9
        with:
          go-version: ${{ matrix.go }}
          version: v2.6
        if: ${{ github.actor != 'maze' }}

  build_collector:
    name: Build collector
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version-file: 'go.mod'
          check-latest: false
      - name: Download modules
        run:  go mod download
      - name: Make build directory
        run:  mkdir -p build
      - name: Build
        run:  |
          go build -o build/hamview-collector ./cmd/hamview-collector && \
          ls -al $(readlink -f build/hamview-collector)
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log in to Docker
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKER_USER }}
          password: ${{ secrets.DOCKER_TOKEN }}
      - name: Log in to Gitea
        uses: docker/login-action@v3
        with:
          registry: ${{ vars.REGISTRY }}
          username: ${{ vars.DEPLOY_USER }}
          password: ${{ secrets.DEPLOY_TOKEN }}
      - name: Build and push collector container
        uses: docker/build-push-action@v6
        with:
          push: true
          tags: ${{ vars.REGISTRY }}/ham/hamview/collector:dev
          file: ./cmd/hamview-collector/Dockerfile
          context: .
          platforms: |
            linux/amd64

  build_receiver:
    name: Build receiver
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version-file: 'go.mod'
          check-latest: false
      - name: Download modules
        run:  go mod download
      - name: Make build directory
        run:  mkdir -p build && readlink -f build
      - name: Build amd64
        env:
          GOOS:   linux
          GOARCH: amd64
        run:  |
          GOOS=$GOOS GOARCH=$GOARCH \
            go build -o build/hamview-receiver-$GOARCH ./cmd/hamview-receiver && \
            readlink -f build/hamview-receiver-$GOARCH
      - name: Build arm6
        env:
          GOOS:   linux
          GOARCH: arm
          GOARM:  6
        run:  |
          GOOS=$GOOS GOARCH=$GOARCH GOARM=$GOARM \
            go build -o build/hamview-receiver-$GOARCH$GOARM ./cmd/hamview-receiver && \
            readlink -f build/hamview-receiver-$GOARCH$GOARM
      - name: Build arm7
        env:
          GOOS:   linux
          GOARCH: arm
          GOARM:  7
        run:  |
          GOOS=$GOOS GOARCH=$GOARCH GOARM=$GOARM \
            go build -o build/hamview-receiver-$GOARCH$GOARM ./cmd/hamview-receiver && \
            readlink -f build/hamview-receiver-$GOARCH$GOARM
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log in to Docker
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKER_USER }}
          password: ${{ secrets.DOCKER_TOKEN }}
      - name: Log in to Gitea
        uses: docker/login-action@v3
        with:
          registry: ${{ vars.REGISTRY }}
          username: ${{ vars.DEPLOY_USER }}
          password: ${{ secrets.DEPLOY_TOKEN }}
      - name: Build and push receiver container
        uses: docker/build-push-action@v6
        with:
          push: true
          tags: ${{ vars.REGISTRY }}/ham/hamview/receiver:dev
          file: ./cmd/hamview-receiver/Dockerfile
          context: .
          platforms: |
            linux/amd64
            linux/arm/v6
            linux/arm/v7
