xdg-sanity/xdg-sanity.sh

70 lines
1.8 KiB
Bash
Raw Normal View History

2022-09-01 20:17:23 -06:00
#!/bin/sh
2022-09-09 19:34:14 -06:00
argv0="$0"
argv1="$1"
2022-09-03 01:35:42 -06:00
2022-09-09 19:34:14 -06:00
# cats out the configuration or {'\n', EOF} if it can't be found
stream_config(){
cat "$HOME"/.xdg-sanity.conf 2>/dev/null \
|| cat "$HOME"/.config/xdg-sanity.conf 2>/dev/null \
|| cat /etc/xdg-sanity/xdg-sanity.conf 2>/dev/null \
|| echo # empty line
}
2022-09-03 19:26:23 -06:00
2022-09-09 19:34:14 -06:00
# check if usage is valid
if ! test -n "$1"; then
printf "Usage: %s [resource...]\n" "$argv0"
exit 64 # sysexits(3) EX_USAGE
fi
2022-09-09 19:34:14 -06:00
# check if we have curl(1)
if ! command -v curl >/dev/null 2>&1; then
printf "%s: Missing dependency: curl(1)\n" "$argv0"
exit 71 # sysexits(3) EX_OSERR
fi
2022-09-09 19:34:14 -06:00
test -n "$BROWSER" || BROWSER="$(stream_config | sed -ne 's/^browser *= *//p')"
2022-09-03 19:26:23 -06:00
2022-09-09 19:34:14 -06:00
# check if we have a $BROWSER
if ! test -n "$BROWSER"; then
printf "\
%s: \$BROWSER not filled.
Please place the path to your default browser's executable in
/etc/xdg-sanity/xdg-sanity.conf
" "$0"
exit 71 # sysexits(3) EX_OSERR
2022-09-03 01:35:42 -06:00
fi
2022-09-09 19:34:14 -06:00
MIME="$(curl -s -o /dev/null -w '%{content_type}' $1 | sed 's/\;.*//' | xargs echo)"
# RESPONSE_CODE=$(curl -fLIs "$INPUT" | sed -ne 's/ [[:space:]]*$//p' | sed -ne 's|^HTTP/.\+ ||p')
2022-09-03 01:35:42 -06:00
2022-09-09 19:34:14 -06:00
for EXT in /etc/xdg-sanity/extensions/*.sh; do
if [ "$EXT" = "/etc/xdg-sanity/extensions/*.sh" ]; then
echo "No extensions to load"
else
for EXT in /etc/xdg-sanity/extensions/*.sh; do
TYPE=$(cat $EXT | sed -ne 's/^# EXT-TYPE=//p' | tr -d '\n')
echo "Found $TYPE extension $EXT"
2022-09-03 01:35:42 -06:00
2022-09-09 19:34:14 -06:00
if [ "$TYPE" = "replace" ]; then
echo "Modifying $INPUT..."
argv1=$($EXT "$argv1")
echo "Got $INPUT"
elif [ "$TYPE" = "mime" ]; then
echo "Modifying MIME type..."
MIME="$($EXT "$argv1")"
echo "Got $MIME"
fi
done
fi
2022-09-09 19:34:14 -06:00
done
2022-09-03 19:26:23 -06:00
2022-09-09 19:34:14 -06:00
# determine the MIME type, strip everything after the first semicolon, and
# check to see if it's HTML
if [ "$MIME"= "text/html" ]; then
$BROWSER "$1"
2022-09-01 20:17:23 -06:00
else
2022-09-09 19:34:14 -06:00
handlr launch "$MIME" -- "$1"
fi