#!/bin/sh # Copyright (c) 2022–2023 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 argv0="$0" # check if usage is valid if test -z "$1"; then printf "Usage: %s [resource...]\n" "$argv0" 1>&2 exit 64 # sysexits(3) EX_USAGE fi for dep in \ curl \ handlr \ tomcat 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 # check if we have a BROWSER if test -z "$BROWSER"; then printf "%s: \$BROWSER not filled.\n" "$argv0" 1>&2 exit 71 # sysexits(3) EX_OSERR 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 MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ | xargs echo)" # get the pattern for the extensions to MATCH 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 # 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 # these commands may fail; this is intentional if [ "$MIME" = "text/html" ]; then "$BROWSER" "$URL" else handlr launch "$MIME" -- "$URL" fi shift done exit 0