#!/usr/bin/env bash

set -o errexit
set -o nounset

printf "== Begin: build-docs.sh ==\n"

echo "Environment:"
env | fgrep 'GIT' | sort

if [ -z "$GIT_BASE_COMMIT" -o -z "$GIT_HEAD_COMMIT" ]; then
    echo "GIT_BASE_COMMIT and GIT_HEAD_COMMIT must both be set" >&2
    exit 1
fi

# TRAVIS_COMMIT_RANGE=${GIT_BASE_COMMIT}..${GIT_HEAD_COMMIT}

# CHANGED_DOCS=$(git diff --name-only  "${GIT_BASE_COMMIT}..${GIT_HEAD_COMMIT}" -- 'docs')

# if [ -z "$CHANGED_DOCS" ]; then
#     echo "No changes in ambassador/docs, so not building docs"
#     exit 0
# fi

# From ./ambassador/.ci/pr-build-website-preview master

set -o xtrace
set -o verbose

website_branch=master
build_dir=$(pwd)

rm -rf "/tmp/getambassador.io-${website_branch}"
git clone --single-branch --branch="$website_branch" https://d6e-automaton:${GH_TOKEN}@github.com/datawire/getambassador.io.git "/tmp/getambassador.io-${website_branch}"
cd "/tmp/getambassador.io-${website_branch}"
git submodule update --init
pyenv local 3.7.0
python3 --version

# Swap out the "latest" info for our stuff.
rm -rf submodules/latest
mkdir submodules/latest
cp -prv $build_dir/ambassador/docs submodules/latest/docs

# Since we only want the one preview, toss out links to other versions from the header...
sed -e '/to="\/docs\/[0-9]/d' < src/components/Header/Header.js > H2.js && mv H2.js src/components/Header/Header.js

# # ...and in docs-structure.
# rm -f docs-structure/docs/[0-9]*

npm install
npm run build

# From ./ambassador/.ci/publish-website-preview master

if ! [[ -d /tmp/getambassador.io-${website_branch}/public ]]; then
	echo '[publish-website-preview] skipping: website preview was not built'
	exit 0
fi

npm install netlify-cli -g

netlify deploy \
	--dir="/tmp/getambassador.io-${website_branch}/public" \
	--message="ambassador.git preview $website_branch (pipeline $PIPELINE_ID)" \
	--site=1d6f5395-6386-49af-8b47-e85aa28488f8 \
	--auth="${NETLIFY_AUTH_TOKEN}" \
	--json \
	> /tmp/netlify-deploy.json

cat /tmp/netlify-deploy.json

cat >/tmp/github-status.json <<EOF
{
  "context": "website-preview",
  "state": "success",
  "target_url": $(jq .deploy_url </tmp/netlify-deploy.json),
  "description": "Website ${website_branch} preview"
}
EOF

curl --fail \
	-H "Accept: application/json" \
	-H "Content-Type: application/json" \
	-X POST \
	--data '@/tmp/github-status.json' \
	"https://${GH_TOKEN}@api.github.com/repos/datawire/apro/statuses/${CIRCLE_SHA1}"

# Install the broken link checker...

${build_dir}/ambassador/.ci/install-blc

# ...then continue with stuff from blc-website-preview

set -o verbose

pushd "/tmp/getambassador.io-$website_branch/public"
python3 -m http.server 2>/dev/null &
trap "kill $!" EXIT
popd

cd /tmp/getambassador.io-blc
./blc.js http://localhost:8000/ > /tmp/blc.txt

set +o verbose
num_complaints=$(grep ^Page /tmp/blc.txt | sort -u | wc -l)
RED=$'\033[1;31m'
GRN=$'\033[1;32m'
BLD=$'\033[1m'
END=$'\033[0m'
if [[ $num_complaints -eq 0 ]]; then
	printf "%s======================= 0 broken-link-checker complaints for %s =======================%s\n" "$GRN$BLD" "$website_branch" "$END"
else
	printf "%s======================= %d broken-link-checker complaints for %s ======================%s\n" "$RED$BLD" $num_complaints "$website_branch" "$END"
	grep ^Page /tmp/blc.txt | sort -u
	printf "%s====================== end broken-link-checker complaints for %s ======================%s\n" "$RED$BLD" "$website_branch" "$END"
	exit $num_complaints
fi

# Don't post to GitHub.
