env/bin/plumb.sh

90 lines
1.6 KiB
Bash

#!/bin/sh
# o
# Written in 2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
printf "Usage: %s [-T] [URI|file]\n" "${0##*/}">&2
exit 1
}
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
check_required_program()
{
command -v "$1" >/dev/null 2>&1 || err "$1: program not found"
}
check_required_program fetch
Tflag=
while getopts T flag; do
case $flag in
T) Tflag=-T
;;
*) usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -le 1 ] || usage
target=
if [ $# -eq 1 ]; then
target=$1
else
IFS= read -r target
fi
[ -n "$target" ] || usage
case $target in
http://* | https://*)
file=$(fetch $Tflag "$target") || err "couldn't fetch $target"
;;
*) [ -f "$target" ] || err "can't handle $uri"
file=$target
;;
esac
mimetype=$(file -bi "$file")
case $mimetype in
audio/*)
check_required_program mpv
mpv --no-video "$file"
;;
image/*)
check_required_program sxiv
sxiv "$file"
;;
text/html)
check_required_program w3m
check_required_program xterm
xterm -e w3m -T text/html "$file"
;;
text/plain | application/x-shellscript)
check_required_program ${PAGER:-less}
check_required_program xterm
xterm -e ${PAGER:-less} "$file"
;;
video/*)
check_required_program mpv
mpv "$file"
;;
*) err "don't know how to open $mimetype"
;;
esac