---
name: Deploy

on:
  push:
    tags:
      - '*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  test:
    uses: ./.github/workflows/test.yml

  containers:
    uses: ./.github/workflows/containers.yml

  build:
    name: Build and release
    runs-on: ${{ matrix.os }}
    needs:
      - test
      - containers

    strategy:
      matrix:
        include:
          - build: linux
            os: ubuntu-latest
            target: x86_64-unknown-linux-musl

          - build: macos
            os: macos-latest
            target: x86_64-apple-darwin

          - build: windows
            os: windows-latest
            target: x86_64-pc-windows-msvc

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Get the release version from the tag
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

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

      # Linux-only prerequisites
      - name: Install system deps (musl)
        if: matrix.build == 'linux'
        run: |
          sudo apt-get update -y
          sudo apt-get install -y musl-dev musl-tools

      - name: Install packaging tools (RPM/DEB)
        if: matrix.build == 'linux'
        run: |
          cargo install cargo-generate-rpm
          cargo install cargo-deb --force --locked

      # Build once for Linux (musl)
      - name: Build Linux (musl)
        if: matrix.build == 'linux'
        run: cargo build --release --locked --target ${{ matrix.target }}

      # Strip once and prep paths for both packagers
      - name: Strip and prepare packaging paths (Linux)
        if: matrix.build == 'linux'
        run: |
          BIN="target/${{ matrix.target }}/release/s3m"
          ls -al "$BIN"
          strip -s "$BIN" || true
          # Ensure cargo-generate-rpm's asset path exists without copying the file
          mkdir -p target/release
          ln -sf "../${{ matrix.target }}/release/s3m" "target/release/s3m"
          ls -al target/release/s3m

      - name: Generate RPM
        if: matrix.build == 'linux'
        run: |
          cargo generate-rpm
          RPM_PATH="$(find target/generate-rpm -type f -name '*.rpm' -print -quit)"
          echo "RPM_ASSET=$RPM_PATH" >> $GITHUB_ENV
          echo "RPM: $RPM_PATH"

      - name: Generate DEB (musl static)
        if: matrix.build == 'linux'
        run: |
          cargo deb --no-build
          DEB_PATH="$(find target/debian -maxdepth 1 -type f -name '*.deb' -print -quit)"
          echo "DEB_ASSET=$DEB_PATH" >> $GITHUB_ENV
          echo "DEB: $DEB_PATH"

      # Non-Linux builds (macOS, Windows)
      - name: Build
        if: matrix.build != 'linux'
        run: cargo build --release --locked --target ${{ matrix.target }}

      - name: Build archive
        shell: bash
        run: |
          binary_name="s3m"
          dirname="$binary_name-${{ env.VERSION }}-${{ matrix.target }}"
          mkdir "$dirname"

          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            mv "target/${{ matrix.target }}/release/$binary_name.exe" "$dirname"
          else
            mv "target/${{ matrix.target }}/release/$binary_name" "$dirname"
          fi

          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            7z a "$dirname.zip" "$dirname"
            echo "ASSET=$dirname.zip" >> $GITHUB_ENV
          else
            tar -czf "$dirname.tar.gz" "$dirname"
            echo "ASSET=$dirname.tar.gz" >> $GITHUB_ENV
          fi

      # Skip release if the tag starts with 't'
      - name: Release
        if: startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref_name, 't')
        uses: softprops/action-gh-release@v2
        with:
          draft: false
          files: |-
            ${{ env.ASSET }}
            ${{ env.RPM_ASSET }}
            ${{ env.DEB_ASSET }}

  publish:
    name: Publish
    runs-on: ubuntu-latest
    needs:
      - build
    # Only publish to crates.io on tag pushes that DON'T start with 't'
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref_name, 't')
    continue-on-error: true
    steps:
      - name: Checkout sources
        uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - run: cargo publish --token ${CRATES_TOKEN}
        env:
          CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}

  package:
    name: PackageCloud
    needs:
      - build
    # Only upload to PackageCloud on tag pushes that DON'T start with 't'
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref_name, 't')
    uses: ./.github/workflows/packagecloud.yml
    secrets: inherit
