#!/bin/bash # show: determine the file type based on its contents and launch the appropriate viewer application, decompressing the file if necessary # Version 2023-10-21 if (( $# == 0 )); then echo Synposis: "$0" file-name >&2; exit 1; fi printf '%s ' "$@" >>/tmp/show-log mime=(mimetype -L -b -M) #(file -L -b --mime-type) #show=("$0") #menu=(dmenu -fn :file=\*/10x20.pcf.gz -l 48) menu=(command menu) html_lynx=(ceterm lynx -force_html -cfg /work/config/lynx) html_w3m=(ceterm w3m -cols 166 -T text/html -o display_link_number -dump) html=("${html_w3m[@]}") cal=(calcurse -D /work/conf/calcurse -i) cal=(vcal -all) text=(ceterm less) image=(sxiv -s f) audio=(mpv -fs -no-video) video=(mpv -fs) dvi=(env LC_CTYPE=C xdvi-xaw \ -s 4 \ -copy \ -mfmode lgtsudfeb \ -p 1308 \ -browser openurl \ -linkstyle 2 \ -gsalpha \ -expertmode 0 \ -hushstdout \ -bg "#$(>/tmp/showlog eval "$2" #echo "pdf=$pdf" >>/tmp/showlog shift 2 fi mainshow() { if [[ -a "$1" ]]; then fname="$1" else fname="$(mktemp --tmpdir download-XXXXXX)" if ! curl -s -S -L -f -o "$fname" -- "$1"; then echo No such file or URL: "$1" >&2 exit 1 fi fi case $("${mime[@]}" "$fname") in application/x-gzip|application/gzip) prog=gunzip ;; application/x-bzip|application/x-bzip2) prog=bunzip2 ;; application/x-xz) prog=unxz ;; application/x-compress) prog=uncompress ;; esac if [[ -v prog ]]; then name="$(mktemp --tmpdir show-XXXXXX)" "$prog" -c "$fname" >"$name" else name="$fname" fi shift if [[ -d "$name" ]]; then cd "$name" || exit #exec "${show[@]}" "$(ls -pA . | "${menu[@]}")" "$@" mainshow "$(ls -pA . | "${menu[@]}")" "$@" fi type="$("${mime[@]}" "$name")" if [[ "$type" == application/zip ]]; then if unzip -p "$name" word/document.xml >/dev/null 2>/dev/null; then type=application/vnd.openxmlformats-officedocument.wordprocessingml.document fi if unzip -p "$name" xl/workbook.xml >/dev/null 2>/dev/null; then type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet fi if unzip -p "$name" content.opf >/dev/null 2>/dev/null; then type=application/epub+zip fi fi case "$type" in text/html|application/xhtml+xml) exec "${html[@]}" "$name" "$@" ;; text/xml|application/x-fictionbook+xml) exec "${fb2[@]}" "$name" "$@" ;; text/calendar) exec "${cal[@]}" "$name" "$@" ;; text/*) exec "${text[@]}" "$name" "$@" ;; application/postscript|image/x-eps) exec "${ps[@]}" "$name" "$@" ;; image/svg+xml) exec "${svg[@]}" "$name" "$@" ;; image/vnd.djvu*) exec "${djvu[@]}" "$name" "$@" ;; image/*) exec "${image[@]}" "$name" "$@" ;; audio/*) exec "${audio[@]}" "$name" "$@" ;; video/*) exec "${video[@]}" "$name" "$@" ;; application/rtf) exec unrtf --text "$name" | "${text[@]}" ;; application/msword) exec catdoc "$name" | "${text[@]}" ;; application/vnd.openxmlformats-officedocument.wordprocessingml.document) { printf "docx2txt"; printf " %s" " $name - | " "${text[@]}"; } >&2; docx2txt "$name" - | "${text[@]}" ;; application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|application/vnd.ms-excel) exec "${spreadsheet[@]}" "$name" ;; application/pdf) #case "$name" in #*.pdf) exec "${pdf[@]}" "$name" "$@" 2>/dev/null ;; #*) # add .pdf extension to work around a bug in mupdf # pname="$(mktemp --tmpdir show-XXXXXX.pdf)" # ln -rfs "$name" "$pname" # exec "${pdf[@]}" "$pname" "$@" 2>/dev/null ;; #esac ;; font/*|application/*font*) exec "${font[@]}" "$name" "$@" ;; application/xml) exec "${fb2[@]}" "$name" "$@" ;; application/epub+zip) exec "${epub[@]}" "$name" "$@" ;; application/x-dvi) sdvi "$name" "$@" ;; application/x-tar) dir=$(mktemp --tmpdir -d show-XXXXXX) tar -x -C "$dir" -f "$name" cd "$dir" || exit sdvi ./*.dvi "$@" ;; *) echo "Unrecognized format: file $name returned $type" >&2 exit 1 ;; esac } mainshow "$@"