#!/usr/bin/env bash

#
# Releases a new version of Neos UI
#
# Expects the following environment variables:
#
# VERSION          the version that is "to be released"
# BRANCH           the branch that is worked on
# NPM_TOKEN        the npm auth token

# break on all failures
set -xe

if [ -z "$VERSION" ]; then echo "\$VERSION not set"; exit 1; fi
if [ -z "$BRANCH" ]; then echo "\$BRANCH not set"; exit 1; fi
if [ -z "$NPM_TOKEN" ]; then echo "\$NPM_TOKEN not set"; exit 1; fi


# go to root directory of Neos.Neos.Ui
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/../../

# go to $BRANCH
git reset --hard origin/$BRANCH

# install yarn if not already
path_to_yarn=$(which yarn)
if [ -z "$path_to_yarn" ] ; then
    echo "installing yarn:"
    npm install -g yarn
fi

# Load bashrc to have nvm available
source ~/.bashrc

# switch node version
nvm install
nvm use

# install dependencies and login to npm
make install

# actual release process

# build
make build-subpackages

# code quality
# @todo: Improve to return correct response for passing tests in Jenkins
#make lint
#make test

# publishing
VERSION=$VERSION make bump-version
VERSION=$VERSION NPM_TOKEN=$NPM_TOKEN make publish-npm

# we do not commit our working dir (the changes to the `version` field, as they will cause conflicts)
# see https://github.com/neos/neos-ui/issues/3778

git tag -a -m "$VERSION" $VERSION
git push origin $VERSION
