# Bitbucket Pipelines — fafb-q release builds
#
# Triggers:
#   - Tag push (v*): builds Linux x86_64 + ARM64 musl
#   - Manual (custom): same build from any branch, for ad-hoc testing

image: rust:1.88-bookworm

definitions:
  steps:
    - step: &build-linux
        name: Build Linux release artifacts
        size: 2x
        caches:
          - cargo-registry
          - cargo-target
        script:
          # Install musl toolchain for x86_64 and aarch64 musl cross-compiler
          - apt-get update && apt-get install -y --no-install-recommends musl-tools curl
          - curl -sSL https://musl.cc/aarch64-linux-musl-cross.tgz | tar xz -C /opt/
          - export PATH="/opt/aarch64-linux-musl-cross/bin:$PATH"

          - rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl

          # Configure linker and CC for aarch64-musl cross-compilation
          - mkdir -p .cargo
          - |
            cat > .cargo/config.toml <<'EOF'
            [target.aarch64-unknown-linux-musl]
            linker = "aarch64-linux-musl-gcc"
            EOF
          - export CC_aarch64_unknown_linux_musl=aarch64-linux-musl-gcc

          # Determine version from tag or commit SHA
          - |
            if [[ -n "${BITBUCKET_TAG:-}" ]]; then
              export VERSION="$BITBUCKET_TAG"
            else
              export VERSION="dev-${BITBUCKET_COMMIT:0:7}"
            fi

          # Clean stale artifacts from prior cached builds
          - rm -rf target/release-artifacts

          # Build and package both Linux targets
          - >-
            ./scripts/release.sh
            --target x86_64-unknown-linux-musl
            --target aarch64-unknown-linux-musl
            --version "$VERSION"

          - ls -lh target/release-artifacts/*.tar.gz

        artifacts:
          - target/release-artifacts/*.tar.gz

    - step: &upload-downloads
        name: Upload to Bitbucket Downloads
        script:
          - UPLOAD_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/downloads"
          - |
            for artifact in target/release-artifacts/*.tar.gz; do
              echo "Uploading $(basename "$artifact")..."
              curl -X POST "$UPLOAD_URL" \
                --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" \
                --form files=@"$artifact" \
                --fail --show-error

              # Upload a "latest" copy so README URLs never go stale
              latest_name=$(basename "$artifact" | sed 's/fafb-q-v[0-9.]*-/fafb-q-latest-/; s/fafb-q-dev-[a-f0-9]*-/fafb-q-latest-/')
              cp "$artifact" "target/release-artifacts/$latest_name"
              echo "Uploading $latest_name..."
              curl -X POST "$UPLOAD_URL" \
                --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" \
                --form files=@"target/release-artifacts/$latest_name" \
                --fail --show-error
            done

          # Upload the install script (stable URL, always overwritten)
          - |
            echo "Uploading install.sh..."
            curl -X POST "$UPLOAD_URL" \
              --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" \
              --form files=@"scripts/install.sh" \
              --fail --show-error
          # Upload the rolling-upgrade script (stable URL, always overwritten)
          - |
            echo "Uploading rolling-upgrade.sh..."
            curl -X POST "$UPLOAD_URL" \
              --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" \
              --form files=@"scripts/rolling-upgrade.sh" \
              --fail --show-error

    - step: &cargo-publish
        name: Publish to crates.io
        caches:
          - cargo-registry
          - cargo-target
        script:
          - cargo publish --token "${CARGO_REGISTRY_TOKEN}" --allow-dirty

  caches:
    cargo-registry:
      key:
        files:
          - Cargo.lock
      path: /usr/local/cargo/registry
    cargo-target:
      key:
        files:
          - Cargo.lock
      path: target

pipelines:
  tags:
    'v*':
      - step: *build-linux
      - step: *upload-downloads
      - step: *cargo-publish

  custom:
    release-build:
      - step: *build-linux
      - step: *upload-downloads
