name: CI

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

jobs:

  linux:
    name: Linux
    runs-on: ubuntu-latest
    environment: Linux
    steps:
      - uses: actions/checkout@v4

      - name: Install depends
        run: |
          sudo apt update && sudo apt install upx rename cmake build-essential -y

      - name: Get tag
        if: startsWith(github.ref, 'refs/tags/')
        id: tag
        uses: dawidd6/action-get-tag@v1
        with:
          strip_v: true

      - name: Build
        run: |
          cargo build --release

      - name: Tag Check
        run: |
          echo "tag=${{ steps.tag.outputs.tag }}" >> $GITHUB_ENV
          echo "tag=${{ steps.tag.outputs.tag }}" >> $GITHUB_OUTPUT
          if [ -z "${{ steps.tag.outputs.tag }}" ]; then
            echo "tag=latest" >> $GITHUB_OUTPUT
            echo "tag=latest" >> $GITHUB_ENV
          fi
          
      - uses: actions-rs/toolchain@v1
        with:
            toolchain: stable
            override: true

      - uses: katyo/publish-crates@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
            registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
            ignore-unpublished-changes: true
      
      - name: Upload binaries to GitHub Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          token: ${{ secrets.CR_PAT }}
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') }}
          generate_release_notes: true
