80 lines
2.0 KiB
Bash
Executable File
80 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
argv0="$0"
|
|
|
|
# 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
|
|
}
|
|
|
|
# check if usage is valid
|
|
if ! test -n "$1"; then
|
|
printf "Usage: %s [resource...]\n" "$argv0"
|
|
exit 64 # sysexits(3) EX_USAGE
|
|
fi
|
|
|
|
# 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
|
|
|
|
# check if we have handlr(1), but we might not need it
|
|
if ! command -v handlr >/dev/null 2>&1; then
|
|
alias handlr=false
|
|
fi
|
|
|
|
test -n "$BROWSER" || BROWSER="$(stream_config | sed -ne 's/^browser *= *//p')"
|
|
|
|
# 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
|
|
" "$argv0"
|
|
exit 71 # sysexits(3) EX_OSERR
|
|
fi
|
|
|
|
while test -n "$1"; do
|
|
argv1="$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
|
|
MIME="$(curl -Ls -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')
|
|
|
|
for extension in /etc/xdg-sanity/extensions/*.sh; do
|
|
! [ "$extension" = "/etc/xdg-sanity/extensions/*.sh" ] || break
|
|
# switch(extension_type)
|
|
case "$(sed -ne 's/^# EXT-TYPE=//p' <"$extension" | xargs echo)" in
|
|
replace)
|
|
argv1="$("$extension" "$argv1")"
|
|
;;
|
|
mime)
|
|
argv1="$("$extension" "$argv1")"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# these commands may fail; this is intentional
|
|
if [ "$MIME" = "text/html" ]; then
|
|
"$BROWSER" "$argv1"
|
|
elif ! handlr launch "$MIME" -- "$argv1"; then
|
|
printf "%s: %s: handlr(1) failed: " "$argv0" "$argv1"
|
|
[ "$(command -v handlr)" = "false" ] \
|
|
&& printf "'handlr' program not found\n" \
|
|
|| printf "non-zero exit status\n"
|
|
fi
|
|
|
|
shift
|
|
done
|
|
|
|
exit 0
|