name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  publish-platform-artifacts:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
            use-cross: false
          - target: x86_64-apple-darwin
            os: macos-latest
            use-cross: false
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            use-cross: false
            packages: true
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            use-cross: true
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            use-cross: true
            packages: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            use-cross: true
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            use-cross: false
          - target: armv7-unknown-linux-gnueabihf
            os: ubuntu-latest
            use-cross: true

    steps:
      - name: Checkout sources
        uses: actions/checkout@v4

      - name: Sync Rustup toolchain
        run: rustup show

      - name: Add Rust target
        run: rustup target add ${{ matrix.target }}

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

      - name: Build release binary
        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 binary (Windows)
        if: matrix.os == 'windows-latest'
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          BINARY_NAME=mlbt.exe
          strip $BINARY_NAME
          ARCHIVE_NAME=mlbt-${{ matrix.target }}
          tar czvf $ARCHIVE_NAME.tar.gz $BINARY_NAME
          certutil -hashfile $ARCHIVE_NAME.tar.gz sha256 | grep -E [A-Fa-f0-9]{64} > $ARCHIVE_NAME.sha256

      - name: Package binary (macOS and Linux)
        if: matrix.os != 'windows-latest'
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          BINARY_NAME=mlbt
          strip $BINARY_NAME 2>/dev/null || true
          ARCHIVE_NAME=mlbt-${{ matrix.target }}
          tar czvf $ARCHIVE_NAME.tar.gz $BINARY_NAME
          shasum -a 256 $ARCHIVE_NAME.tar.gz > $ARCHIVE_NAME.sha256

      - name: Install packaging tools
        if: matrix.packages
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-deb,cargo-generate-rpm

      - name: Generate .deb package
        if: matrix.packages
        run: cargo deb --no-build --target ${{ matrix.target }}

      - name: Generate .rpm package
        if: matrix.packages
        run: cargo generate-rpm --target ${{ matrix.target }}

      - name: Upload .deb package
        if: matrix.packages
        uses: softprops/action-gh-release@v2
        with:
          files: target/${{ matrix.target }}/debian/*.deb
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload .rpm package
        if: matrix.packages
        uses: softprops/action-gh-release@v2
        with:
          files: target/${{ matrix.target }}/generate-rpm/*.rpm
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload release assets
        uses: softprops/action-gh-release@v2
        with:
          files: |
            target/${{ matrix.target }}/release/mlbt-${{ matrix.target }}.tar.gz
            target/${{ matrix.target }}/release/mlbt-${{ matrix.target }}.sha256
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  update-homebrew:
    name: Update Homebrew formula
    runs-on: ubuntu-latest
    needs: publish-platform-artifacts
    steps:
      - name: Get release version
        run: echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV

      - name: Download checksums
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release download ${{ github.ref_name }} \
            --repo mlb-rs/mlbt \
            --pattern "*.sha256" \
            --dir checksums

      - name: Generate formula
        run: |
          MACOS_ARM_SHA=$(awk '{print $1}' checksums/mlbt-aarch64-apple-darwin.sha256)
          MACOS_INTEL_SHA=$(awk '{print $1}' checksums/mlbt-x86_64-apple-darwin.sha256)
          LINUX_ARM_SHA=$(awk '{print $1}' checksums/mlbt-aarch64-unknown-linux-gnu.sha256)
          LINUX_INTEL_SHA=$(awk '{print $1}' checksums/mlbt-x86_64-unknown-linux-gnu.sha256)

          cat > mlbt.rb << EOF
          class Mlbt < Formula
            desc "Interact with MLB's Statcast API and even watch a live baseball game in your terminal."
            homepage "https://github.com/mlb-rs/mlbt"
            version "${VERSION}"
            license "MIT"

            on_macos do
              on_arm do
                url "https://github.com/mlb-rs/mlbt/releases/download/v#{version}/mlbt-aarch64-apple-darwin.tar.gz"
                sha256 "${MACOS_ARM_SHA}"
              end
              on_intel do
                url "https://github.com/mlb-rs/mlbt/releases/download/v#{version}/mlbt-x86_64-apple-darwin.tar.gz"
                sha256 "${MACOS_INTEL_SHA}"
              end
            end

            on_linux do
              on_arm do
                url "https://github.com/mlb-rs/mlbt/releases/download/v#{version}/mlbt-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "${LINUX_ARM_SHA}"
              end
              on_intel do
                url "https://github.com/mlb-rs/mlbt/releases/download/v#{version}/mlbt-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "${LINUX_INTEL_SHA}"
              end
            end

            def install
              bin.install "mlbt"
            end

            def caveats
              <<~EOS
                Play ball! Run with "mlbt"
              EOS
            end

            test do
              assert_predicate bin/"mlbt", :executable?
            end
          end
          EOF
          sed -i 's/^          //' mlbt.rb

      - name: Update tap repo
        env:
          TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          git clone https://x-access-token:${TAP_TOKEN}@github.com/mlb-rs/homebrew-mlbt.git tap
          cp mlbt.rb tap/mlbt.rb
          cd tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add mlbt.rb
          git commit -m "Update mlbt to ${VERSION}"
          git push

  publish-docker-image:
    runs-on: ubuntu-latest
    permissions:
     contents: read
     packages: write
    steps:
      - name: Checkout Sources
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get tag
        run: |
          git fetch --tags --force
          echo "TAG=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{github.actor}}
          password: ${{secrets.GITHUB_TOKEN}}

      - name: Build and Push Image
        run: |
          docker build . --tag ghcr.io/mlb-rs/mlbt:$TAG
          docker push ghcr.io/mlb-rs/mlbt:$TAG
