#! /bin/sh
set -e
VERSION=$1
PROJECT=pdf

if [ "$VERSION" == "" ]; then
   echo "A release needs a version number!"
   exit 1
fi

DOCSUBDIR=$PROJECT/$VERSION

cat Cargo.toml \
    | sed -e "s#^version.*#version = \"$VERSION\"#" \
    > Cargo.toml.new
mv Cargo.toml.new Cargo.toml

LOG=`mktemp --suffix=.buildlog`

function error {
  cat $LOG
  exit 1
}

echo Testing ...
cargo test > $LOG 2>&1 || error

echo Comitting the relase ...
git add Cargo.toml README.md
git commit -m "Release $VERSION"
git tag -a v$VERSION
git push --all
git push --tags

echo "Publishing the release to crates.io ..."
cargo publish
