rusefi_documentation/wiki-tools/brokenlinks.sh

78 lines
2.2 KiB
Bash
Raw Normal View History

2021-02-15 18:21:10 -08:00
#!/usr/bin/env bash
escape() {
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1";
}
export -f escape
escapeReplace() {
sed 's/[&/\]/\\&/g' <<<"$1";
}
export -f escapeReplace
searchfile() {
2021-02-15 19:40:53 -08:00
while IFS= read -r -u 3 link; do
2021-02-15 20:11:08 -08:00
if echo $link | grep -E '^[http|\/]' >/dev/null; then
2021-02-15 18:21:10 -08:00
continue
fi
2021-02-16 05:40:55 -08:00
if echo $link | grep -E '^\.\/' >/dev/null; then
NEWLINK=$(echo $link | sed 's/^\.\///')
echo "In $1:"
echo $link
echo $NEWLINK | cat --number
read PICK
if [ $PICK -eq 1 ]; then
REPLACE=$(escape '('$link')')
REPLACEWITH=$(escapeReplace "$NEWLINK")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
fi
fi
2021-02-15 18:52:31 -08:00
if [ $(find . -name "$link"".md" 2>/dev/null | wc -l) -gt 0 ]; then
2021-02-15 18:21:10 -08:00
continue
fi
2021-02-15 18:52:31 -08:00
if echo "$link" | grep "#" >/dev/null; then
continue
fi
if echo "$link" | grep -Ev ".md$" >/dev/null; then
if ls "$link" 2>/dev/null >/dev/null; then
continue
else
echo "In $1:"
2021-02-15 18:52:31 -08:00
echo $link
2021-02-16 05:40:55 -08:00
SEARCH='*'$(basename "$link" | sed 's/[-_ ]/*/g')'*'
FILES=$(find . -iname "$SEARCH")
2021-02-15 19:47:06 -08:00
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
echo "Could not find"
continue
fi
2021-02-15 18:52:31 -08:00
echo "$FILES" | cat --number
read PICK
2021-02-17 13:26:36 -08:00
if ! [[ $PICK =~ ^[0-9]+$ ]]; then
2021-02-16 13:05:38 -08:00
continue
fi
2021-02-16 05:40:55 -08:00
FILE=$(echo "$FILES" | head -n $PICK | tail -n 1 | sed 's/^\.\///')
2021-02-15 18:52:31 -08:00
REPLACE=$(escape '('$link')')
REPLACEWITH=$(escapeReplace "$FILE")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
continue
fi
fi
echo "In $1:"
2021-02-15 18:21:10 -08:00
echo $link
2021-02-16 05:40:55 -08:00
SEARCH='*'$(basename "$link" | sed 's/[-_ ]/*/g')'*'
FILES=$(find . -iname "$SEARCH")
2021-02-15 19:47:06 -08:00
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
echo "Could not find"
continue
fi
2021-02-15 18:21:10 -08:00
echo "$FILES" | cat --number
read PICK
2021-02-17 13:26:36 -08:00
if ! [[ $PICK =~ ^[0-9]+$ ]]; then
2021-02-16 13:05:38 -08:00
continue
fi
2021-02-15 18:52:31 -08:00
FILE=$(basename "$(echo "$FILES" | head -n $PICK | tail -n 1)" .md)
REPLACE=$(escape '('$link')')
REPLACEWITH=$(escapeReplace "$FILE")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
2021-02-15 19:40:53 -08:00
done 3< <(grep -oP '(?<=\]\().+?(?=[\)])' "$1")
2021-02-15 18:21:10 -08:00
}
export -f searchfile
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;