#!/bin/bash -eu

# Lints Markdown files using Mobify's `.remarkrc` configuration.
BIN="${BASH_SOURCE[0]}"

# We jump through some hoops here because we symlink the node module's
# bin into the docs/bin/ directory and we want to support invoking
# via the symlink and directly via `/docs/bin/lint-md`
while [ -h "$BIN" ]; do
    # Dereference the symlink
    BIN="$(cd "$(dirname "$BIN")" && pwd)/$(readlink "$BIN")"
done

DOCS_DIR="$(cd "$(dirname "$BIN")/.." && pwd)"
REMARK_RC="$DOCS_DIR/remarkrc"

if [[ $(node -v) =~ ^v[0-4] ]]; then
	# Node v4!!
	# This is tricky. `remark` is a dep of mobify-code-style
	# and so won't show up in `node_modules/.bin` of a project
	# that uses mobify-code-style. We have to move into the
	# the mobify-code-style /docs directory and find the npm
	# bin directory relative that _that_ to find remark!
    REMARK_CLI="$(cd "$DOCS_DIR" && npm bin)/remark"
else
    REMARK_CLI="$(npm bin)/remark"
fi

$REMARK_CLI --rc-path $REMARK_RC "$@"
