#!/bin/bash

function user_continue() {
  read -p "Do you want to continue? [y/n]" -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    exit 1
  fi
}

if [ "$#" -ne 5 ]; then
  echo "USAGE: `basename $0` <branch> <release-version> <next-version> <sonatype-user> <sonatype-passwd>"
  exit
fi

if [ ! -f "distribution/release" ]; then
  echo "Script must be executed in the root directory of this project"
  exit
fi

BRANCH="${1}"
RELEASE_VERSION="${2}"
if [[ ! "${RELEASE_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "<release-version> must be of the form 'MAJOR.MINOR.REVISION'"
  exit
fi
NEXT_VERSION="${3}"
if [[ ! "${NEXT_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+-SNAPSHOT$ ]]; then
  echo "<next-version> must be of the form 'MAJOR.MINOR.REVISION-SNAPSHOT'"
  exit
fi
SONATYPE_USER="${4}"
SONATYPE_PASSWORD="${5}"

if [ -z $(git config --get user.signingkey) ]; then
  echo "Git signing must be enabled. Add user.signingkey to ~/.gitconfig"
  exit
fi

if [ $(git tag -l | grep "$RELEASE_VERSION") ]; then
  echo "Tag ${RELEASE_VERSION} already exists"
  exit
fi

echo "================================================================="
echo "BEGIN RELEASE"
echo "BRANCH TO TAG:   ${BRANCH}"
echo "RELEASE VERSION: ${RELEASE_VERSION}"
echo "NEXT VERSION:    ${NEXT_VERSION}"
echo "================================================================="
user_continue

# update pom to release version
./mvn_cmd clean
./mvn_cmd versions-set ${RELEASE_VERSION}
echo "Updated pom to release version ${RELEASE_VERSION}"
user_continue

# commit pom changes
git commit -a -m "Update version for ${RELEASE_VERSION} release."

# tag the release version
git tag -s v${RELEASE_VERSION} -m "Tagging ${RELEASE_VERSION} release."
echo "Tagged release ${RELEASE_VERSION}"

# update pom to the next version
./mvn_cmd versions-set ${NEXT_VERSION}
echo "Updated pom to next version ${NEXT_VERSION}"

# commit pom changes
git commit -a -m "Bump version to ${NEXT_VERSION}."

# push commits
git push origin ${BRANCH}

# push release tag
git push origin v${RELEASE_VERSION}

# checkout the release tag
git checkout v${RELEASE_VERSION}
echo "Switched to the tag version ${RELEASE_VERSION}"

# build the release distribution
./mvn_cmd install
./mvn_cmd bundle-create
gpg --armor --detach-sign distribution/target/ldaptive-${RELEASE_VERSION}-dist.tar.gz
gpg --armor --detach-sign distribution/target/ldaptive-${RELEASE_VERSION}-dist.zip

# update the javadocs
echo "Updating javadocs"
user_continue

git checkout gh-pages
git pull origin gh-pages
# remove root directory javadocs
git rm -r javadocs/org javadocs/*.html javadocs/*.css javadocs/*.js javadocs/package-list
# add new javadocs to root directory
cp distribution/target/ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar javadocs
pushd javadocs
jar xf ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar
rm -rf META-INF ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar
popd
# add new javadocs to release version directory
mkdir javadocs/${RELEASE_VERSION}
cp distribution/target/ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar javadocs/${RELEASE_VERSION}
pushd javadocs/${RELEASE_VERSION}
jar xf ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar
rm -rf META-INF ldaptive-distribution-${RELEASE_VERSION}-javadoc.jar
popd
git add javadocs
git commit -a -m "Updated javadocs for ${RELEASE_VERSION} release."
echo "Committed new javadocs"

# update the spring ext schema
echo "Updating schema"
user_continue

cp beans/target/classes/org/ldaptive/beans/spring/spring-ext.xsd schema/spring-ext.xsd
cp beans/target/classes/org/ldaptive/beans/spring/spring-ext.xsd schema/spring-ext-${RELEASE_VERSION}.xsd
git add schema
git commit -a -m "Updated schema for ${RELEASE_VERSION} release."
echo "Committed new schema"

# add new binaries
echo "Adding release binaries"
user_continue

mkdir downloads/${RELEASE_VERSION}
cp distribution/target/ldaptive-${RELEASE_VERSION}-dist* downloads/${RELEASE_VERSION}
git add downloads/${RELEASE_VERSION}
git commit -a -m "Added binaries for ${RELEASE_VERSION} release."
echo "Committed new release binaries"

# push changes to the server
git push origin gh-pages

# upload bundle jars to sonatype
echo "Uploading bundle jars to sonatype"
user_continue

curl -i -u ${SONATYPE_USER}:${SONATYPE_PASSWORD} \
  -F "file=@target/ldaptive-parent-"${RELEASE_VERSION}"-bundle.jar" \
  "https://oss.sonatype.org/service/local/staging/bundle_upload"
curl -i -u ${SONATYPE_USER}:${SONATYPE_PASSWORD} \
  -F "file=@core/target/ldaptive-"${RELEASE_VERSION}"-bundle.jar" \
  "https://oss.sonatype.org/service/local/staging/bundle_upload"
curl -i -u ${SONATYPE_USER}:${SONATYPE_PASSWORD} \
  -F "file=@json/target/ldaptive-json-"${RELEASE_VERSION}"-bundle.jar" \
  "https://oss.sonatype.org/service/local/staging/bundle_upload"
curl -i -u ${SONATYPE_USER}:${SONATYPE_PASSWORD} \
  -F "file=@beans/target/ldaptive-beans-"${RELEASE_VERSION}"-bundle.jar" \
  "https://oss.sonatype.org/service/local/staging/bundle_upload"
curl -i -u ${SONATYPE_USER}:${SONATYPE_PASSWORD} \
  -F "file=@templates/target/ldaptive-templates-"${RELEASE_VERSION}"-bundle.jar" \
  "https://oss.sonatype.org/service/local/staging/bundle_upload"

echo "Finished release ${RELEASE_VERSION} for ldaptive."

