#!/usr/bin/env bash
set -u

echo "Testing links stability in documentation"

DOCS_DIR=$1 # The location of documentation is passed as a first parameter

EXPECTED_LINKS_FILE=$2 # The location of expected-links.txt is passed as a second parameter

pushd $DOCS_DIR > /dev/null # Go to docs dir but remember previous location
# We need to go to docs dir to make find return correct relative paths
find . -type f -name "*.html" | LC_ALL=C sort | (popd > /dev/null; diff $EXPECTED_LINKS_FILE -;)
# We list every html file in documentation, then sort it and finally compare to the expected links list.
# Before running diff we need to popd to return to the previous location because we don't have relative path to the expected links file.

if [ ! $? -eq 0 ]
then
  echo "Links changed. If it's intentional, regenerate expected links list by running ./project/scripts/regenerateExpectedLinks $1 $2"
  exit -1
else
  echo "Links OK!"
  exit 0
fi
