#!/bin/bash -u
# -u Treat unset variables as an error when substituting.
#  More info can be found by typing `help set` in any bash shell

# The return value of a pipeline is the status of the last command to exit with
# a non-zero status, or zero if no command exited with a non-zero status.
set -o pipefail

# Identifies the path that the script is in (http://stackoverflow.com/a/246128/11807)
MYPATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# ROOT is the root directory of your project. It isn't always needed if your
# script is at the root of your project, but its a useful convention to adopt
# so that your scripts can be moved and the only thing that needs to change
# then is ROOT.
ROOT=$( cd $MYPATH/.. && pwd )

cd $ROOT

# Figure out the current major.minor version (docs don't version on patches)
CURRENT_VERSION=$(npm view . version)
CURRENT_SHORT_VERSION="$(echo "$CURRENT_VERSION" | cut -d. -f1).$(echo "$CURRENT_VERSION" | cut -d. -f2)"

# Use npm to bump the minor version number. This assumes that the minor version was previously bumped
# and sitting at v0.XX.Y-dev and will move to v0.XX.Y when we run this command.
TARGET_VERSION=$(npm --no-git-tag-version version minor | tr -d 'v')
TARGET_SHORT_VERSION="$(echo "$TARGET_VERSION" | cut -d. -f1).$(echo "$TARGET_VERSION" | cut -d. -f2)"

DOCS_DIR="$ROOT/docs/public"
SOURCE_DIR="$DOCS_DIR/dev"
TARGET_DIR="$DOCS_DIR/$TARGET_SHORT_VERSION"

if [ -d $TARGET_DIR ]; then
    echo "Documentation directory for version $TARGET_SHORT_VERSION already exists at $TARGET_DIR"
    echo "Exiting..."
    exit
fi

echo "Last version is $CURRENT_SHORT_VERSION"
echo "Making docs for $TARGET_SHORT_VERSION"
echo "Creating $TARGET_DIR"
echo

echo "Copying $SOURCE_DIR -> $TARGET_DIR"
cp -R "$SOURCE_DIR" "$TARGET_DIR"

# Update version number in $TARGET_DIR and 'dev'
echo "Updating version $CURRENT_VERSION to $TARGET_VERSION..."

# Long Versions
REPLACE_TARGET_VERSION=$(echo "$TARGET_VERSION" | sed "s/\./\\\./g")
REPLACE_SOURCE_VERSION=$(echo "$CURRENT_VERSION" | sed "s/\./\\\./g")

# Short Versions
REPLACE_TARGET_SHORT_VERSION=$(echo "$TARGET_SHORT_VERSION" | sed "s/\./\\\./g")
REPLACE_SOURCE_SHORT_VERSION=$(echo "$CURRENT_SHORT_VERSION" | sed "s/\./\\\./g")

# Bump version number in or new doc version dir to the latest release version
cd "$TARGET_DIR"
egrep -lR "$REPLACE_SOURCE_VERSION" --include \*.md . | tr '\n' '\0' | xargs -0 -n1 sed -i '' "s/$REPLACE_SOURCE_VERSION/$REPLACE_TARGET_VERSION/g" 2>/dev/null
egrep -lR "$REPLACE_SOURCE_SHORT_VERSION" --include \*.md . | tr '\n' '\0' | xargs -0 -n1 sed -i '' "s/$REPLACE_SOURCE_SHORT_VERSION/$REPLACE_TARGET_SHORT_VERSION/g" 2>/dev/null

# Bump version number in `dev` dir to the latest release version
cd "$SOURCE_DIR"
egrep -lR "$REPLACE_SOURCE_VERSION" --include \*.md . | tr '\n' '\0' | xargs -0 -n1 sed -i '' "s/$REPLACE_SOURCE_VERSION/$REPLACE_TARGET_VERSION/g" 2>/dev/null
egrep -lR "$REPLACE_SOURCE_SHORT_VERSION" --include \*.md . | tr '\n' '\0' | xargs -0 -n1 sed -i '' "s/$REPLACE_SOURCE_SHORT_VERSION/$REPLACE_TARGET_SHORT_VERSION/g" 2>/dev/null

# Link "latest" folder to the new version
cd $DOCS_DIR
rm -f latest
ln -s $TARGET_SHORT_VERSION latest

# Point "current" to the new version
echo "Updating current version in harp.json and updating versions in public/_data.json"
SCRIPT="
var fs = require('fs');

var harpJson = '$ROOT/docs/harp.json';
var harpConfig = require(harpJson);
harpConfig.globals.currentVersion = \"$TARGET_SHORT_VERSION\";
fs.writeFileSync(harpJson, JSON.stringify(harpConfig, null, 4));

var dataJson = '$ROOT/docs/public/_data.json';
var dataConfig = require(dataJson);
dataConfig.versions.unshift(\"$CURRENT_SHORT_VERSION\");
fs.writeFileSync(dataJson, JSON.stringify(dataConfig, null, 4));
"
node -e "$SCRIPT"

echo "DONE!"
