name: Release

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

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

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

    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2
        with:
          key: release-${{ matrix.target }}

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

      - name: Package (Unix)
        if: matrix.archive == 'tar.gz'
        run: |
          staging="nighthawk-${{ github.ref_name }}-${{ matrix.target }}"
          mkdir "$staging"
          cp target/${{ matrix.target }}/release/nh "$staging/"
          cp target/${{ matrix.target }}/release/nighthawk-daemon "$staging/"
          cp LICENSE README.md "$staging/"
          tar czf "$staging.tar.gz" "$staging"

      - name: Package (Windows)
        if: matrix.archive == 'zip'
        shell: bash
        run: |
          staging="nighthawk-${{ github.ref_name }}-${{ matrix.target }}"
          mkdir "$staging"
          cp target/${{ matrix.target }}/release/nh.exe "$staging/"
          cp target/${{ matrix.target }}/release/nighthawk-daemon.exe "$staging/"
          cp LICENSE README.md "$staging/"
          7z a "$staging.zip" "$staging"

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          draft: true
          files: nighthawk-${{ github.ref_name }}-${{ matrix.target }}.*
