good practice changes, removed config file, and replaced dc(1) with bc(1)

This commit is contained in:
Emma Tebibyte 2023-03-20 19:12:06 -04:00
parent 00bd98df9a
commit b636494fd7
Signed by: emma
GPG Key ID: 6D661C738815E7DD
2 changed files with 37 additions and 49 deletions

View File

View File

@ -1,47 +1,45 @@
#!/bin/sh #!/bin/sh
# Copyright (c) 20222023 Emma Tebibyte
# 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/.
set -e set -e
argv0="$0" argv0="$0"
# grabs configuration files
CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml
if ! test -e "$CONFIG"; then
touch "$CONFIG"
fi
# check if usage is valid # check if usage is valid
if ! test -n "$1"; then if test -z "$1"; then
printf "Usage: %s [resource...]\n" "$argv0" 1>&2 printf "Usage: %s [resource...]\n" "$argv0" 1>&2
exit 64 # sysexits(3) EX_USAGE exit 64 # sysexits(3) EX_USAGE
fi fi
# check if we have curl(1) for dep in \
if ! command -v curl >/dev/null 2>&1; then curl \
printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 handlr \
exit 69 # sysexits(3) EX_UNAVAILABLE tomcat
fi do
if ! command -v "$dep" >/dev/null 2>&1; then
# check if we have tomcat(1) printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2
if ! command -v tomcat >/dev/null 2>&1; then exit 69 # sysexits(3) EX_UNAVAILABLE
printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 fi
exit 69 # sysexits(3) EX_UNAVAILABLE done
fi
# set the XDG_COMMAND
test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg "$CONFIG")"
! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \
&& XDG_COMMAND=false \
|| true
# check if we have a BROWSER # check if we have a BROWSER
test -n "$BROWSER" || BROWSER="$(tomcat tools.browser "$CONFIG")" if test -z "$BROWSER"; then
if ! test -n "$BROWSER"; then printf "%s: \$BROWSER not filled.\n" "$argv0" 1>&2
printf "\
%s: \$BROWSER not filled.
Please place the path to your preferred browser's executable in
$XDG_CONFIG_HOME/xdg-sanity.toml
" "$argv0" 1>&2
exit 71 # sysexits(3) EX_OSERR exit 71 # sysexits(3) EX_OSERR
fi fi
@ -51,12 +49,12 @@ while test -n "$1"; do
# use curl(1) to write out the request header's content_type, # use curl(1) to write out the request header's content_type,
# strip everything after the first semicolon, # strip everything after the first semicolon,
# chop off any weird whitespace remnants # chop off any weird whitespace remnants
MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \
xargs echo)" | xargs echo)"
# get the pattern for the extensions to MATCH # get the pattern for the extensions to MATCH
MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \
sed -e 's/\/.*\+//g')" | sed -e 's/\/.*\+//g')"
# run through MIME extensions # run through MIME extensions
for file in \ for file in \
@ -70,7 +68,7 @@ while test -n "$1"; do
if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then
MIME="$(tomcat with.mime "$file")" MIME="$(tomcat with.mime "$file")"
fi fi
i="$(dc -e "$i 1 - p")" i="$(printf "%s-1\n" "$i" | bc)"
done done
fi fi
done done
@ -85,9 +83,9 @@ while test -n "$1"; do
i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)"
while ! [ "$i" = 0 ]; do while ! [ "$i" = 0 ]; do
if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then
URL="$(tomcat with.url "$file")" URL="$(tomcat with.url "$file")"
fi fi
i="$(dc -e "$i 1 - p")" i="$(printf "%s-1\n" "$i" | bc)"
done done
fi fi
done done
@ -96,17 +94,7 @@ while test -n "$1"; do
if [ "$MIME" = "text/html" ]; then if [ "$MIME" = "text/html" ]; then
"$BROWSER" "$URL" "$BROWSER" "$URL"
else else
case "$(command -v $XDG_COMMAND)" in handlr launch "$MIME" -- "$URL"
*/handlr )
handlr launch "$MIME" -- "$URL"
;;
*/xdg-open )
"$(xdg-mime query default "$MIME")" "$URL"
;;
false )
exit 69 # sysexits(3) EX_UNAVAILABLE
;;
esac
fi fi
shift shift
done done