#!/bin/bash -pe
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2024, Gerrit-John Los <los@lugons.org>

midi() { xterm -e timidity "$@" ; }
image() { display -b "$@" ; }
excel() { xls2lst "$@" |tcol |less ; }
epub() { einfo -pp "$@" |w3m -T text/html ; }
djvu() {
	djview4 \
		-geometry 800x1025-0+0 \
		-zoom=page \
		-layout=single -style=motif \
		-sidebar=no -toolbar=no
}

if [ "$1" = '-H' ] ;then
	shift 1
	video() { mpv -H --geometry=+0+0 -- "$@" ; }
	movie() { mpv -H --geometry=+0+0 -fs -- "$@" ; }
else
	video() { mpv --geometry=+0+0 -- "$@" ; }
	movie() { mpv --geometry=+0+0 -fs -- "$@" ; }
fi

code=0
shopt -s extglob
for x in "$@" ;do
	[ -f "$x" ] ||
		{ [ -f "${x%.part}" ] && x="${x%.part}" ; } ||
		{ [ -f "${x}.part" ] && x="${x}.part" ; } ||
		{ : <"$x" ; continue ; }
	magic=($(file --mime --uncompress --brief --dereference "$x"))
	case "${magic[0]%;}" in
		application/x-decompression-error-gzip-Stdin-has-more-than-one-entry--rest-ignored)
			magic=($(file --mime --brief --dereference "$x"))
		;;
	esac
	app=(unknown)
	case "${magic[0]%;}" in
	audio/@(x-flac|mpeg|mp4|ogg))                   app=(mocp -a) ;;
	audio/@(x-hx-aac-adts|vnd.dolby.dd-raw))        app=(mocp -a) ;;
	audio/midi)                                     app=midi ;;
	audio/x-wav)                                    app=(mocp -a) ;;
	application/epub+zip)            app=epub ;;
	application/msword)              app=(lowriter --quickstart) ;;
	application/@(octet-stream|ogg))
		magic[2]="$(file --uncompress --brief --dereference "$x")"
		case "${magic[2]}" in
		Ogg\ data,\ Theora\ video*)     app=(movie) ;;
		Ogg\ data,\ Skeleton*)          app=(video) ;;
		Ogg\ data,\ Vorbis\ audio*)     app=(mocp -a) ;;
		MPEG\ ADTS,\ layer\ III*)       app=(mocp -a) ;;
		ATSC\ A/52*)                    app=(mocp -a) ;;
		Matroska\ data)                 app=(movie) ;;
		EBML\ file,\ creator\ matroska) app=(movie) ;;
		ISO\ Media,\ MPEG*)             app=(movie) ;;
		ISO\ Media)                     app=(movie) ;;
		Mobipocket\ E-book\ *)          app=fbreader ;;
		Ogg\ data)
			case "$x" in
			*.ogm)  app=(movie) ;;
			esac
		;;
		data)
			case "$x" in
			*.mkv)  app=(movie) ;;
			*.webm) app=(video) ;;
			esac
		;;
		esac 
	;;
	application/pdf)                app=(xpdf -z width) ;;
	application/postscript)         app=gv ;;
	application/vnd.ms-excel)       app=excel ;;
	application/x-directory)        app=recurse ;;
	application/x-empty)            app=ignore ;;
	application/x-tar)              app=view ;;
	image/@(jpeg|png|gif|bmp|avif)) app=image ;;
	image/x-ms-bmp)                 app=image ;;
	image/vnd.djvu)i                app=djvu ;;
	message/news)                   app=view ;;
	text/html)                      app=www ;;
	text/*)
		case "${magic[2]%;}" in
			*=application/epub+zip) app=epub ;;
			*=application/vnd.oasis.opendocument.text)
				app=(lowriter --quickstart)
			;;
			*)                      app=view ;;
		esac
	;;
	video/@(mpeg|mp2p|quicktime|x-msvideo|x-ms-asf|x-matroska|x-m4v))
		app=(movie)
	;;
	video/@(x-flv|mp4)|application/x-shockwave-flash) app=(video) ;;
	video/@(x-ms-asf|webm|ogg))                       app=(video) ;;
	*)
		echo "${0##*/}: $x: don't know how to handle '$magic'" 1>&2
		code=1
		continue
	esac
	"${app[@]}" "$x"
done
exit $code

