From e3c870737293dfae39a5b3cfd8d62bdd28f29746 Mon Sep 17 00:00:00 2001
From: Jan Frenzel <jan.frenzel@tu-dresden.de>
Date: Mon, 6 Dec 2021 12:13:11 +0100
Subject: [PATCH] Added more options in check-links.sh and the config file
 link-check-config.json. This closes #183.

---
 doc.zih.tu-dresden.de/util/check-links.sh     | 83 ++++++++++++++-----
 .../util/link-check-config.json               |  7 ++
 2 files changed, 68 insertions(+), 22 deletions(-)
 create mode 100644 doc.zih.tu-dresden.de/util/link-check-config.json

diff --git a/doc.zih.tu-dresden.de/util/check-links.sh b/doc.zih.tu-dresden.de/util/check-links.sh
index 49ee7f2db..1867667ec 100755
--- a/doc.zih.tu-dresden.de/util/check-links.sh
+++ b/doc.zih.tu-dresden.de/util/check-links.sh
@@ -8,57 +8,96 @@
 ##
 ## Author: Martin.Schroschk@tu-dresden.de
 
-set -euo pipefail
+set -eo pipefail
+
+scriptpath=${BASH_SOURCE[0]}
+basedir=`dirname "$scriptpath"`
+basedir=`dirname "$basedir"`
 
 usage() {
-  echo "Usage: bash $0"
+  cat <<-EOF
+usage: $0 [file | -a]
+If file is given, checks whether all links in it are reachable.
+If parameter -a (or --all) is given instead of the file, checks all markdown files.
+Otherwise, checks whether any changed file contains broken links.
+EOF
 }
 
-# Any arguments?
-if [ $# -gt 0 ]; then
-  usage
-  exit 1
-fi
-
 mlc=markdown-link-check
 if ! command -v $mlc &> /dev/null; then
   echo "INFO: $mlc not found in PATH (global module)"
   mlc=./node_modules/markdown-link-check/$mlc
   if ! command -v $mlc &> /dev/null; then
     echo "INFO: $mlc not found (local module)"
-    echo "INFO: See CONTRIBUTE.md for information."
-    echo "INFO: Abort."
     exit 1
   fi
 fi
 
 echo "mlc: $mlc"
 
+LINK_CHECK_CONFIG="$basedir/util/link-check-config.json"
+if [ ! -f "$LINK_CHECK_CONFIG" ]; then
+  echo $LINK_CHECK_CONFIG does not exist
+  exit 1
+fi
+
 branch="preview"
 if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
     branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
 fi
 
-any_fails=false
+function checkSingleFile(){
+  theFile="$1"
+  if [ -e "$theFile" ]; then
+    echo "Checking links in $theFile"
+    if ! $mlc -q -c "$LINK_CHECK_CONFIG" -p "$theFile"; then
+      return 1
+    fi
+  fi
+  return 0
+}
 
-files=$(git diff --name-only "$(git merge-base HEAD "$branch")")
+function checkFiles(){
+any_fails=false
 echo "Check files:"
 echo "$files"
 echo ""
 for f in $files; do
-  if [ "${f: -3}" == ".md" ]; then
-    # do not check links for deleted files
-    if [ "$f" != "doc.zih.tu-dresden.de/README.md" ]; then
-      if [ -e $f ]; then
-        echo "Checking links for $f"
-        if ! $mlc -q -p "$f"; then
-          any_fails=true
-        fi
-      fi
-    fi
+  if checkSingleFile "$f"; then
+    any_fails=true
   fi
 done
 
 if [ "$any_fails" == true ]; then
     exit 1
 fi
+}
+
+function checkAllFiles(){
+files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/ | grep '.md$' || true)
+checkFiles
+}
+
+function checkChangedFiles(){
+files=$(git diff --name-only "$(git merge-base HEAD "$branch")" | grep '.md$' || true)
+checkFiles
+}
+
+if [ $# -eq 1 ]; then
+  case $1 in
+  help | -help | --help)
+    usage
+    exit
+  ;;
+  -a | --all)
+    checkAllFiles
+  ;;
+  *)
+    checkSingleFile "$1"
+  ;;
+  esac
+elif [ $# -eq 0 ]; then
+  checkChangedFiles
+else
+  usage
+fi
diff --git a/doc.zih.tu-dresden.de/util/link-check-config.json b/doc.zih.tu-dresden.de/util/link-check-config.json
new file mode 100644
index 000000000..69bb0110a
--- /dev/null
+++ b/doc.zih.tu-dresden.de/util/link-check-config.json
@@ -0,0 +1,7 @@
+{
+  "ignorePatterns": [
+    {
+      "pattern": "^https://gitlab.hrz.tu-chemnitz.de/zih/hpcsupport/hpc-compendium/-/merge_requests/new$"
+    }
+  ]
+}
-- 
GitLab