#!/usr/bin/env bash

## Description: Tags a version of DXPR CMS and updates version constraints.
## Usage: tag VERSION
## Example: "ddev tag 1.2.3"

# Abort this script if any one step fails.
set -e

VERSION=$1
if [ -z "$VERSION" ]; then
  echo "You need to give me a version number."
  exit 1
fi

COMPONENTS=$(find $PWD -maxdepth 2 -type d -name 'dxpr_cms_*' -or -name project_template)

# Set all components' versions explicitly.
for dir in $COMPONENTS; do
  composer config version $VERSION --working-dir=$dir
done

# Stage all changes, but if we're just testing this script, don't actually commit
# or tag anything.
git add $COMPONENTS
if [[ $2 == "test" ]]; then
  exit 0
fi

# Tag it!
git commit --message=$VERSION
git tag $VERSION

# Put this branch back into its pre-tagged state.
git revert HEAD --no-commit
git commit --all --message="Back to dev."
