#!/bin/sh set -e rel () { to="$1" from="$2" while test $(printf '%s\n' "$to" "$from" | grep / | wc -l) = 2; do todir="$(printf '%s\n' "$to" | cut -d / -f 1)" fromdir="$(printf '%s\n' "$from" | cut -d / -f 1)" if test ! "$todir" = "$fromdir"; then break fi to="$(printf '%s\n' "$to" | cut -d / -f 2-)" from="$(printf '%s\n' "$frm" | cut -d / -f 2-)" done while test -n "$(printf '%s\n' "$from" | grep /)"; do from="$(printf '%s\n' "$from" | cut -d / -f 2-)" to="../$to" done printf '%s\n' "$to" } lines=$(sed 's/\\$//' < icons.mk | sed '/^\s*\(#.*\|\)$/d' | sed 1d | wc -l) test ! $lines = 0 test $(sed 's/\\$//' < icons.mk | sed '/^\s*\(#.*\|\)$/d' | sed 1d | sed 's/\t/ /g' | grep '^ ' | wc -l) = $lines test $(sed 's/\\$//' < icons.mk | sed '/^\s*\(#.*\|\)$/d' | sed '1s/^.*$//' | grep -n ' ' | wc -l) = 0 stack="" i=0 sed 's/\\$//' < icons.mk | sed '/^\s*\(#.*\|\)$/d' | sed 1d | sed 's/^\t/x/' | while read L; do i=$(( i + 1 )) printf 'Checking %i of %i\n\033[A' $i $lines >&2 tabs=0 while test ! $(printf '%s\n' "$L" | sed -n '/^x\t/p' | wc -l) = 0; do L="$(printf '%s\n' "$L" | sed 's/^x\t/x/')" tabs=$(( tabs + 1 )) done L="$(printf '%s\n' "$L" | sed 's/^x//')" f="scalable/$L.svg" if test ! $(printf '%s\n' $f | wc -l) = 1; then printf '\033[K%s contains whitespace\n' "$L" >&2 exit 1 fi tabsplus1=$(( tabs + 1 )) if test -z "$(printf '%s\n' $stack x | sed -n ${tabsplus1}p)"; then printf '\033[K%s is overtabulated\n' "$L" >&2 exit 1 else stack="$(printf '%s\n' $stack | head -n $tabs; printf '%s\n' "$L")" fi if test ! -e "$f"; then if test -L "$f"; then printf '\033[K%s is a dangling symlink\n' "$L" >&2 else printf '\033[K%s is listed but does not exist\n' "$L" >&2 fi exit 1 fi if test $tabs = 0; then if test -L "$f"; then printf '\033[K%s is a symlink but not indented\n' "$L" >&2 exit 1 fi else goal_="$(printf '%s\n' $stack | sed -n "${tabs}p")" if test ! -L "$f"; then printf '\033[K%s is not a symlink but listed as linking to %s\n' "$L" "$goal" >&2 exit 1 fi target="$(realpath -- "$f")" goal="$(realpath -- "scalable/${goal_}.svg")" if test "$target" = "$goal"; then diff="same real file" else diff="different real file" fi target="$(readlink -- "$f")" goal="$(rel "${goal_}.svg" "$L.svg")" if test "$target" = "./$goal"; then ln -sf -- "$target" "$f" elif test ! "$target" = "$goal"; then printf '\033[K%s links to %s but should link to %s (%s)\n' "$L" "$target" "$goal" "$diff" >&2 exit 1 fi fi done printf '\033[K' >&2