72323b
#!/bin/sh
72323b
#
72323b
# To use this filter with less, define LESSOPEN:
72323b
# export LESSOPEN="|/usr/bin/lesspipe.sh %s"
72323b
#
72323b
# The script should return zero if the output was valid and non-zero
72323b
# otherwise, so less could detect even a valid empty output
72323b
# (for example while uncompressing gzipped empty file).
72323b
# For backward-compatibility, this is not required by default. To turn
72323b
# this functionality there should be another vertical bar (|) straight
72323b
# after the first one in the LESSOPEN environment variable:
72323b
# export LESSOPEN="||/usr/bin/lesspipe.sh %s"
72323b
72323b
if [ ! -e "$1" ] ; then
72323b
	exit 1
72323b
fi
72323b
72323b
if [ -d "$1" ] ; then
72323b
	ls -alF -- "$1"
72323b
	exit $?
72323b
fi
72323b
72323b
exec 2>/dev/null
72323b
72323b
# Allow for user defined filters
72323b
if [ -x ~/.lessfilter ]; then
72323b
	~/.lessfilter "$1"
72323b
	if [ $? -eq 0 ]; then
72323b
		exit 0
72323b
	fi
72323b
fi
72323b
72323b
manfilter ()
72323b
{
72323b
	if test -x /usr/bin/man ; then
72323b
		# See rhbz#1241543 for more info.  Well, actually we firstly
72323b
		# used 'man -l', then we switched to groff, and then we again
72323b
		# switched back to 'man -l'.
72323b
		/usr/bin/man -P /usr/bin/cat -l "$1"
72323b
	elif test -x /usr/bin/groff; then
72323b
		# This is from pre-rhbz#1241543-time.
72323b
		groff -Tascii -mandoc "$1" | cat -s
72323b
	else
72323b
		echo "WARNING:"
72323b
		echo "WARNING: to better show manual pages, install 'man-db' package"
72323b
		echo "WARNING:"
72323b
		cat "$1"
72323b
	fi
72323b
}
72323b
72323b
export MAN_KEEP_FORMATTING=1
72323b
72323b
case "$1" in
72323b
*.[1-9n].bz2|*.[1-9]x.bz2|*.man.bz2|*.[1-9n].[gx]z|*.[1-9]x.[gx]z|*.man.[gx]z|*.[1-9n].lzma|*.[1-9]x.lzma|*.man.lzma)
72323b
	case "$1" in
72323b
	*.gz)		DECOMPRESSOR="gzip -dc" ;;
72323b
	*.bz2)		DECOMPRESSOR="bzip2 -dc" ;;
72323b
	*.xz|*.lzma)	DECOMPRESSOR="xz -dc" ;;
72323b
	esac
72323b
	if [ -n "$DECOMPRESSOR" ] && $DECOMPRESSOR -- "$1" | file - | grep -q troff; then
72323b
		$DECOMPRESSOR -- "$1" | manfilter -
72323b
		exit $?
72323b
	fi ;;&
72323b
*.[1-9n]|*.[1-9]x|*.man)
72323b
	if file "$1" | grep -q troff; then
72323b
		manfilter "$1"
72323b
		exit $?
72323b
	fi ;;&
72323b
*.tar) tar tvvf "$1"; exit $? ;;
72323b
*.tgz|*.tar.gz|*.tar.[zZ]) tar tzvvf "$1"; exit $? ;;
72323b
*.tar.xz) tar Jtvvf "$1"; exit $? ;;
72323b
*.xz|*.lzma) xz -dc -- "$1"; exit $? ;;
72323b
*.tar.bz2|*.tbz2) bzip2 -dc -- "$1" | tar tvvf -; exit $? ;;
72323b
*.[zZ]|*.gz) gzip -dc -- "$1"; exit $? ;;
72323b
*.bz2) bzip2 -dc -- "$1"; exit $? ;;
72323b
*.zip|*.jar|*.nbm) zipinfo -- "$1"; exit $? ;;
72323b
# --nomanifest -> rhbz#1450277
72323b
*.rpm) rpm -qpivl --changelog --nomanifest -- "$1"; exit $? ;;
72323b
*.cpi|*.cpio) cpio -itv < "$1"; exit $? ;;
72323b
*.gpg)
72323b
	if [ -x /usr/bin/gpg2 ]; then
72323b
		gpg2 -d "$1"
72323b
		exit $?
72323b
	elif [ -x /usr/bin/gpg ]; then
72323b
		gpg -d "$1"
72323b
		exit $?
72323b
	else
72323b
		echo "No GnuPG available."
72323b
		echo "Install gnupg2 or gnupg to show encrypted files."
72323b
		exit 1
72323b
	fi ;;
72323b
*.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
72323b
	if [ -x /usr/bin/identify ]; then
72323b
		identify "$1"
72323b
		exit $?
72323b
	elif [ -x /usr/bin/gm ]; then
72323b
		gm identify "$1"
72323b
		exit $?
72323b
	else
72323b
		echo "No identify available"
72323b
		echo "Install ImageMagick or GraphicsMagick to browse images"
72323b
		exit 1
72323b
	fi ;;
72323b
*)
72323b
	if [ -x /usr/bin/file ] && [ -x /usr/bin/iconv ] && [ -x /usr/bin/cut ]; then
72323b
		case `file -b "$1"` in
72323b
		*UTF-16*) conv='UTF-16' ;;
72323b
		*UTF-32*) conv='UTF-32' ;;
72323b
		esac
72323b
		if [ -n "$conv" ]; then
72323b
			env=`echo $LANG | cut -d. -f2`
72323b
			if [ -n "$env" -a "$conv" != "$env" ]; then
72323b
				iconv -f $conv -t $env "$1"
72323b
				exit $?
72323b
			fi
72323b
		fi
72323b
	fi
72323b
	exit 1
72323b
esac