name: build

on:
  push:
    branches:
      - main
      - master
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - goos: linux
            goarch: amd64
            binary_name: api
          - goos: linux
            goarch: arm64
            binary_name: api
          - goos: windows
            goarch: amd64
            binary_name: api.exe

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Verify build
        run: |
          go build ./cmd/api
          go build ./...

      - name: Prepare directories
        run: |
          mkdir -p dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/frontend
          mkdir -p dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/docs

      - name: Build binary
        env:
          CGO_ENABLED: 0
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          go build -trimpath -ldflags="-s -w" -o dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/${{ matrix.binary_name }} ./cmd/api

      - name: Copy runtime files
        run: |
          cp README.md dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/README.md
          cp .env.example dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/.env.example
          cp -r frontend/. dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/frontend/
          cp -r docs/. dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/docs/
          cp scripts/install.sh dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/install.sh
          chmod +x dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}/install.sh

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}
          path: dist/singbox-gopanel-${{ matrix.goos }}-${{ matrix.goarch }}
