name: Release

on:
  push:
    tags:
      - "v*"

env:
  CARGO_TERM_COLOR: always
  ZIG_VERSION: "0.14.1"
  ZIG_SHA256: "24aeeec8af16c381934a6cd7d95c807a8cb2cf7df9fa40d359aa884195c4716c"

permissions:
  contents: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run tests
        run: cargo test --locked --verbose

  build:
    needs: test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            binary: sdme-x86_64-linux
          - target: aarch64-unknown-linux-musl
            binary: sdme-aarch64-linux

    steps:
      - uses: actions/checkout@v4

      - name: Install Zig
        run: |
          curl -sL -o zig.tar.xz "https://ziglang.org/download/${ZIG_VERSION}/zig-x86_64-linux-${ZIG_VERSION}.tar.xz"
          echo "${ZIG_SHA256}  zig.tar.xz" | sha256sum -c -
          mkdir -p "$HOME/.zig"
          tar -xJf zig.tar.xz -C "$HOME/.zig" --strip-components=1
          echo "$HOME/.zig" >> "$GITHUB_PATH"
          rm zig.tar.xz

      - name: Install cargo-zigbuild
        run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/rust-cross/cargo-zigbuild/releases/download/v0.22.1/cargo-zigbuild-installer.sh | sh

      - name: Add rustup target
        run: rustup target add ${{ matrix.target }}

      - name: Build
        run: cargo zigbuild --locked --release --target ${{ matrix.target }}

      - name: Rename binary
        run: cp target/${{ matrix.target }}/release/sdme ${{ matrix.binary }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.binary }}
          path: ${{ matrix.binary }}

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

  package:
    needs: build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
          - target: aarch64-unknown-linux-musl

    steps:
      - uses: actions/checkout@v4

      - name: Install packaging tools
        run: cargo install cargo-deb cargo-generate-rpm

      - name: Download target binary
        uses: actions/download-artifact@v4
        with:
          name: target-${{ matrix.target }}
          path: target/${{ matrix.target }}/release

      - name: Download x86_64 binary for completions
        uses: actions/download-artifact@v4
        with:
          name: target-x86_64-unknown-linux-musl
          path: /tmp/sdme-bin

      - name: Generate completions and AppArmor profile
        run: |
          chmod +x /tmp/sdme-bin/sdme
          mkdir -p dist/completions dist/apparmor
          /tmp/sdme-bin/sdme config completions bash > dist/completions/sdme.bash
          /tmp/sdme-bin/sdme config completions zsh > dist/completions/_sdme
          /tmp/sdme-bin/sdme config completions fish > dist/completions/sdme.fish
          /tmp/sdme-bin/sdme config apparmor-profile > dist/apparmor/sdme-default

      - name: Ensure binaries are executable
        run: chmod +x target/${{ matrix.target }}/release/sdme

      - name: Build deb
        run: cargo deb --no-build --no-strip --target ${{ matrix.target }}

      - name: Build rpm
        run: cargo generate-rpm --target ${{ matrix.target }}

      - name: Collect packages
        run: |
          mkdir -p packages
          cp target/debian/*.deb packages/
          cp target/${{ matrix.target }}/generate-rpm/*.rpm packages/
          ls -lh packages/

      - name: Upload packages
        uses: actions/upload-artifact@v4
        with:
          name: packages-${{ matrix.target }}
          path: packages/*

  release:
    needs: [build, package]
    runs-on: ubuntu-latest

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Generate checksums
        run: |
          cd artifacts
          sha256sum */sdme-*-linux */*.deb */*.rpm > SHA256SUMS
          cat SHA256SUMS

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            artifacts/*/sdme-*-linux
            artifacts/*/*.deb
            artifacts/*/*.rpm
            artifacts/SHA256SUMS
