name: Release
on:
  push:
    tags:
      - "*/v*.*.*"

jobs:
  Release:
    runs-on: cimaster-latest
    steps:
      - name: 📦 Checkout repository
        uses: actions/checkout@v4

      - name: Get variables
        run: |
          CRATE=$(echo '${{ github.ref_name }}' | awk 'BEGIN{RS="/"} NR==1{print}')
          echo "CRATE=$CRATE" >> "$GITHUB_ENV"
          echo "CRATE_VERSION=$(echo '${{ github.ref_name }}' | awk 'BEGIN{RS="/"} NR==2{print}')" >> "$GITHUB_ENV"
          CL_PATH="CHANGELOG.md"
          if [[ "$CRATE" != "rustypipe" ]]; then pfx="rustypipe-"; CL_PATH="${CRATE#"$pfx"}/$CL_PATH"; fi
          {
            echo 'CHANGELOG<<END_OF_FILE'
            awk 'BEGIN{RS="(^|\n)## [^\n]+\n*"} NR==2 { print }' "$CL_PATH"
            echo END_OF_FILE
          } >> "$GITHUB_ENV"

      - name: 📤 Publish crate on crates.io
        run: cargo publish --token ${{ secrets.CARGO_TOKEN }} --package "${{ env.CRATE }}"

      - name: 🎉 Publish release
        uses: https://gitea.com/actions/release-action@main
        with:
          title: "${{ env.CRATE }} ${{ env.CRATE_VERSION }}"
          body: "${{ env.CHANGELOG }}"
