name: Publish to crates.io
on:
  workflow_dispatch:
jobs:
  checks:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.get_tag.outputs.tag }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Check HEAD has a tag
        id: get_tag
        run: |
          TAG=$(git tag --points-at HEAD | head -n1)
          if [ -z "$TAG" ]; then
            echo "Error: HEAD commit has no tag. Please tag this commit before publishing."
            exit 1
          fi
          echo "Found tag: $TAG"
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
      - name: Check tag matches Cargo.toml version
        run: |
          TAG="${{ steps.get_tag.outputs.tag }}"
          # Strip leading 'v' if present (v0.4.1 -> 0.4.1)
          VERSION="${TAG#v}"
          CARGO_VERSION=$(grep -m1 '^version' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          if [ "$VERSION" != "$CARGO_VERSION" ]; then
            echo "Error: Tag version ($VERSION) does not match Cargo.toml version ($CARGO_VERSION)"
            exit 1
          fi
          echo "Tag matches Cargo.toml version: $VERSION"
      - name: Check version not already on crates.io
        run: |
          TAG="${{ steps.get_tag.outputs.tag }}"
          # Strip leading 'v' if present (v0.4.1 -> 0.4.1)
          VERSION="${TAG#v}"
          echo "Checking if $VERSION exists on crates.io..."

          CRATE_NAME=$(grep -m1 '^name' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/$CRATE_NAME/$VERSION")
          if [ "$HTTP_STATUS" = "200" ]; then
            echo "Error: Version $VERSION already exists on crates.io"
            exit 1
          fi
          echo "Version $VERSION not yet published, proceeding."
  ci:
    needs: checks
    uses: ./.github/workflows/ci.yml
  publish:
    needs: ci
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Install packages needed for build
        run: sudo apt-get update && sudo apt-get install -y build-essential libgtksourceview-5-dev
      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
      - name: Publish to crates.io
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
  appimage:
    needs: publish
    strategy:
      matrix:
        include:
          - runner: ubuntu-24.04
            arch: x86_64
          - runner: ubuntu-24.04-arm
            arch: aarch64
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
      - name: Install packages needed for build
        run: sudo apt-get update && sudo apt-get install -y build-essential libgtksourceview-5-dev librsvg2-bin libfuse2
      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
      - name: Build release binary
        run: cargo build --release
      - name: Convert SVG icon to PNG
        run: rsvg-convert -w 256 -h 256 assets/mergers.svg -o mergers.png
      - name: Download appimagetool
        run: |
          curl -fsSL -o appimagetool https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${{ matrix.arch }}.AppImage
          chmod +x appimagetool
      - name: Build AppImage
        run: |
          mkdir -p AppDir/usr/bin
          cp target/release/mergers AppDir/usr/bin/mergers
          cp assets/mergers.desktop AppDir/mergers.desktop
          cp mergers.png AppDir/mergers.png
          ln -s usr/bin/mergers AppDir/AppRun
          ./appimagetool --appimage-extract-and-run AppDir mergers-${{ matrix.arch }}.AppImage
      - name: Upload AppImage to GitHub release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          TAG="${{ needs.checks.outputs.tag }}"
          gh release create "$TAG" --title "$TAG" --generate-notes 2>/dev/null || true
          gh release upload "$TAG" mergers-${{ matrix.arch }}.AppImage --clobber
