#!/usr/bin/env bash
# xcat -- like `cat` but with tail-style headings

if (( ! $# )); then
	echo "Usage: ${0##*/} <file...>" >&2
	exit 2
fi

if [[ -t 1 ]]; then
	header='\e[1m\e[38;5;12m==> %s <==\e[m\n'
else
	header='==> %s <==\n'
fi

if [[ ${0%%*/} == *tail ]]; then
	lines=10
	if [[ $1 == -n ]]; then
		lines=${1#-n}; shift
		if [[ ! $lines ]]; then
			lines=$2; shift
		fi
	fi
	for arg; do
		printf "$header" "$arg"
		tail -n "$lines" "$arg"
		printf '\n'
	done
else
	for arg; do
		printf "$header" "$arg"
		cat "$arg"
		printf '\n'
	done
fi
