#!/usr/bin/env bash
# fail if any commands fails
set -e

## Setup git user
git config user.name $USER_NAME
git config user.email $EMAIL

## Prevent recursive publishing
if [ $EMAIL = $GIT_CLONE_COMMIT_AUTHOR_EMAIL ]; then
    echo "This is a $USER_NAME commit, skipping publish"
    exit 0
fi

npm link

echo "Debug: Git Status:"
git status
echo "Debug: Git Diff:"
git diff

echo "Bumping and tagging the version"
npm version $(npx release next-version)

echo "Pushing the new package.json to master"
git config --global push.default simple
git push origin HEAD:master && git push --tags

echo "Publishing the new release"
release new --productOwner "The Times Tooling team" --scheduled --skipReleaseNotes
release promote

echo "Publishing to NPM"
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
echo "Publishing to NPM"
npm publish

echo "Promoting the release"
release promote
