#!/bin/bash
#

set -e

if ! which json >/dev/null 2>&1; then
  echo "$(basename "$0"): this tool requires jsontool! (try 'npm install -g jsontool')" >&2
  exit 1
fi

if ! which aws >/dev/null 2>&1; then
  echo "$(basename "$0"): this tool requires awscli! (try 'pip install awscli')" >&2
  exit 1
fi

cd "$(dirname "$0")"
cd ..

print_usage () {
  echo "Usage: $(basename "$0") <bump>"
  echo
  echo "  bump - the string 'major', 'minor', or 'patch' denoting the amount by"
  echo "         which to bump the version"
}

confirm_proceed () {
  read -p "${1} [yN]: "
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    exit 1
  fi
}

json_key () {
  local json="${1}"
  local key="${2}"
  echo "${response}" | json "${key}" | tr -d '\n'
}

maybe () {
  if [ -n "$DRY_RUN" ]; then
    echo "$@"
  else
    "$@"
  fi
}

BUMP=$1
OLD_VERSION="$(json version < package.json)"
VERSION_MAJOR="$(echo $OLD_VERSION | cut -d. -f1)"
VERSION_MINOR="$(echo $OLD_VERSION | cut -d. -f2)"
VERSION_PATCH="$(echo $OLD_VERSION | cut -d. -f3)"

case "$BUMP" in
  major)
    ((VERSION_MAJOR+=1))
    ((VERSION_MINOR=0))
    ((VERSION_PATCH=0))
    ;;
  minor)
    ((VERSION_MINOR+=1))
    ((VERSION_PATCH=0))
    ;;
  patch)
    ((VERSION_PATCH+=1))
    ;;
  *)
    print_usage
    exit 1
    ;;
esac

# Check repo environment

if ! (git diff-index --quiet --cached HEAD \
   && git diff-files --quiet \
   && git ls-files --others --exclude-standard); then
  echo "Not creating release in dirty environment: ensure that there are no"
  echo "files in the git index and no tracked files with uncommitted changes."
  exit 1
fi

# Confirm new version

export VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
confirm_proceed "Going to release v${VERSION} -- proceed?"

# Update version in package.json

echo "Bumping version in package.json and committing version bump"
sed -i '' -e 's/"version": "[^"]*"/"version": "'"${VERSION}"'"/' package.json
git commit package.json -m "Bump version -> v${VERSION}"

# Generate package files

echo "Generating package files"
make clean
make dist

echo "Committing release and creating tag"
git add -f pkg/*

tree=$(git write-tree)
parent=$(git rev-parse HEAD)

commit=$(echo "Annotator release v${VERSION}" | git commit-tree "${tree}" -p "${parent}")

git tag "v${VERSION}" "${commit}"
git reset HEAD pkg/

# Upload release to S3
pushd pkg/

echo "Uploading release to S3"
maybe aws s3 sync --acl public-read \
                  --exclude "*" \
                  --include "annotator*.js" \
                  --include "annotator*.css" \
                  --include "annotator*.map" \
                  . \
                  "s3://assets.annotateit.org/annotator/v${VERSION}/"

# Make zips

echo "Making zips for GitHub"

mkdir "annotator.${VERSION}"
ln annotator.*min.{js,css} "annotator.${VERSION}"
zip -r9 "annotator.${VERSION}.zip" "annotator.${VERSION}"

mkdir "annotator-full.${VERSION}"
ln annotator-full.min.js annotator.min.css "annotator-full.${VERSION}"
zip -r9 "annotator-full.${VERSION}.zip" "annotator-full.${VERSION}"
