name: Release

on:
  push:
    branches: ["release"]

env:
  CARGO_TERM_COLOR: always
  BINARY_NAME: frust

jobs:
  check-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.version }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get version from Cargo.toml
        id: get_version
        run: |
          VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*= *"//' | sed 's/"//')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Detected version: $VERSION"

      - name: Ensure version tag does not already exist
        run: |
          VERSION="${{ steps.get_version.outputs.version }}"
          if git tag --list | grep -qx "v${VERSION}"; then
            echo "ERROR: tag v${VERSION} already exists. Bump the version in Cargo.toml before merging to release."
            exit 1
          fi

  build:
    needs: check-version
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            use_cross: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            use_cross: true
          - target: armv7-unknown-linux-musleabihf
            os: ubuntu-latest
            use_cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
            use_cross: false
          - target: aarch64-apple-darwin
            os: macos-latest
            use_cross: false
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            use_cross: false

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@cross

      - name: Build
        shell: bash
        run: |
          if [ "${{ matrix.use_cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi

      - name: Package artifact
        shell: bash
        run: |
          TARGET="${{ matrix.target }}"
          VERSION="${{ needs.check-version.outputs.version }}"
          BIN="target/${TARGET}/release/${BINARY_NAME}"
          ARCHIVE="${BINARY_NAME}-v${VERSION}-${TARGET}"

          if [[ "$TARGET" == *"windows"* ]]; then
            BIN="${BIN}.exe"
            7z a "${ARCHIVE}.zip" "$BIN"
            echo "ASSET=${ARCHIVE}.zip" >> $GITHUB_ENV
          else
            tar czf "${ARCHIVE}.tar.gz" -C "$(dirname "$BIN")" "$(basename "$BIN")"
            echo "ASSET=${ARCHIVE}.tar.gz" >> $GITHUB_ENV
          fi

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

  release:
    needs: [check-version, build]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

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

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.check-version.outputs.version }}
          name: v${{ needs.check-version.outputs.version }}
          files: artifacts/*
          generate_release_notes: true
