#!/bin/sh # # backlinks - check for backlinks in extension-less Gemini files # # SPDX-FileCopyrightText: 2023 Daniel Kalak # SPDX-License-Identifier: GPL-3.0-or-later # # This script loops through all files in the current directory that # don't have a file ending and prints: # # "f1 <=> f2" if f1 and f2 link to each other, # # "f1 => f2" if f1 links to f2, but f2 not to f1, # # "=> f1" if no other file links to f1. # Sort $1 and $2 alphabetically and print the first to stdout. alpha_first() { printf '%s\n%s\n' "$1" "$2" | sort | head -n1 } # List all files in the current dir that contain no dots in their names. # Loop over them and call them f1. find -type f -regex '\./[^/.]*' | cut -c3- | while read -r f1 do # List all paths that f1 links to that contain no ":" (i.e. they # are on the same server), no "/" (i.e. they are in the same # dir), and no ".". Loop over them and call them f2. grep -o '^=> [^:/. ]*\( \|$\)' "$f1" | cut -c4- | while read -r f2 do # Does f2 link back to f1? If no, print "f1 => f2". If # yes, print "f1 <=> f2", but only if f1 comes before f2 # in the alphabet. This prevents printing both "f1 <=> # f2" and "f2 <=> f1". if grep -q '^=> '"$f1"'\( \|$\)' "$f2" then [ "$(alpha_first "$f1" "$f2")" = "$f1" ] && printf '%s <=> %s\n' "$f1" "$f2" else printf '%s => %s\n' "$f1" "$f2" fi done # List all files in the current dir that contain no dots in # their names, again. Loop over them and call them f2. If none # of these f2 link to f1, print "=> f1". find -type f -regex '\./[^/.]*' | cut -c3- | { while read -r f2 do grep -q '^=> '"$f1"'\( \|$\)' "$f2" && exit 0 done exit 1 } || printf '=> %s\n' "$f1" done | sort