#!/bin/sh

# Publish all crates
# Usage: ./publish.sh [--dry-run]

set -e

DRY_RUN=false
if [ "$1" = "--dry-run" ]; then
  DRY_RUN=true
  echo "Running in dry-run mode..."
fi

if [ -n "$(git status --porcelain)" ]; then
  echo "You have local changes!"
  exit 1
fi

pushd crates

for crate in $(cargo package --workspace 2>&1 | grep Packaging | sed 's_.*crates/\(.*\))_\1_' | grep -v Packaging); do
  echo "Publishing ${crate}"
  pushd "$crate"
  if [ "$DRY_RUN" = true ]; then
    echo "  Running cargo publish --dry-run for $crate_name"
    cargo publish --dry-run
  else
    echo "  Publishing $crate_name to crates.io"
    cargo publish
  fi
  popd
done

popd

echo "Publishing root crate"
cargo publish
