name: Release
on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"

env:
  BIN_NAME: dotr
  PROJECT_NAME: DotR
  REPO_NAME: uroybd/dotr

jobs:
  build:
    name: Build release binaries
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
            name: dotr-aarch64-apple-darwin.tar.gz
          - target: x86_64-apple-darwin
            os: macos-15-intel
            name: dotr-x86_64-apple-darwin.tar.gz
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: dotr-x86_64-unknown-linux-gnu.tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            name: dotr-aarch64-unknown-linux-gnu.tar.gz
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            name: dotr-x86_64-unknown-linux-musl.tar.gz
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            name: dotr-aarch64-unknown-linux-musl.tar.gz
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross-compilation tool
        if: matrix.os == 'ubuntu-latest'
        run: |
          cargo install cross --git https://github.com/cross-rs/cross

      - name: Build release binary (native)
        if: matrix.os != 'ubuntu-latest'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build release binary (cross-compile)
        if: matrix.os == 'ubuntu-latest'
        run: cross build --release --target ${{ matrix.target }}

      - name: Package binary
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../${{ matrix.name }} ${{ env.BIN_NAME }}
          cd -

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

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Generate Changelog
        id: changelog
        uses: orhun/git-cliff-action@v4
        with:
          config: cliff.toml
          args: --latest --strip header
        env:
          OUTPUT: CHANGELOG.md

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: artifacts/**/*.tar.gz
          body: ${{ steps.changelog.outputs.content }}

  homebrew:
    name: Update Homebrew Tap
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          repository: uroybd/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Update formula with multi-arch support
        run: |
          VERSION="${{ github.ref_name }}"
          VERSION_NUM="${VERSION#v}"

          # Calculate checksums
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-aarch64-apple-darwin.tar.gz" -o arm64.tar.gz
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-x86_64-apple-darwin.tar.gz" -o intel.tar.gz
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-x86_64-unknown-linux-gnu.tar.gz" -o linux.tar.gz
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-aarch64-unknown-linux-gnu.tar.gz" -o linux-arm64.tar.gz
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-x86_64-unknown-linux-musl.tar.gz" -o linux-musl.tar.gz
          curl -sL "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-aarch64-unknown-linux-musl.tar.gz" -o linux-arm64-musl.tar.gz

          ARM64_SHA=$(shasum -a 256 arm64.tar.gz | awk '{print $1}')
          INTEL_SHA=$(shasum -a 256 intel.tar.gz | awk '{print $1}')
          LINUX_SHA=$(shasum -a 256 linux.tar.gz | awk '{print $1}')
          LINUX_ARM64_SHA=$(shasum -a 256 linux-arm64.tar.gz | awk '{print $1}')
          LINUX_MUSL_SHA=$(shasum -a 256 linux-musl.tar.gz | awk '{print $1}')
          LINUX_ARM64_MUSL_SHA=$(shasum -a 256 linux-arm64-musl.tar.gz | awk '{print $1}')

          # Update formula
          cat > Formula/dotr.rb << EOF
          class Dotr < Formula
            desc "A dotfiles manager that is as dear as a daughter"
            homepage "https://github.com/uroybd/DotR"
            version "${VERSION_NUM}"
            
            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-aarch64-apple-darwin.tar.gz"
                sha256 "${ARM64_SHA}"
              end
              if Hardware::CPU.intel?
                url "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-x86_64-apple-darwin.tar.gz"
                sha256 "${INTEL_SHA}"
              end
            end

            on_linux do
              if Hardware::CPU.intel?
                url "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "${LINUX_SHA}"
              end
              if Hardware::CPU.arm?
                url "https://github.com/uroybd/DotR/releases/download/${VERSION}/dotr-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "${LINUX_ARM64_SHA}"
              end
            end

            def install
              bin.install "dotr"
            end

            test do
              system "#{bin}/dotr", "--help"
            end
          end
          EOF

          # Commit and push
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/dotr.rb
          git commit -m "Update dotr to ${VERSION}"
          git push
