name: Release

on:
  push:
    tags: ["v*"]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target.triple }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - triple: x86_64-unknown-linux-musl
          - triple: aarch64-unknown-linux-musl

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target.triple }}

      - name: Install musl tools
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - name: Install aarch64 musl toolchain
        if: matrix.target.triple == 'aarch64-unknown-linux-musl'
        run: |
          curl -sSL https://github.com/cross-tools/musl-cross/releases/latest/download/aarch64-unknown-linux-musl.tar.xz | tar -xJ -C /tmp
          echo "/tmp/aarch64-unknown-linux-musl/bin" >> $GITHUB_PATH

      - name: Set cross-compile env
        if: matrix.target.triple == 'aarch64-unknown-linux-musl'
        run: |
          echo "CC_aarch64_unknown_linux_musl=aarch64-unknown-linux-musl-gcc" >> $GITHUB_ENV
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-unknown-linux-musl-gcc" >> $GITHUB_ENV

      - name: Install protoc
        run: sudo apt-get update && sudo apt-get install -y protobuf-compiler

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 11

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: pnpm
          cache-dependency-path: frontend/pnpm-lock.yaml

      - name: Install frontend dependencies
        run: cd frontend && pnpm install --frozen-lockfile

      - name: Build frontend
        run: cd frontend && pnpm build

      - uses: Swatinem/rust-cache@v2

      - name: Build release
        run: cargo build --release --target ${{ matrix.target.triple }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: peerman-${{ matrix.target.triple }}
          path: target/${{ matrix.target.triple }}/release/peerman

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Generate checksums and upload
        run: |
          cd artifacts
          for dir in peerman-*/; do
            triple="${dir%/}"
            triple="${triple#peerman-}"
            src="${dir}peerman"
            if [ -f "$src" ]; then
              mv "$src" "peerman-${triple}.tmp"
              rm -rf "$dir"
              mv "peerman-${triple}.tmp" "peerman-${triple}"
            fi
          done
          sha256sum peerman-* > SHA256SUMS
          cat SHA256SUMS

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          generate_release_notes: true
