name: Release

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross-compilation tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        timeout-minutes: 10
        run: |
          # Replace flaky Azure/cloud mirrors with canonical Ubuntu mirrors
          sudo sed -i 's|http://azure.archive.ubuntu.com/ubuntu|http://archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true
          sudo sed -i 's|http://.*\.clouds\.archive\.ubuntu\.com/ubuntu|http://archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true
          for attempt in 1 2 3; do
            sudo apt-get update -q && break
            echo "apt-get update attempt $attempt failed, retrying..."
            sleep 5
          done
          sudo apt-get install -y --no-install-recommends gcc-aarch64-linux-gnu libc6-dev-arm64-cross
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package (unix)
        if: matrix.archive == 'tar.gz'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../tdg-${{ matrix.target }}.tar.gz tdg
          cd ../../..

      - name: Package (windows)
        if: matrix.archive == 'zip'
        shell: pwsh
        run: Compress-Archive -Path target/${{ matrix.target }}/release/tdg.exe -DestinationPath tdg-${{ matrix.target }}.zip

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

  release:
    name: Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: Dogfood — create .tg archives from each platform binary
        run: |
          tar xzf artifacts/tdg-x86_64-unknown-linux-gnu.tar.gz -C /tmp
          chmod +x /tmp/tdg

          # Create a .tg for each unix target
          for tgz in artifacts/tdg-*.tar.gz; do
            target=$(basename "$tgz" .tar.gz)
            tmpdir=$(mktemp -d)
            tar xzf "$tgz" -C "$tmpdir"
            /tmp/tdg create "artifacts/${target}.tg" "$tmpdir/tdg"
            /tmp/tdg verify "artifacts/${target}.tg"
            rm -rf "$tmpdir"
          done

          # Create a .tg for each windows target
          for z in artifacts/tdg-*.zip; do
            [ -f "$z" ] || continue
            target=$(basename "$z" .zip)
            tmpdir=$(mktemp -d)
            unzip -o "$z" -d "$tmpdir"
            /tmp/tdg create "artifacts/${target}.tg" "$tmpdir/tdg.exe"
            /tmp/tdg verify "artifacts/${target}.tg"
            rm -rf "$tmpdir"
          done

      - name: Install b3sum
        run: cargo install b3sum

      - name: Generate checksums
        run: |
          cd artifacts
          sha256sum tdg-* > SHA256SUMS
          b3sum tdg-* > B3SUMS

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish --token ${{ secrets.CRATES_TOKEN }} --allow-dirty
