diff --git a/xdg-sanity b/xdg-sanity index 7db0629..9a00659 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -20,6 +20,19 @@ set -e argv0="$0" +get_urls() { + i="0" + until ! tomcat replace.urls["$i"] "$file" >/dev/null 2>&1 + do + URLS="$(printf "%s\n%s" \ + "$URLS" \ + "$(tomcat replace.urls["$i"] "$file")" + )" + + i="$(printf "%s+1\n" "$i" | bc)" + done +} + # check if usage is valid if test -z "$1"; then printf "Usage: %s [resource...]\n" "$argv0" 1>&2 @@ -28,8 +41,7 @@ fi for dep in \ curl \ - handlr \ - tomcat + handlr do if ! command -v "$dep" >/dev/null 2>&1; then printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2 @@ -46,9 +58,8 @@ fi while test -n "$1"; do URL="$1" - # use curl(1) to write out the request header's content_type, - # strip everything after the first semicolon, - # chop off any weird whitespace remnants + # use curl(1) to write out the request header's content_type, strip everything + # after the first semicolon, chop off any weird whitespace remnants MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ | xargs echo)" @@ -56,39 +67,40 @@ while test -n "$1"; do MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ | sed -e 's/\/.*\+//g')" - # run through MIME extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ - /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml - do - if test -e "$file"; then - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - MIME="$(tomcat with.mime "$file")" - fi - i="$(printf "%s-1\n" "$i" | bc)" - done - fi - done + # only check for extensions if tomcat(1) is installed + if command -v tomcat >/dev/null 2>&1; then + # run through MIME extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ + /usr/share/xdg-sanity/*-mime.toml \ + /usr/local/share/xdg-sanity/*-mime.toml + do + if test -e "$file"; then + get_urls + for url in $URLS; do + if [ "$MATCH" = "$url" ]; then + MIME="$(tomcat with.mime "$file")" + fi + done + fi + done - # and the replace extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ - /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml - do - if test -e "$file"; then - i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - URL="$(tomcat with.url "$file")" - fi - i="$(printf "%s-1\n" "$i" | bc)" - done - fi - done + # and the replace extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ + /usr/share/xdg-sanity/*-replace.toml \ + /usr/local/share/xdg-sanity/*-replace.toml + do + if test -e "$file"; then + get_urls + for url in $URLS; do + if [ "$MATCH" = "$url" ]; then + URL="$(tomcat with.url "$file")" + fi + done + fi + done + fi # these commands may fail; this is intentional if [ "$MIME" = "text/html" ]; then