#!/usr/bin/bash

if [ $# -eq 0 ]; then
	exec cat
fi

MIMELIST=$(file --mime-type "$@" | awk -F ': *' '{print $2}')

shopt -s extglob

function cat_or_lzop() {
	if [[ "$(file "$1")" =~ "lzop compressed data" ]]; then
		lzop -d < "$1" | cat
	else
		cat "$1"
	fi
}

for mime in $MIMELIST; do
	case $mime in
		application/?(x-)bzip2)
			bzcat "$1"
			;;
		application/?(x-)gzip)
			zcat "$1"
			;;
		application/?(x-)lzma)
			lzcat "$1"
			;;
		application/?(x-)xz)
			xzcat "$1"
			;;
		application/?(x-)lz4)
			lz4cat "$1"
			;;
		application/?(x-)zstd)
			zstdcat "$1"
			;;
		application/octet-stream)
			cat_or_lzop "$1"
			;;
		*)
			cat "$1"
			;;
	esac
	shift 1
done
