name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            archive_ext: tar.gz
            use_cross: false
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            archive_ext: tar.gz
            use_cross: false
          - os: macos-13
            target: x86_64-apple-darwin
            archive_ext: tar.gz
            use_cross: false
          - os: macos-latest
            target: aarch64-apple-darwin
            archive_ext: tar.gz
            use_cross: false
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            archive_ext: zip
            use_cross: false

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

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Install Protoc
        uses: arduino/setup-protoc@v3
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install System Dependencies (Linux)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Install Cross
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build Unix / Windows
        run: |
          cargo build --release
        shell: bash

      - name: Install Packaging Tools (Linux)
        if: runner.os == 'Linux'
        run: |
          cargo install cargo-deb
          cargo install cargo-generate-rpm
        shell: bash

      - name: Generate DEB & RPM (Linux)
        if: runner.os == 'Linux'
        run: |
          cargo deb --no-build
          cargo generate-rpm
        shell: bash



      - name: Strip Binary (Unix)
        if: runner.os != 'Windows'
        run: strip target/release/rms-memory



      - name: Package (Unix / macOS)
        if: runner.os != 'Windows'
        run: |
          mkdir -p staging
          cp target/release/rms-memory staging/rms-memory
          
          # Copy deb and rpm to staging if Linux
          if [ "${{ runner.os }}" == "Linux" ]; then
            cp target/debian/*.deb staging/ || true
            cp target/generate-rpm/*.rpm staging/ || true
          fi
          
          cd staging
          tar -czf ../rms-memory-${{ matrix.target }}.${{ matrix.archive_ext }} rms-memory
          if [ "${{ runner.os }}" == "Linux" ]; then
            mv *.deb ../ || true
            mv *.rpm ../ || true
          fi
        shell: bash

      - name: Package (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir staging
          cp target\release\rms-memory.exe staging\rms-memory.exe
          cd staging
          Compress-Archive -Path rms-memory.exe -DestinationPath ..\rms-memory-${{ matrix.target }}.${{ matrix.archive_ext }}
        shell: pwsh

      - name: Upload Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: rms-memory-${{ matrix.target }}
          path: |
            rms-memory-${{ matrix.target }}.${{ matrix.archive_ext }}
            *.deb
            *.rpm
          retention-days: 1

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Display structure of downloaded files
        run: ls -R artifacts/

      - name: Create Release and Upload Assets
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: artifacts/**/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
