2022-11-08 06:05:10 +00:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
2023-03-20 23:22:14 +00:00
|
|
|
|
# Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
2023-03-20 23:12:06 +00:00
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify it under
|
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
|
# later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
|
# details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
|
# with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
|
2022-11-08 06:05:10 +00:00
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
argv0="$0"
|
|
|
|
|
|
2023-03-22 17:41:32 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 06:05:10 +00:00
|
|
|
|
# check if usage is valid
|
2023-03-20 23:12:06 +00:00
|
|
|
|
if test -z "$1"; then
|
2022-11-22 05:02:30 +00:00
|
|
|
|
printf "Usage: %s [resource...]\n" "$argv0" 1>&2
|
|
|
|
|
exit 64 # sysexits(3) EX_USAGE
|
2022-11-08 06:05:10 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2023-03-20 23:12:06 +00:00
|
|
|
|
for dep in \
|
|
|
|
|
curl \
|
2023-03-22 17:41:32 +00:00
|
|
|
|
handlr
|
2023-03-20 23:12:06 +00:00
|
|
|
|
do
|
|
|
|
|
if ! command -v "$dep" >/dev/null 2>&1; then
|
|
|
|
|
printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2
|
|
|
|
|
exit 69 # sysexits(3) EX_UNAVAILABLE
|
|
|
|
|
fi
|
|
|
|
|
done
|
2022-11-08 06:05:10 +00:00
|
|
|
|
|
|
|
|
|
# check if we have a BROWSER
|
2023-03-20 23:12:06 +00:00
|
|
|
|
if test -z "$BROWSER"; then
|
|
|
|
|
printf "%s: \$BROWSER not filled.\n" "$argv0" 1>&2
|
2022-11-22 05:02:30 +00:00
|
|
|
|
exit 71 # sysexits(3) EX_OSERR
|
2022-11-08 06:05:10 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
while test -n "$1"; do
|
2022-11-22 05:02:30 +00:00
|
|
|
|
URL="$1"
|
|
|
|
|
|
2023-03-22 17:41:32 +00:00
|
|
|
|
# use curl(1) to write out the request header's content_type, strip everything
|
|
|
|
|
# after the first semicolon, chop off any weird whitespace remnants
|
2023-03-20 23:12:06 +00:00
|
|
|
|
MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \
|
|
|
|
|
| xargs echo)"
|
2022-11-22 05:02:30 +00:00
|
|
|
|
|
2023-03-22 18:38:04 +00:00
|
|
|
|
# get the URL with no scheme
|
|
|
|
|
NO_SCHEME="$(printf "%s\n" "$URL" | sed -n 's/^h.\+\/\///p')"
|
|
|
|
|
|
2022-11-22 05:02:30 +00:00
|
|
|
|
# get the pattern for the extensions to MATCH
|
2023-03-22 18:38:04 +00:00
|
|
|
|
MATCH="$(printf "%s\n" "$NO_SCHEME" | sed 's/\/.*\+//g')"
|
|
|
|
|
|
|
|
|
|
# get the non-domain of the URI for tacking onto the end of replaced URIs
|
|
|
|
|
URI="$(printf "%s\n" "$NO_SCHEME" | sed -n 's/^[^/]*\///p')"
|
2022-11-22 05:02:30 +00:00
|
|
|
|
|
2023-03-22 17:41:32 +00:00
|
|
|
|
# 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 \
|
2023-03-22 18:38:04 +00:00
|
|
|
|
"$XDG_DATA_HOME"/xdg-sanity/*.mime.toml \
|
|
|
|
|
/usr/share/xdg-sanity/*.mime.toml \
|
|
|
|
|
/usr/local/share/xdg-sanity/*.mime.toml
|
2023-03-22 17:41:32 +00:00
|
|
|
|
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
|
2022-11-22 05:02:30 +00:00
|
|
|
|
|
2023-03-22 18:38:04 +00:00
|
|
|
|
# then replace extensions
|
2023-03-22 17:41:32 +00:00
|
|
|
|
for file in \
|
2023-03-22 18:38:04 +00:00
|
|
|
|
"$XDG_DATA_HOME"/xdg-sanity/*.replace.toml \
|
|
|
|
|
/usr/share/xdg-sanity/*.replace.toml \
|
|
|
|
|
/usr/local/share/xdg-sanity/*.replace.toml
|
2023-03-22 17:41:32 +00:00
|
|
|
|
do
|
|
|
|
|
if test -e "$file"; then
|
|
|
|
|
get_urls
|
|
|
|
|
for url in $URLS; do
|
|
|
|
|
if [ "$MATCH" = "$url" ]; then
|
2023-03-22 18:38:04 +00:00
|
|
|
|
URL="$(tomcat with.url "$file")$URI"
|
2023-03-22 17:41:32 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
done
|
2023-03-22 18:38:04 +00:00
|
|
|
|
|
|
|
|
|
# and the scheme extensions
|
|
|
|
|
for file in \
|
|
|
|
|
"$XDG_DATA_HOME"/xdg-sanity/*.scheme.toml \
|
|
|
|
|
/usr/share/xdg-sanity/*.scheme.toml \
|
|
|
|
|
/usr/local/share/xdg-sanity/*.scheme.toml
|
|
|
|
|
do
|
|
|
|
|
get_urls
|
|
|
|
|
for url in $URLS; do
|
|
|
|
|
if [ "$URL" = "$url" ]; then
|
|
|
|
|
URL="$(tomcat with.scheme "$file")$MATCH"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
done
|
2023-03-22 17:41:32 +00:00
|
|
|
|
fi
|
2022-11-08 06:05:10 +00:00
|
|
|
|
|
|
|
|
|
# these commands may fail; this is intentional
|
2022-11-22 05:02:30 +00:00
|
|
|
|
if [ "$MIME" = "text/html" ]; then
|
|
|
|
|
"$BROWSER" "$URL"
|
|
|
|
|
else
|
2023-03-20 23:12:06 +00:00
|
|
|
|
handlr launch "$MIME" -- "$URL"
|
2022-11-22 05:02:30 +00:00
|
|
|
|
fi
|
|
|
|
|
shift
|
2022-11-08 06:05:10 +00:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
exit 0
|